UNPKG

w3-redux

Version:

redux and redux thunk tools

42 lines (41 loc) 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useReducer = void 0; var react_redux_1 = require("react-redux"); var useReducer = function (reducerCategory, reducerName) { var dispatch = (0, react_redux_1.useDispatch)(); var state = (0, react_redux_1.useSelector)(function (store) { var _a, _b; return (_b = (_a = store === null || store === void 0 ? void 0 : store[reducerCategory]) === null || _a === void 0 ? void 0 : _a[reducerName]) !== null && _b !== void 0 ? _b : null; }); return { dispatch: dispatch, types: { set: "".concat(reducerCategory.toUpperCase(), "/").concat(reducerName.toUpperCase(), "/SET"), clear: "".concat(reducerCategory.toUpperCase(), "/").concat(reducerName.toUpperCase(), "/CLEAR"), custom: "".concat(reducerCategory.toUpperCase(), "/").concat(reducerName.toUpperCase(), "/CUSTOM"), }, state: state, setState: function (values, scope) { if (values === void 0) { values = {}; } if (scope === void 0) { scope = ""; } return dispatch({ type: "".concat(reducerCategory.toUpperCase(), "/").concat(reducerName.toUpperCase(), "/SET"), payload: { values: values, scope: scope }, }); }, clearState: function (values, scope) { if (values === void 0) { values = {}; } if (scope === void 0) { scope = ""; } return dispatch({ type: "".concat(reducerCategory.toUpperCase(), "/").concat(reducerName.toUpperCase(), "/CLEAR"), payload: { values: values, scope: scope }, }); }, customDispatch: function (payload) { if (payload === void 0) { payload = {}; } return dispatch({ type: "".concat(reducerCategory.toUpperCase(), "/").concat(reducerName.toUpperCase(), "/CUSTOM"), payload: payload, }); }, }; }; exports.useReducer = useReducer;