@boomerang-io/utils
Version:
A library of reusable utilities and hooks for React webapps on the Boomerang platform.
49 lines (45 loc) • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
/**
* Reducer boilerplate to create reducers
*
* @param {Obj} initialState - intial state of reducer
* @param {Obj} actionHandlers - action types and what they do to state
* @example
* const initialState = {
* fetchingState: "",
* data: []
* }
* const actionHandlers = {
* APPS_OVERVIEW_RESET: () => {
* return { ...initialState };
* },
* FETCH_APPS_OVERVIEW_REQUEST: state => {
* return { ...state, fetchingState: "fetching" };
* },
* FETCH_APPS_OVERVIEW_SUCCESS: (state, action) => {
* return { ...state, data: action.data, fetchingState: "success" };
* },
* FETCH_APPS_OVERVIEW_FAILURE: state => {
* return { ...state, data: [], fetchingState: "failure" };
* }
* };
*
* export default createReducer(initialState, actionHandlers);
*/
var createReducer = function createReducer(initialState, actionHandlers) {
return function reducer() {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
var action = arguments.length > 1 ? arguments[1] : undefined;
if (Boolean(actionHandlers[action.type]) && typeof actionHandlers[action.type] === "function") {
return actionHandlers[action.type](state, action);
} else {
return state;
}
};
};
var _default = createReducer;
exports.default = _default;