all files / src/redux/reducers/ network.js

78.65% Statements 70/89
67.44% Branches 29/43
85.71% Functions 6/7
85.53% Lines 65/76
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196                23×                             71× 71×   71×     10×       10× 10× 10× 10× 20×   10× 10×   10×                         12×         12× 12×       12× 12× 12×     12× 12×     12×     12×                                                                                                                                                   43×      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.initialState = undefined;
exports.default = networkReducer;
 
var _network = require('../actions/network');
 
var Actions = _interopRequireWildcard(_network);
 
function _interopRequireWildcard(obj) { Eif (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
 
function _defineProperty(obj, key, value) { Iif (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
 
var initialState = exports.initialState = {
  pending: {},
  success: {},
  error: {},
  activeErrors: {
    application: [],
    form: []
  },
  backlog: [],
  errorTimeout: null,
  progress: {},
  progressReset: false
};
 
function networkReducer() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  var action = arguments[1];
 
  switch (action.type) {
    case Actions.ADD_NETWORK_CALL:
      {
        var id = action.id,
            label = action.label,
            ts = action.ts;
 
        var pending = Object.assign({}, state.pending, _defineProperty({}, id, { id: id, label: label, ts: ts }));
        var success = Object.assign({}, state.success);
        var error = Object.assign({}, state.error);
        var backlog = [].concat(state.backlog, success[id], error[id]).filter(function (el) {
          return !!el;
        });
        delete success[id];
        delete error[id];
 
        return Object.assign({}, state, { pending: pending, success: success, error: error, backlog: backlog });
      }
 
    case Actions.SUCCESS_NETWORK_CALL:
      {
        if (!state.pending[action.id]) {
          // FIXME: strange behavior where action.id is null
          return state;
        }
        var _pending = Object.assign({}, state.pending);
        var callToMove = Object.assign({}, _pending[action.id], {
          resp: action.resp
        });
        var _success = Object.assign({}, state.success, _defineProperty({}, action.id, callToMove));
        delete _pending[action.id];
        return Object.assign({}, state, { pending: _pending, success: _success });
      }
 
    case Actions.ERROR_NETWORK_CALL:
      {
        var _id = action.id,
            resp = action.resp,
            errorTimeout = action.errorTimeout,
            errType = action.errType;
 
        var _pending2 = Object.assign({}, state.pending);
        var _callToMove = Object.assign({}, _pending2[_id], {
          resp: resp,
          invalid: false
        });
        var _error = Object.assign({}, state.error, _defineProperty({}, _id, _callToMove));
        var activeErrors = Object.assign({}, state.activeErrors);
        delete _pending2[_id];
 
        // no duplicate errors
        Eif (state.activeErrors[errType].indexOf(_id) === -1) {
          activeErrors[errType] = [_id].concat(state.activeErrors[errType]);
        }
 
        if (action.errorTimeout && state.errorTimeout !== null) {
          clearTimeout(state.errorTimeout);
        }
 
        return Object.assign({}, state, {
          pending: _pending2,
          error: _error,
          errorTimeout: errorTimeout,
          activeErrors: activeErrors
        });
      }
 
    case Actions.INVALIDATE_ERROR:
      {
        var _id2 = action.id,
            _errType = action.errType;
 
        var _error2 = Object.assign({}, state.error);
        var _activeErrors = Object.assign({}, state.activeErrors);
 
        Eif (_error2[_id2] && !_error2[_id2].invalid) {
          _error2[_id2].invalid = true;
        }
 
        Eif (state.errorTimeout !== null) {
          clearTimeout(state.errorTimeout);
        }
 
        _activeErrors[_errType] = state.activeErrors[_errType];
        _activeErrors[_errType].splice(_activeErrors[_errType].indexOf(_id2), 1);
 
        return Object.assign({}, state, {
          error: _error2,
          activeErrors: _activeErrors,
          errorTimeout: null
        });
      }
 
    case Actions.INVALIDATE_ERRORS:
      {
        var ids = action.ids,
            _errType2 = action.errType;
 
        var _error3 = Object.assign({}, state.error);
        var _activeErrors2 = Object.assign({}, state.activeErrors);
 
        if (ids === '*') {
          Object.keys(_error3).forEach(function (key) {
            _error3[key].invalid = true;
          });
          _activeErrors2.application = [];
          _activeErrors2.form = [];
        } else {
          ids.forEach(function (id) {
            Eif (_error3[id]) {
              _error3[id].invalid = true;
            }
          });
          _activeErrors2[_errType2] = [];
        }
 
        Eif (state.errorTimeout !== null) {
          clearTimeout(state.errorTimeout);
        }
 
        return Object.assign({}, state, {
          error: _error3,
          activeErrors: _activeErrors2,
          errorTimeout: null
        });
      }
 
    case Actions.PREPARE_UPLOAD:
      {
        var files = action.files;
        var progress = {};
        Object.keys(files).forEach(function (key, index) {
          progress[files[key].name + '_' + files[key].lastModified] = {
            current: 0,
            total: files[key].size
          };
        });
        return Object.assign({}, state, { progress: progress });
      }
 
    case Actions.RESET_UPLOAD_PROGRESS:
      {
        return Object.assign({}, state, {
          progress: {},
          progressReset: action.val
        });
      }
 
    case Actions.ON_UPLOAD_PROGRESS:
      {
        var _progress = Object.assign({}, state.progress);
        var progressItem = Object.assign({}, _progress[action.progressPacket.id]);
        progressItem.current = action.progressPacket.current;
        _progress[action.progressPacket.id] = progressItem;
        return Object.assign({}, state, { progress: _progress });
      }
 
    default:
      return state;
  }
}