import { update } from 'lodash' import { Action, createAction } from 'redux-actions' import { Reducer } from '../types/reducer' import { ActionCreator } from '../types/actionCreator' export interface IReducerAndAction
{
reducer: Reducer<{}>
action: ActionCreator
}
export const makeScope =
(namespacing: string[], excludeField?: boolean) =>
=> {
const reducer = (state: {}, action: Action ) => {
const keyPath = excludeField
? namespacing.slice(1, -1)
: namespacing.slice(1)
const nextState = { ...state }
if (keyPath.length) {
return update(
nextState,
keyPath.join('.'),
(scopedState) => fn(scopedState, action),
)
}
return fn(state as any, action)
}
return {
reducer,
action: createAction(type),
}
}