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

66.67% Statements 28/42
29.17% Branches 7/24
57.14% Functions 4/7
65.79% Lines 25/38
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                                                                                             
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.updateList = updateList;
exports.removeItem = removeItem;
exports.updateItem = updateItem;
exports.updateActive = updateActive;
 
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 = {
  list: [],
  active: null,
  mapById: {}
};
 
function updateList(state, itemList) {
  var idKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '_id';
 
  var list = [];
  var mapById = {};
  itemList.forEach(function (item) {
    list.push(item[idKey]);
    mapById[item[idKey]] = item;
  });
 
  var active = mapById[state.active] ? state.active : null;
  return Object.assign({}, state, { list: list, mapById: mapById, active: active });
}
 
function removeItem(state, id) {
  var mapById = state.mapById;
  if (mapById && mapById[id]) {
    mapById = Object.assign({}, state.mapById);
    delete mapById[id];
  }
  if (state.list.indexOf(id) !== -1) {
    var list = state.list.filter(function (item) {
      return item !== id;
    });
    var active = state.active === id ? null : state.active;
    if (mapById) {
      return Object.assign({}, state, { list: list, active: active, mapById: mapById });
    }
    return Object.assign({}, state, { list: list, active: active });
  }
  return state;
}
 
function updateItem(state, item) {
  var idKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '_id';
 
  var itemId = item[idKey];
  var list = state.list.indexOf(itemId) === -1 ? [itemId].concat(state.list) : state.list;
  var mapById = Object.assign({}, state.mapById, _defineProperty({}, itemId, item));
  return Object.assign({}, state, { list: list, mapById: mapById });
}
 
function updateActive(state, id) {
  return Object.assign({}, state, { active: id });
}
 
exports.default = {
  updateList: updateList,
  removeItem: removeItem,
  updateItem: updateItem,
  initialState: initialState,
  updateActive: updateActive
};