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

50% Statements 30/60
40% Branches 16/40
57.14% Functions 4/7
58.7% Lines 27/46
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                                                  71× 71×   71×                                                                                                                       67×      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.awsTemplate = exports.initialState = undefined;
exports.default = awsReducer;
 
var _deepClone = require('mout/src/lang/deepClone');
 
var _deepClone2 = _interopRequireDefault(_deepClone);
 
var _aws = require('../actions/aws');
 
var Actions = _interopRequireWildcard(_aws);
 
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: {}
};
 
var awsTemplate = exports.awsTemplate = {
  accessKeyId: '',
  availabilityZone: 'us-east-1a',
  name: 'new AWS profile',
  regionName: 'us-east-1',
  secretAccessKey: ''
};
 
function awsReducer() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  var action = arguments[1];
 
  switch (action.type) {
    case Actions.ADD_AWS_PROFILE:
      {
        return Object.assign({}, state, {
          list: [].concat(state.list, (0, _deepClone2.default)(awsTemplate)),
          active: state.list.length
        });
      }
 
    case Actions.REMOVE_AWS_PROFILE:
      {
        var list = state.list.filter(function (item, idx) {
          return idx !== action.index;
        });
        var aws = state.list.filter(function (item, idx) {
          return idx === action.index;
        })[0];
        var active = state.active < list.length ? state.active : list.length - 1;
        var newState = Object.assign({}, state, { list: list, active: active });
 
        if (aws && aws._id && state.mapById[aws._id]) {
          var mapById = Object.assign({}, state.mapById);
          delete mapById[aws._id];
          return Object.assign(newState, { mapById: mapById });
        }
        return newState;
      }
 
    case Actions.UPDATE_ACTIVE_AWS_PROFILE:
      {
        var _active = Math.min(Math.max(action.index, 0), state.list.length - 1);
        return Object.assign({}, state, { active: _active });
      }
 
    case Actions.UPDATE_AWS_PROFILES:
      {
        var _list = action.profiles;
        var _active2 = state.active < _list.length ? state.active : _list.length - 1;
        var _mapById = {};
        _list.forEach(function (aws) {
          Eif (aws._id) {
            _mapById[aws._id] = aws;
          }
        });
        return Object.assign({}, state, { list: _list, active: _active2, mapById: _mapById });
      }
 
    case Actions.SAVE_AWS_PROFILE:
      {
        var index = action.index,
            profile = action.profile;
 
 
        var _list2 = [].concat(state.list.slice(0, index), profile, state.list.slice(index + 1));
        var _active3 = state.active < _list2.length ? state.active : _list2.length - 1;
 
        if (profile._id) {
          var _mapById2 = Object.assign({}, state.mapById, _defineProperty({}, profile._id, profile));
          return Object.assign({}, state, { list: _list2, active: _active3, mapById: _mapById2 });
        }
 
        return Object.assign({}, state, { list: _list2, active: _active3 });
      }
 
    case Actions.PENDING_AWS_NETWORK:
      {
        return Object.assign({}, state, { pending: action.pending });
      }
 
    default:
      return state;
  }
}