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

49.25% Statements 33/67
38.1% Branches 16/42
50% Functions 3/6
51.85% Lines 28/54
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    1×     1× 1×   1×   1×   1×   1×   1×               1×           1× 70× 70×   70×                               1× 1× 1×               2×       2× 2×   2× 1× 1×     1×                                                                         1×         1× 1× 1×                 65×      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.groupTemplate = exports.initialState = undefined;
exports.default = groupReducer;
 
var _groups = require('../actions/groups');
 
var Actions = _interopRequireWildcard(_groups);
 
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 = {
  list: [],
  active: 0,
  pending: false,
  mapById: {},
  usersByGroup: {}
};
 
var groupTemplate = exports.groupTemplate = {
  name: '',
  description: '',
  public: true
};
 
function groupReducer() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  var action = arguments[1];
 
  switch (action.type) {
    case Actions.GET_GROUPS:
      {
        var list = action.groups;
        var active = state.active < list.length ? state.active : list.length - 1;
        var mapById = Object.assign({}, state.mapById);
        list.forEach(function (group) {
          if (group._id && !mapById[group._id]) {
            mapById[group._id] = group;
          }
        });
        return Object.assign({}, state, { list: list, active: active, mapById: mapById });
      }
 
    case Actions.ADD_GROUP:
      {
        var newGroup = Object.assign({}, groupTemplate);
        newGroup.name = 'new group ' + state.list.length;
        return Object.assign({}, state, {
          list: [].concat(state.list, newGroup),
          active: state.list.length
        });
      }
 
    case Actions.SAVE_GROUP:
      {
        var index = action.index,
            group = action.group;
 
 
        var _list = [].concat(state.list.slice(0, index), group, state.list.slice(index + 1));
        var _active = state.active < _list.length ? state.active : _list.length - 1;
 
        if (group._id) {
          var _mapById = Object.assign({}, state.mapById, _defineProperty({}, group._id, group));
          return Object.assign({}, state, { list: _list, active: _active, mapById: _mapById });
        }
 
        return Object.assign({}, state, { list: _list, active: _active });
      }
 
    case Actions.REMOVE_GROUP:
      {
        var _list2 = state.list.filter(function (item, idx) {
          return idx !== action.index;
        });
        var _group = state.list.filter(function (item, idx) {
          return idx === action.index;
        })[0];
        var _active2 = state.active < _list2.length ? state.active : _list2.length - 1;
        var newState = Object.assign({}, state, { list: _list2, active: _active2 });
 
        if (state.usersByGroup[_group.id]) {
          var usersByGroup = Object.assign({}, state.usersByGroup);
          delete usersByGroup[_group.id];
          newState.usersByGroup = usersByGroup;
        }
 
        if (_group && _group._id && state.mapById[_group._id]) {
          var _mapById2 = Object.assign({}, state.mapById);
          delete _mapById2[_group._id];
          return Object.assign(newState, { mapById: _mapById2 });
        }
        return newState;
      }
 
    case Actions.UPDATE_USER_LIST:
      {
        var _usersByGroup = Object.assign({}, state.usersByGroup);
        _usersByGroup[action.id] = action.users;
        return Object.assign({}, state, { usersByGroup: _usersByGroup });
      }
 
    case Actions.UPDATE_ACTIVE_GROUP:
      {
        return Object.assign({}, state, { active: action.index });
      }
 
    case Actions.LIST_USERS:
      {
        var newUsers = Object.assign({}, state.usersByGroup);
        newUsers[action.groupId] = action.users;
        return Object.assign({}, state, { usersByGroup: newUsers });
      }
 
    case Actions.PENDING_GROUP_NETWORK:
      {
        return Object.assign({}, state, { pending: action.pending });
      }
 
    default:
      return state;
  }
}