/** * External dependencies */ import type { AnyAction } from '@nelio-content/types'; /** * Internal dependencies */ import { INIT_STATE } from './config'; import type { State } from './types'; import type { Action } from './actions'; export function reducer( state = INIT_STATE, action: AnyAction ): State { return actualReducer( state, action as Action ) ?? state; } function actualReducer( state: State, action: Action ): State { switch ( action.type ) { case 'INIT_FIELD': return { ...state, isInit: { ...state.isInit, [ action.field ]: true, }, values: { ...state.values, [ action.field ]: action.value, }, attributes: { ...state.attributes, [ action.field ]: action.attributes, }, }; case 'SET_FIELD_VALUE': return { ...state, values: { ...state.values, [ action.field ]: action.value, }, }; case 'SET_FIELD_ATTRIBUTES': return { ...state, attributes: { ...state.attributes, [ action.field ]: { ...state.attributes[ action.field ], ...action.attributes, }, }, }; } }