UNPKG

@e-group/redux-modules

Version:

eGroup team react-redux modules that share across projects.

93 lines (77 loc) 3.19 kB
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import { createReducer } from '@reduxjs/toolkit'; import produce from 'immer'; import merge from 'lodash.merge'; import warning from 'warning'; import setIn from '@e-group/utils/setIn'; import hasIn from '@e-group/utils/hasIn'; import { supportedTypes } from '../utils'; import { INITIALIZE_DIALOG, OPEN_DIALOG, CLOSE_DIALOG, SET_DIALOG_DATA } from './types'; const initialState = {}; /** * Reducer */ export const dialogs = createReducer(initialState, { [INITIALIZE_DIALOG]: produce((draft, action) => { if (!action.payload) return; const _supportedTypes = supportedTypes(action.payload, ['string']), _supportedTypes2 = _slicedToArray(_supportedTypes, 2), isSupported = _supportedTypes2[0], type = _supportedTypes2[1]; if (!isSupported) { warning(false, "[@e-group/redux-modules] ERROR: Action \"initializeDialog\" is not supported \"".concat(type, "\" payload.")); return; } setIn(draft, [action.payload, 'isOpen'], false); }), [OPEN_DIALOG]: produce((draft, action) => { if (!action.payload) return; const _supportedTypes3 = supportedTypes(action.payload, ['string']), _supportedTypes4 = _slicedToArray(_supportedTypes3, 2), isSupported = _supportedTypes4[0], type = _supportedTypes4[1]; if (!isSupported) { warning(false, "[@e-group/redux-modules] ERROR: Action \"openDialog\" is not supported \"".concat(type, "\" payload.")); return; } const name = action.payload; if (hasIn(draft, [name])) { setIn(draft, [name, 'isOpen'], true); } }), [CLOSE_DIALOG]: produce((draft, action) => { if (!action.payload) return; const _supportedTypes5 = supportedTypes(action.payload, ['string']), _supportedTypes6 = _slicedToArray(_supportedTypes5, 2), isSupported = _supportedTypes6[0], type = _supportedTypes6[1]; if (!isSupported) { warning(false, "[@e-group/redux-modules] ERROR: Action \"closeDialog\" is not supported \"".concat(type, "\" payload.")); return; } const name = action.payload; if (hasIn(draft, [name])) { setIn(draft, [name, 'isOpen'], false); } }), [SET_DIALOG_DATA]: produce((draft, action) => { if (!action.payload) return; const _supportedTypes7 = supportedTypes(action.payload, ['object']), _supportedTypes8 = _slicedToArray(_supportedTypes7, 2), isSupported = _supportedTypes8[0], type = _supportedTypes8[1]; if (!isSupported) { warning(false, "[@e-group/redux-modules] ERROR: Action \"setDialogData\" is not supported \"".concat(type, "\" payload.")); return; } const _action$payload = action.payload, name = _action$payload.name, other = _objectWithoutProperties(_action$payload, ["name"]); if (hasIn(draft, [name])) { merge(draft[name], other); } else { warning(false, "[@e-group/redux-modules] ERROR: Dialog \"".concat(name, "\" is not exist.")); } }) });