massivelines
6/26/2018 - 2:17 PM

Update State React Redux Immutable

export default function ActiveQueryReducer(_state=initialState,action={}){
  return _state
  .update(state =>
    state.update('filtered', ()=>
      //true if any root level filters and sorts are set (column filters, sorting, etc - anything not within a Query())
      !Boolean(
        state
        .keySeq()
        .toArray()
        .filter(key =>
          !['caseKinds',
            'fieldList',
            'didRun',
            'filtered',
            'lastState',
            'queries'
          ]
          .includes(key)
        )
        .every(key =>{
          return Boolean(state.get(key) === initialState.get(key))
        })
      )
      //true if there is more than one query
      || state.get('queries').size > 1
      //true if anything is set within the first Query()
      //note that itemKinds and itemTypes need to be filtered to remove their members with false values
      // i.e. if you click the Image filter button, itemKinds will be Map { image: true }
      // clicking the same button again results in itemKinds being Map { image: false }
      // removing these is necessary for comparison
      || !Boolean(
        state
        .getIn(['queries', 0])
        .update('itemKinds', kinds=>
          kinds.valueSeq().toArray().filter(val=>Boolean(val)).length === 0
          ? new Query().get('itemKinds')
          : kinds
        )
        .update('itemTypes', types=>
          types.valueSeq().toArray().filter(val=>Boolean(val)).length === 0
          ? new Query().get('itemTypes')
          : types
        )
        .entrySeq()
        .every(entry=>
          Boolean(entry[1] === new Query().get(entry[0]))
        )
      )
    )
  )
  .update(state => {

    switch(action.type){
      ...
    }
  });

}