import { ParamInstance } from './agg_params_helper' ; export enum AGG_TYPE_ACTION_KEYS { TOUCHED = 'aggTypeTouched' , VALID = 'aggTypeValid' } export interface AggTypeState { touched : boolean ; valid : boolean ; } export interface AggTypeAction { type : AGG_TYPE_ACTION_KEYS ; payload : boolean ; } function aggTypeReducer ( state , action ) { switch ( action . type ) { case AGG_TYPE_ACTION_KEYS . TOUCHED : return { ... state , touched : action . payload } ; case AGG_TYPE_ACTION_KEYS . VALID : return { ... state , valid : action . payload } ; default : throw new Error ( ) ; } } export enum AGG_PARAMS_ACTION_KEYS { TOUCHED = 'aggParamsTouched' , VALID = 'aggParamsValid' , RESET = 'aggParamsReset' } export interface AggParamsItem { touched : boolean ; valid : boolean ; } export interface AggParamsAction { type : AGG_PARAMS_ACTION_KEYS ; payload ? : boolean ; paramName ? : string ; } export interface AggParamsState { [ key ] : AggParamsItem ; } function aggParamsReducer ( state , { type , paramName = '' , payload } ) { const targetParam = state [ paramName ] || { valid : true , touched : false , } ; switch ( type ) { case AGG_PARAMS_ACTION_KEYS . TOUCHED : return { ... state , [ paramName ] : { ... targetParam , touched : payload , } , } as AggParamsState ; case AGG_PARAMS_ACTION_KEYS . VALID : return { ... state , [ paramName ] : { ... targetParam , valid : payload , } , } as AggParamsState ; case AGG_PARAMS_ACTION_KEYS . RESET : return { } ; default : throw new Error ( ) ; } } function initAggParamsState ( params ) { const state = params . reduce ( ( stateObj , param ) => { stateObj [ param . aggParam . name ] = { valid : true , touched : false , } ; return stateObj ; } , { } ) ; return state ; } export { aggTypeReducer , aggParamsReducer , initAggParamsState } ;