UNPKG

@e-group/redux-modules

Version:

eGroup team react-redux modules that share across projects.

124 lines (97 loc) 4.61 kB
"use strict"; 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 _toolkit = require("@reduxjs/toolkit"); var _warning = _interopRequireDefault(require("warning")); var _immer = _interopRequireDefault(require("immer")); var _setIn = _interopRequireDefault(require("@e-group/utils/setIn")); var _deleteIn = _interopRequireDefault(require("@e-group/utils/deleteIn")); var _hasIn = _interopRequireDefault(require("@e-group/utils/hasIn")); var _utils = require("../utils"); var _types = require("./types"); const initialState = {}; /** * Reducer */ const apis = (0, _toolkit.createReducer)(initialState, { [_types.EG_API_TAKE]: (0, _immer.default)((draft, action) => { (0, _setIn.default)(draft, [...action.payload.leafs, 'isError'], false); }), [_types.EG_API_REQUEST]: (0, _immer.default)((draft, action) => { (0, _setIn.default)(draft, [...action.payload.leafs, 'isLoading'], true); }), [_types.EG_API_CANCEL]: (0, _immer.default)((draft, action) => { (0, _setIn.default)(draft, [...action.payload.leafs, 'isLoading'], false); }), [_types.EG_API_SUCCESS]: (0, _immer.default)((draft, action) => { (0, _setIn.default)(draft, [...action.payload.leafs, 'isLoading'], false); if (typeof action.payload.response !== 'undefined') { (0, _setIn.default)(draft, [...action.payload.leafs, 'response'], action.payload.response); } }), [_types.EG_API_FAILURE]: (0, _immer.default)((draft, action) => { (0, _setIn.default)(draft, [...action.payload.leafs, 'isLoading'], false); (0, _setIn.default)(draft, [...action.payload.leafs, 'isError'], true); if (action.payload.error) { (0, _setIn.default)(draft, [...action.payload.leafs, 'error'], action.payload.error); } }), [_types.EG_CLEAR_API_RESPONSE]: (0, _immer.default)((draft, 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; } 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; } const trimedLeafs = (0, _utils.getTrimedLeafs)(actionType); (0, _deleteIn.default)(draft, [...trimedLeafs, 'response']); }), [_types.EG_CLEAR_APIS_RESPONSE]: (0, _immer.default)((draft, 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; } 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; } actionTypes.push(actionType); } actionTypes.forEach(actionType => { const trimedLeafs = (0, _utils.getTrimedLeafs)(actionType); (0, _deleteIn.default)(draft, [...trimedLeafs, 'response']); }); }), [_types.EG_DESTROY_API]: (0, _immer.default)((draft, 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; } if ((0, _hasIn.default)(draft, action.payload)) { (0, _setIn.default)(draft, action.payload, {}); } }) }); exports.apis = apis;