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

80.95% Statements 68/84
60.71% Branches 17/28
78.95% Functions 15/19
87.84% Lines 65/74
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                                      10×   10×                                                                                
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SAVE_AWS_PROFILE = exports.REMOVE_AWS_PROFILE = exports.PENDING_AWS_NETWORK = exports.UPDATE_ACTIVE_AWS_PROFILE = exports.UPDATE_AWS_PROFILES = exports.ADD_AWS_PROFILE = undefined;
exports.addAWSProfile = addAWSProfile;
exports.updateActiveProfile = updateActiveProfile;
exports.pendingNetworkCall = pendingNetworkCall;
exports.updateAWSProfiles = updateAWSProfiles;
exports.fetchAWSProfiles = fetchAWSProfiles;
exports.removeAWSProfile = removeAWSProfile;
exports.updateAWSProfile = updateAWSProfile;
 
var _network = require('../../network');
 
var _network2 = _interopRequireDefault(_network);
 
var _network3 = require('./network');
 
var netActions = _interopRequireWildcard(_network3);
 
var _ = require('..');
 
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 }; }
 
var ADD_AWS_PROFILE = exports.ADD_AWS_PROFILE = 'ADD_AWS_PROFILE';
var UPDATE_AWS_PROFILES = exports.UPDATE_AWS_PROFILES = 'UPDATE_AWS_PROFILES';
var UPDATE_ACTIVE_AWS_PROFILE = exports.UPDATE_ACTIVE_AWS_PROFILE = 'UPDATE_ACTIVE_AWS_PROFILE';
var PENDING_AWS_NETWORK = exports.PENDING_AWS_NETWORK = 'PENDING_AWS_NETWORK';
var REMOVE_AWS_PROFILE = exports.REMOVE_AWS_PROFILE = 'REMOVE_AWS_PROFILE';
var SAVE_AWS_PROFILE = exports.SAVE_AWS_PROFILE = 'SAVE_AWS_PROFILE';
 
/* eslint-disable no-shadow */
 
function addAWSProfile() {
  return { type: ADD_AWS_PROFILE };
}
 
function updateActiveProfile(index) {
  return { type: UPDATE_ACTIVE_AWS_PROFILE, index: index };
}
 
function pendingNetworkCall() {
  var pending = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
 
  return { type: PENDING_AWS_NETWORK, pending: pending };
}
 
function updateAWSProfiles(profiles) {
  return { type: UPDATE_AWS_PROFILES, profiles: profiles };
}
 
function fetchAWSProfiles() {
  Iif (_.store.getState().preferences.aws.pending) {
    return { type: 'NOOP' };
  }
  return function (dispatch) {
    var action = netActions.addNetworkCall('fetch_aws_profiles', 'Retreive AWS profiles');
    dispatch(pendingNetworkCall(true));
    _network2.default.listAWSProfiles().then(function (resp) {
      dispatch(netActions.successNetworkCall(action.id, resp));
      dispatch(updateAWSProfiles(resp.data));
      dispatch(pendingNetworkCall(false));
    }).catch(function (error) {
      dispatch(netActions.errorNetworkCall(action.id, error));
      dispatch(pendingNetworkCall(false));
    });
 
    return action;
  };
}
 
function removeAWSProfile(index, profile) {
  if (!profile._id) {
    return { type: REMOVE_AWS_PROFILE, index: index };
  }
 
  return function (dispatch) {
    var action = netActions.addNetworkCall('remove_aws_profile', 'Remove cluster');
 
    dispatch(pendingNetworkCall(true));
    _network2.default.deleteAWSProfile(profile._id).then(function (resp) {
      dispatch(netActions.successNetworkCall(action.id, resp));
      dispatch(pendingNetworkCall(false));
      dispatch(fetchAWSProfiles());
    }, function (err) {
      dispatch(netActions.errorNetworkCall(action.id, err, 'form'));
      dispatch(pendingNetworkCall(false));
    });
 
    return action;
  };
}
 
function updateAWSProfile(index, profile) {
  var pushToServer = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
 
  if (!pushToServer) {
    return { type: SAVE_AWS_PROFILE, index: index, profile: profile };
  }
  return function (dispatch) {
    var action = netActions.addNetworkCall('save_aws_profile', 'Save cluster');
    dispatch(pendingNetworkCall(true));
    _network2.default.createAWSProfile(profile).then(function (resp) {
      dispatch(pendingNetworkCall(false));
      dispatch(netActions.successNetworkCall(action.id, resp));
      dispatch(fetchAWSProfiles());
    }, function (err) {
      dispatch(netActions.errorNetworkCall(action.id, err, 'form'));
      dispatch(pendingNetworkCall(false));
    });
    return action;
  };
}
 
// Auto trigger actions on authentication change...
_network2.default.onAuthChange(function (authenticated) {
  if (!authenticated) {
    (0, _.dispatch)(updateAWSProfiles([]));
  }
});