@e-group/redux-modules
Version:
eGroup team react-redux modules that share across projects.
113 lines (84 loc) • 3.93 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.snackbars = void 0;
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _toolkit = require("@reduxjs/toolkit");
var _immer = _interopRequireDefault(require("immer"));
var _lodash = _interopRequireDefault(require("lodash.merge"));
var _warning = _interopRequireDefault(require("warning"));
var _setIn = _interopRequireDefault(require("@e-group/utils/setIn"));
var _hasIn = _interopRequireDefault(require("@e-group/utils/hasIn"));
var _utils = require("../utils");
var _types = require("./types");
const initialState = {};
/**
* Reducer
*/
const snackbars = (0, _toolkit.createReducer)(initialState, {
[_types.INITIALIZE_SNACKBAR]: (0, _immer.default)((draft, action) => {
if (!action.payload) return;
const _supportedTypes = (0, _utils.supportedTypes)(action.payload, ['string']),
_supportedTypes2 = (0, _slicedToArray2.default)(_supportedTypes, 2),
isSupported = _supportedTypes2[0],
type = _supportedTypes2[1];
if (!isSupported) {
(0, _warning.default)(false, "[@e-group/redux-modules] ERROR: Action \"initializeSnackbar\" is not supported \"".concat(type, "\" payload."));
return;
}
(0, _setIn.default)(draft, [action.payload, 'isOpen'], false);
}),
[_types.OPEN_SNACKBAR]: (0, _immer.default)((draft, action) => {
if (!action.payload) return;
const _supportedTypes3 = (0, _utils.supportedTypes)(action.payload, ['string']),
_supportedTypes4 = (0, _slicedToArray2.default)(_supportedTypes3, 2),
isSupported = _supportedTypes4[0],
type = _supportedTypes4[1];
if (!isSupported) {
(0, _warning.default)(false, "[@e-group/redux-modules] ERROR: Action \"openSnackbar\" is not supported \"".concat(type, "\" payload."));
return;
}
const name = action.payload;
if ((0, _hasIn.default)(draft, [name])) {
(0, _setIn.default)(draft, [name, 'isOpen'], true);
}
}),
[_types.CLOSE_SNACKBAR]: (0, _immer.default)((draft, action) => {
if (!action.payload) return;
const _supportedTypes5 = (0, _utils.supportedTypes)(action.payload, ['string']),
_supportedTypes6 = (0, _slicedToArray2.default)(_supportedTypes5, 2),
isSupported = _supportedTypes6[0],
type = _supportedTypes6[1];
if (!isSupported) {
(0, _warning.default)(false, "[@e-group/redux-modules] ERROR: Action \"closeSnackbar\" is not supported \"".concat(type, "\" payload."));
return;
}
const name = action.payload;
if ((0, _hasIn.default)(draft, [name])) {
(0, _setIn.default)(draft, [name, 'isOpen'], false);
}
}),
[_types.SET_SNACKBAR_DATA]: (0, _immer.default)((draft, action) => {
if (!action.payload) return;
const _supportedTypes7 = (0, _utils.supportedTypes)(action.payload, ['object']),
_supportedTypes8 = (0, _slicedToArray2.default)(_supportedTypes7, 2),
isSupported = _supportedTypes8[0],
type = _supportedTypes8[1];
if (!isSupported) {
(0, _warning.default)(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 = (0, _objectWithoutProperties2.default)(_action$payload, ["name"]);
if ((0, _hasIn.default)(draft, [name])) {
(0, _lodash.default)(draft[name], other);
} else {
(0, _warning.default)(false, "[@e-group/redux-modules] ERROR: Snackbar \"".concat(name, "\" is not exist."));
}
})
});
exports.snackbars = snackbars;