@e-group/redux-modules
Version:
eGroup team react-redux modules that share across projects.
93 lines (77 loc) • 3.22 kB
JavaScript
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_SNACKBAR, OPEN_SNACKBAR, CLOSE_SNACKBAR, SET_SNACKBAR_DATA } from './types';
const initialState = {};
/**
* Reducer
*/
export const snackbars = createReducer(initialState, {
[INITIALIZE_SNACKBAR]: 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 \"initializeSnackbar\" is not supported \"".concat(type, "\" payload."));
return;
}
setIn(draft, [action.payload, 'isOpen'], false);
}),
[OPEN_SNACKBAR]: 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 \"openSnackbar\" is not supported \"".concat(type, "\" payload."));
return;
}
const name = action.payload;
if (hasIn(draft, [name])) {
setIn(draft, [name, 'isOpen'], true);
}
}),
[CLOSE_SNACKBAR]: 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 \"closeSnackbar\" is not supported \"".concat(type, "\" payload."));
return;
}
const name = action.payload;
if (hasIn(draft, [name])) {
setIn(draft, [name, 'isOpen'], false);
}
}),
[SET_SNACKBAR_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 \"setSnackbarData\" 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: Snackbar \"".concat(name, "\" is not exist."));
}
})
});