import { find , uniq , without } from 'lodash' ; import { Action } from 'redux-actions' ; import { createDefinition } from '../../state/createDefinition' ; import { isArray } from 'lodash' ; interface IState { index : string [ ] ; } export default createDefinition < IState > ( { reducers : { add : ( state , { payload } ) : IState => { if ( ! payload ) { return state ; } const arr = isArray ( payload ) ? [ ... state . index , ... payload ] : [ ... state . index , payload ] ; return { index : uniq ( arr ) } ; } , remove : ( state , { payload } ) : IState => { if ( ! payload ) { return state ; } const index = isArray ( payload ) ? without . apply ( null , [ state . index , ... payload ] ) : without ( state . index , payload ) ; return { index } ; } , reset : ( ) => ( { index : [ ] } ) , set : ( state , { payload } ) : IState => { if ( ! payload ) { return state ; } return { index : payload } ; } , toggle : ( state , { payload } ) : IState => { if ( ! payload ) { return state ; } const found = find ( state . index , ( e ) => e === payload ) ; return { index : found ? without ( state . index , payload ) : uniq ( [ ... state . index , payload ] ) } ; } , } , defaultState : { index : [ ] } , selectors : { get : ( state ) : string [ ] => state . index , includes : ( state , params = undefined ) : boolean => params && params . id ? state . index . includes ( params . id ) : false , count : ( state ) : number => state . index . length , } , transformInitialState : ( initialState ) : IState => ( { index : initialState } ) } ) ;