import { IAction, isType } from '@zaibot/fsa/es5'; import { IEditorDescriptor, IEditorOptions } from '.'; import { stripFields } from '../resource'; export interface IState { readonly creating: boolean; readonly deleting: boolean; readonly error: string; readonly item: Partial; readonly reading: boolean; readonly updating: boolean; } export function makeEditorReducer(descriptor: IEditorDescriptor, options: IEditorOptions) { const emptyState: IState = { creating: false, deleting: false, error: null, item: {}, reading: false, updating: false, }; const { actions } = descriptor; return (state = emptyState, action: IAction): IState => { if (isType(action, actions.APPLY)) { return { ...state, item: { ...(state.item as any), ...(stripFields(action.payload.item) as any) } }; } else if (isType(action, actions.CREATE)) { return { ...state, creating: true, reading: false, updating: false, deleting: false, item: descriptor.resource.create(action.payload.item) }; } else if (isType(action, actions.CREATE_CANCEL)) { return { ...state, creating: false, item: {} }; } else if (isType(action, actions.CREATE_CONTINUE)) { return { ...state, creating: false, item: { ...(state.item as any), ...(action.payload.item as any) } }; } else if (isType(action, actions.READ)) { return { ...state, creating: false, reading: true, updating: false, deleting: false, item: action.payload.item }; } else if (isType(action, actions.READ_CANCEL)) { return { ...state, reading: false, item: {} }; } else if (isType(action, actions.READ_CONTINUE)) { return { ...state, reading: false, item: { ...(state.item as any), ...(action.payload.item as any) } }; } else if (isType(action, actions.UPDATE)) { return { ...state, creating: false, reading: false, updating: true, deleting: false, item: action.payload.item }; } else if (isType(action, actions.UPDATE_CANCEL)) { return { ...state, updating: false, item: {} }; } else if (isType(action, actions.UPDATE_CONTINUE)) { return { ...state, updating: false, item: { ...(state.item as any), ...(action.payload.item as any) } }; } else if (isType(action, actions.DELETE)) { return { ...state, creating: false, reading: false, updating: false, deleting: true, item: action.payload.item }; } else if (isType(action, actions.DELETE_CANCEL)) { return { ...state, deleting: false, item: {} }; } else if (isType(action, actions.DELETE_CONTINUE)) { return { ...state, deleting: false, item: { ...(state.item as any), ...(action.payload.item as any) } }; } else if (isType(action, actions.RESET)) { return { ...state, creating: false, reading: false, updating: false, deleting: false, error: null, item: {} }; } else { return state; } }; }