/** * External dependencies */ import { isEqual } from 'lodash'; import type { AnyAction } from '@nab/types'; /** * Internal dependencies */ import { INIT_STATE } from '../config'; import type { State as FullState } from '../types'; import type { PageAction as Action } from '../actions/page'; type State = FullState[ 'page' ]; export function page( state = INIT_STATE.page, action: AnyAction ): State { return actualReducer( state, action as Action ) ?? state; } function actualReducer( state: State, action: Action ): State { switch ( action.type ) { case 'SET_PAGE_ATTRIBUTE': { const { name, value } = action; if ( ! name ) { return state; } if ( isEqual( state[ name ], value ) ) { return state; } return { ...state, [ name ]: value, }; } case 'LOCK_UI': return { ...state, isLocked: true, }; case 'UNLOCK_UI': return { ...state, isLocked: false, }; } }