mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-02-27 12:16:36 +00:00
Removed dependency on redux-actions for all reducers already migrated to typescript
This commit is contained in:
17
src/utils/helpers/redux.ts
Normal file
17
src/utils/helpers/redux.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Action } from 'redux';
|
||||
|
||||
type ActionDispatcher<State, AT> = (currentState: State, action: AT) => State;
|
||||
type ActionDispatcherMap<State, AT> = Record<string, ActionDispatcher<State, AT>>;
|
||||
|
||||
export const buildReducer = <State, AT extends Action>(map: ActionDispatcherMap<State, AT>, initialState: State) => (
|
||||
state: State | undefined,
|
||||
action: AT,
|
||||
): State => {
|
||||
const { type } = action;
|
||||
const actionDispatcher = map[type];
|
||||
const currentState = state ?? initialState;
|
||||
|
||||
return actionDispatcher ? actionDispatcher(currentState, action) : currentState;
|
||||
};
|
||||
|
||||
export const buildActionCreator = <T extends string>(type: T) => (): Action<T> => ({ type });
|
||||
Reference in New Issue
Block a user