PromucFlow_constructor/app/client/src/utils/ReducerUtils.ts
2022-08-04 11:10:44 +05:30

29 lines
782 B
TypeScript

import { ReduxAction } from "@appsmith/constants/ReduxActionConstants";
import produce from "immer";
export const createReducer = (
initialState: any,
handlers: { [type: string]: (state: any, action: any) => any },
) => {
return function reducer(state = initialState, action: ReduxAction<any>) {
if (handlers.hasOwnProperty(action.type)) {
return handlers[action.type](state, action);
} else {
return state;
}
};
};
export const createImmerReducer = (
initialState: any,
handlers: { [type: string]: any },
) => {
return function reducer(state = initialState, action: ReduxAction<any>) {
if (handlers.hasOwnProperty(action.type)) {
return produce(handlers[action.type])(state, action);
} else {
return state;
}
};
};