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

51.85% Statements 42/81
43.48% Branches 20/46
66.67% Functions 4/6
58.21% Lines 39/67
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                                                      71× 71×   71×                                                                                                                                                                                     67×      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.volumeTemplate = exports.initialState = undefined;
exports.default = volumesReducer;
 
var _deepClone = require('mout/src/lang/deepClone');
 
var _deepClone2 = _interopRequireDefault(_deepClone);
 
var _volumes = require('../actions/volumes');
 
var Actions = _interopRequireWildcard(_volumes);
 
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _defineProperty(obj, key, value) { if (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 = {
  list: [],
  active: 0,
  pending: false,
  mapById: {},
  logById: {},
  mapByClusterId: {}
};
 
var volumeTemplate = exports.volumeTemplate = {
  name: 'new volume',
  size: 4,
  profileId: '',
  type: 'ebs',
  cluster: null
};
 
function volumesReducer() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  var action = arguments[1];
 
  switch (action.type) {
    case Actions.ADD_VOLUME:
      {
        var newVol = (0, _deepClone2.default)(volumeTemplate);
        newVol.profileId = action.profileId;
        return Object.assign({}, state, {
          list: [].concat(state.list, newVol),
          active: state.list.length
        });
      }
    case Actions.UPDATE_ACTIVE_VOLUME:
      {
        return Object.assign({}, state, { active: action.active });
      }
    case Actions.REMOVE_VOLUME:
      {
        var list = state.list.filter(function (item, idx) {
          return idx !== action.index;
        });
        var active = state.active < list.length ? state.active : list.length - 1;
 
        var newState = Object.assign({}, state, { list: list, active: active });
        return newState;
      }
    case Actions.UPDATE_VOLUMES:
      {
        var _list = action.volumes;
        var _active = state.active < _list.length ? state.active : _list.length - 1;
        var mapById = {};
        var mapByClusterId = {};
        _list.forEach(function (vol) {
          Eif (vol._id) {
            mapById[vol._id] = Object.assign({}, volumeTemplate, vol);
            Iif (vol.clusterId) {
              mapByClusterId[vol.clusterId] = vol._id;
            }
          }
        });
        return Object.assign({}, state, {
          list: _list,
          active: _active,
          mapById: mapById,
          mapByClusterId: mapByClusterId
        });
      }
    // unused
    // case Actions.UPDATE_VOLUME: {
    //   console.log('todo');
    //   return state;
    // }
    case Actions.UPDATE_VOLUME_STATUS:
      {
        var volumeId = action.volumeId,
            status = action.status;
 
        var _list2 = [].concat(state.list);
        var _mapById = Object.assign({}, state.mapById);
        var volume = Object.assign({}, state.mapById[volumeId]);
        volume.status = status;
        _mapById[volumeId] = volume;
        for (var i = 0; i < _list2.length; i++) {
          if (_list2[i]._id === volumeId) {
            _list2[i] = volume;
            break;
          }
        }
        return Object.assign({}, state, { list: _list2, mapById: _mapById });
      }
    case Actions.SAVE_VOLUME:
      {
        var index = action.index,
            _volume = action.volume;
 
 
        var _list3 = [].concat(state.list.slice(0, index), _volume, state.list.slice(index + 1));
        var _active2 = state.active < _list3.length ? state.active : _list3.length - 1;
 
        if (_volume._id) {
          var _mapById2 = Object.assign({}, state.mapById, _defineProperty({}, _volume._id, _volume));
          return Object.assign({}, state, { list: _list3, active: _active2, mapById: _mapById2 });
        }
 
        return Object.assign({}, state, { list: _list3, active: _active2 });
      }
 
    case Actions.APPEND_TO_VOLUME_LOG:
      {
        var logById = Object.assign({}, state.logById);
        var volumeLog = [].concat(state.logById[action.id]);
        Iif (!volumeLog) {
          volumeLog = [];
        }
        Iif (Array.isArray(action.logEntry)) {
          volumeLog = volumeLog.concat(action.logEntry);
        } else {
          volumeLog.push(action.logEntry);
        }
        logById[action.id] = volumeLog;
        return Object.assign({}, state, { logById: logById });
      }
 
    case Actions.UPDATE_VOLUME_LOG:
      {
        var _logById = Object.assign({}, state.logById);
        _logById[action.id] = action.log;
        return Object.assign({}, state, { logById: _logById });
      }
 
    case Actions.PENDING_VOLUME_NETWORK:
      {
        return Object.assign({}, state, { pending: action.pending });
      }
    default:
      return state;
  }
}