Created redux test

This commit is contained in:
Alejandro Celaya
2020-08-25 20:23:12 +02:00
parent f04aece7df
commit 6696fb13d6
2 changed files with 66 additions and 5 deletions

View File

@@ -1,17 +1,17 @@
import { Action } from 'redux';
type ActionDispatcher<State, AT> = (currentState: State, action: AT) => State;
type ActionDispatcherMap<State, AT> = Record<string, ActionDispatcher<State, AT>>;
type ActionHandler<State, AT> = (currentState: State, action: AT) => State;
type ActionHandlerMap<State, AT> = Record<string, ActionHandler<State, AT>>;
export const buildReducer = <State, AT extends Action>(map: ActionDispatcherMap<State, AT>, initialState: State) => (
export const buildReducer = <State, AT extends Action>(map: ActionHandlerMap<State, AT>, initialState: State) => (
state: State | undefined,
action: AT,
): State => {
const { type } = action;
const actionDispatcher = map[type];
const actionHandler = map[type];
const currentState = state ?? initialState;
return actionDispatcher ? actionDispatcher(currentState, action) : currentState;
return actionHandler ? actionHandler(currentState, action) : currentState;
};
export const buildActionCreator = <T extends string>(type: T) => (): Action<T> => ({ type });