redux-code
Version:
Redux helpers for actions and reducers
34 lines (33 loc) • 922 B
JavaScript
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Actions Mixin provides reset and update actions
*/
export const commonActions = {
/**
*
* Creates an action to reset the state
*/
reset: undefined,
/**
*
* Creates an action to update the state with the given payload
*/
update: (payload) => payload,
}
/**
*
* Creates a mixin for a reducer that adds the ability to reset and update the state
* @param {actions} actions The actions to handle.
* @param {initial} initial The initial state.
*/
export const commonReducer = (actions, initial) => ({
[actions.reset.type]: () => initial,
[actions.update.type]: (state, { payload }) => Object.assign(Object.assign({}, state), payload),
})
/**
* Initial reducer.
* Supports INIT action
*/
export const initReducer = (actions) => ({
[actions.init.type]: (state) => Object.assign(Object.assign({}, state), { inited: true }),
})