UNPKG

@e-group/redux-modules

Version:

eGroup team react-redux modules that share across projects.

104 lines (86 loc) 3.92 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import { handleActions } from 'redux-actions'; import { fromJS, Map } from '@e-group/immutable'; import warning from 'warning'; import { supportedTypes, getTrimedLeafs } from '../../utils'; import { EG_API_TAKE, EG_API_REQUEST, EG_API_CANCEL, EG_API_SUCCESS, EG_API_FAILURE, EG_CLEAR_API_RESPONSE, EG_CLEAR_APIS_RESPONSE, EG_DESTROY_API } from '../../apis'; const initialState = fromJS({}); /** * Reducer */ export const apis = handleActions({ [EG_API_TAKE]: (state, action) => state.setIn([...action.payload.leafs, 'isError'], false), [EG_API_REQUEST]: (state, action) => state.setIn([...action.payload.leafs, 'isLoading'], true), [EG_API_CANCEL]: (state, action) => state.setIn([...action.payload.leafs, 'isLoading'], false), [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; }, [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; }, [EG_CLEAR_API_RESPONSE]: (state, action) => { const _supportedTypes = supportedTypes(action.payload, ['object']), _supportedTypes2 = _slicedToArray(_supportedTypes, 2), isSupported = _supportedTypes2[0], type = _supportedTypes2[1]; if (!isSupported) { warning(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') { warning(false, "[@e-group/redux-modules] ERROR: Redux action type need to be \"string\" not \"".concat(typeof actionType, "\"")); return state; } const trimedLeafs = getTrimedLeafs(actionType); return state.deleteIn([...trimedLeafs, 'response']); }, [EG_CLEAR_APIS_RESPONSE]: (state, action) => { const _supportedTypes3 = supportedTypes(action.payload, ['array']), _supportedTypes4 = _slicedToArray(_supportedTypes3, 2), isSupported = _supportedTypes4[0], type = _supportedTypes4[1]; if (!isSupported) { warning(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') { warning(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 = getTrimedLeafs(actionType); nextState = nextState.deleteIn([...trimedLeafs, 'response']); }); return nextState; }, [EG_DESTROY_API]: (state, action) => { const _supportedTypes5 = supportedTypes(action.payload, ['array']), _supportedTypes6 = _slicedToArray(_supportedTypes5, 2), isSupported = _supportedTypes6[0], type = _supportedTypes6[1]; if (!isSupported) { warning(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, Map()); } return state; } }, initialState);