UNPKG

@boomerang-io/utils

Version:

A library of reusable utilities and hooks for React webapps on the Boomerang platform.

45 lines (44 loc) 1.39 kB
"use strict"; 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); */ const createReducer = (initialState, actionHandlers) => { return function reducer() { let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; let 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 = exports.default = createReducer;