import maxBy from 'lodash/maxBy' ; import remove from 'lodash/remove' ; import { ADD_UI_SCHEMA , REMOVE_UI_SCHEMA , UISchemaActions } from '../actions' ; import { NOT_APPLICABLE } from '../testers' ; import { JsonSchema , UISchemaElement } from '../models' ; import { Reducer } from '../util' ; export type UISchemaTester = ( schema : JsonSchema , schemaPath : string , path : string ) => number ; export interface JsonFormsUISchemaRegistryEntry { tester : UISchemaTester ; uischema : UISchemaElement ; } export const uischemaRegistryReducer = ( state = [ ] , action ) => { switch ( action . type ) { case ADD_UI_SCHEMA : return state . slice ( ) . concat ( { tester : action . tester , uischema : action . uischema } ) ; case REMOVE_UI_SCHEMA : const copy = state . slice ( ) ; remove ( copy , entry => entry . tester === action . tester ) ; return copy ; default : return state ; } } ; export const findMatchingUISchema = ( state ) => ( jsonSchema , schemaPath , path ) : UISchemaElement => { const match = maxBy ( state , entry => entry . tester ( jsonSchema , schemaPath , path ) ) ; if ( match !== undefined && match . tester ( jsonSchema , schemaPath , path ) !== NOT_APPLICABLE ) { return match . uischema ; } return undefined ; } ;