Last active
June 13, 2019 16:28
-
-
Save captain-yossarian/07e955663a8fcb702cb76f14bc04f88d to your computer and use it in GitHub Desktop.
Reducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ActionPayload, AllActionsPayload, Constants, IActionMap } from './actionFactory'; | |
type ReducerCase<P> = (state: IStore, payload: P) => IStore; | |
type IReducer = { [P in keyof Record<Constants, ReducerCase<AllActionsPayload>>]: ReducerCase<IActionMap[P]> }; | |
export const reducer: IReducer = { | |
ACTION_NAMESPACE: (state, payload) => { | |
const { id, mode } = payload; | |
return state | |
}, | |
}; | |
export const communicatorReducer = (reducers: Record<Constants, ReducerCase<AllActionsPayload>>) => ( | |
state = initialState, | |
{ type, payload }: ActionPayload<Constants, AllActionsPayload> | |
): IStore => (reducers[type] ? reducers[type](state, payload) : state); | |
export default communicatorReducer(reducer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment