@e-group/redux-modules
Version:
eGroup team react-redux modules that share across projects.
76 lines (65 loc) • 2.79 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { createReducer } from '@reduxjs/toolkit';
import produce from 'immer';
import warning from 'warning';
import assign from 'lodash.assign';
import mergeWith from 'lodash.mergewith';
import getIn from '@e-group/utils/getIn';
import setIn from '@e-group/utils/setIn';
import { SET_ENTITIES, SET_ENTITIES_SHALLOW } from './types';
import merger from './merger';
import { supportedTypes } from '../utils';
const initialState = {};
/**
* Reducer
*/
export const entities = createReducer(initialState, {
[SET_ENTITIES]: produce((draft, action) => {
if (!action.payload) return;
if (action.meta) {
const _supportedTypes = supportedTypes(action.meta.path, ['array']),
_supportedTypes2 = _slicedToArray(_supportedTypes, 2),
isSupported = _supportedTypes2[0],
type = _supportedTypes2[1];
if (!isSupported) {
warning(false, "[@e-group/redux-modules] ERROR: Action \"setEntities\" is not supported \"".concat(type, "\" payload."));
return;
}
setIn(draft, action.meta.path, mergeWith(getIn(draft, action.meta.path), action.payload, merger));
} else {
const _supportedTypes3 = supportedTypes(action.payload, ['object']),
_supportedTypes4 = _slicedToArray(_supportedTypes3, 2),
isSupported = _supportedTypes4[0],
type = _supportedTypes4[1];
if (!isSupported) {
warning(false, "[@e-group/redux-modules] ERROR: Action \"setEntities\" is not supported \"".concat(type, "\" payload."));
return;
}
mergeWith(draft, action.payload, merger);
}
}),
[SET_ENTITIES_SHALLOW]: produce((draft, action) => {
if (!action.payload) return;
if (action.meta) {
const _supportedTypes5 = supportedTypes(action.meta.path, ['array']),
_supportedTypes6 = _slicedToArray(_supportedTypes5, 2),
isSupported = _supportedTypes6[0],
type = _supportedTypes6[1];
if (!isSupported) {
warning(false, "[@e-group/redux-modules] ERROR: Action \"setEntitiesShallow\" is not supported \"".concat(type, "\" payload."));
return;
}
setIn(draft, action.meta.path, assign(getIn(draft, action.meta.path), action.payload));
} else {
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 \"setEntities\" is not supported \"".concat(type, "\" payload."));
return;
}
assign(draft, action.payload);
}
})
});