import { createDefinition } from '../../state/createDefinition' ; import { Action } from 'redux-actions' ; const defaultState = { error : null , isDirty : false , isValid : false , value : '' , } ; interface IState { error : string | null ; isDirty : boolean ; isValid : boolean ; value : string | null ; } type Validator = [ ( value : string ) => boolean , string ] ; interface IActionsParams { validators : Validator [ ] ; } export default createDefinition < IState > ( { reducers : ( { validators = [ ] } ) => ( { reset : ( state , action ) => defaultState , set : ( state , { payload } ) => { const invalid = validators . find ( ( [ fn , _ ] ) => ! fn ( payload ) ) ; return { ... state , error : invalid && invalid [ 1 ] , isDirty : true , isValid : ! ! invalid , value : payload || null , } ; } , unset : ( state , action ) => ( { ... state , value : null , } ) } ) , defaultState , selectors : { error : ( state ) => state . error , get : ( state ) => state , isSet : ( state ) => ! ! state . value , isValid : ( state ) => state . isValid , value : ( state ) => state . value , } , transformInitialState : ( initialState = { } ) => ( { ... defaultState , ... initialState , } ) , } ) ;