@e-group/redux-modules
Version:
eGroup team react-redux modules that share across projects.
120 lines (93 loc) • 4.29 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.apis = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _reduxActions = require("redux-actions");
var _immutable = require("@e-group/immutable");
var _warning = _interopRequireDefault(require("warning"));
var _utils = require("../../utils");
var _apis = require("../../apis");
const initialState = (0, _immutable.fromJS)({});
/**
* Reducer
*/
const apis = (0, _reduxActions.handleActions)({
[_apis.EG_API_TAKE]: (state, action) => state.setIn([...action.payload.leafs, 'isError'], false),
[_apis.EG_API_REQUEST]: (state, action) => state.setIn([...action.payload.leafs, 'isLoading'], true),
[_apis.EG_API_CANCEL]: (state, action) => state.setIn([...action.payload.leafs, 'isLoading'], false),
[_apis.EG_API_SUCCESS]: (state, action) => {
let newState = state.setIn([...action.payload.leafs, 'isLoading'], false);
if (typeof action.payload.response !== 'undefined') {
newState = newState.setIn([...action.payload.leafs, 'response'], action.payload.response);
}
return newState;
},
[_apis.EG_API_FAILURE]: (state, action) => {
let newState = state.setIn([...action.payload.leafs, 'isLoading'], false);
newState = newState.setIn([...action.payload.leafs, 'isError'], true);
if (action.payload.error) {
newState = newState.setIn([...action.payload.leafs, 'error'], action.payload.error);
}
return newState;
},
[_apis.EG_CLEAR_API_RESPONSE]: (state, action) => {
const _supportedTypes = (0, _utils.supportedTypes)(action.payload, ['object']),
_supportedTypes2 = (0, _slicedToArray2.default)(_supportedTypes, 2),
isSupported = _supportedTypes2[0],
type = _supportedTypes2[1];
if (!isSupported) {
(0, _warning.default)(false, "[@e-group/redux-modules] ERROR: Action \"clearApiResponse\" is not supported \"".concat(type, "\" payload."));
return state;
}
const actionType = action.payload.type;
if (typeof actionType !== 'string') {
(0, _warning.default)(false, "[@e-group/redux-modules] ERROR: Redux action type need to be \"string\" not \"".concat(typeof actionType, "\""));
return state;
}
const trimedLeafs = (0, _utils.getTrimedLeafs)(actionType);
return state.deleteIn([...trimedLeafs, 'response']);
},
[_apis.EG_CLEAR_APIS_RESPONSE]: (state, action) => {
const _supportedTypes3 = (0, _utils.supportedTypes)(action.payload, ['array']),
_supportedTypes4 = (0, _slicedToArray2.default)(_supportedTypes3, 2),
isSupported = _supportedTypes4[0],
type = _supportedTypes4[1];
if (!isSupported) {
(0, _warning.default)(false, "[@e-group/redux-modules] ERROR: Action \"clearApisResponse\" is not supported ".concat(type, " payload."));
return state;
}
const actionTypes = [];
for (let i = 0; i < action.payload.length; i++) {
const actionType = action.payload[i].type;
if (typeof actionType !== 'string') {
(0, _warning.default)(false, "[@e-group/redux-modules] ERROR: Redux action type need to be \"string\" not \"".concat(typeof actionType, "\""));
return state;
}
actionTypes.push(actionType);
}
let nextState = state;
actionTypes.forEach(actionType => {
const trimedLeafs = (0, _utils.getTrimedLeafs)(actionType);
nextState = nextState.deleteIn([...trimedLeafs, 'response']);
});
return nextState;
},
[_apis.EG_DESTROY_API]: (state, action) => {
const _supportedTypes5 = (0, _utils.supportedTypes)(action.payload, ['array']),
_supportedTypes6 = (0, _slicedToArray2.default)(_supportedTypes5, 2),
isSupported = _supportedTypes6[0],
type = _supportedTypes6[1];
if (!isSupported) {
(0, _warning.default)(false, "[@e-group/redux-modules] ERROR: Action \"destroyApi\" is not supported ".concat(type, " payload."));
return state;
}
if (state.hasIn(action.payload)) {
return state.setIn(action.payload, (0, _immutable.Map)());
}
return state;
}
}, initialState);
exports.apis = apis;