/** * 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 { ResultsAction as Action } from '../actions/results'; type State = FullState[ 'results' ]; export function results( state = INIT_STATE.results, action: AnyAction ): State { return actualReducer( state, action as Action ) ?? state; } function actualReducer( state: State, action: Action ): State { switch ( action.type ) { case 'RECEIVE_RESULT': { if ( isEqual( state[ action.key ], action.result ) ) { return state; } return { ...state, [ action.key ]: action.result, }; } } }