g33k57
7/12/2019 - 1:35 PM

A more efficient reducer for adding/editing/removing objects in an array using a hash table

A more efficient reducer for adding/editing/removing objects in an array using a hash table

const initialState = {
  byId: ['1', '2', '3'],
  byHash: {
    '1': {id: '1', content: {title: 'item 1'}},
    '2': {id: '2', content: {title: 'item 2'}},
    '3': {id: '3', content: {title: 'item 3'}}
  }
}

const action1 = {
  type: 'add',
  id: '4',
  payload: { id: '4', content: {title: 'item 4' }}
}

const action2 = {
  type: 'update',
  id: '2',
  payload: { content: {title: 'item 2 updated' }}
}

const action3 = {
  type: 'remove',
  id: '4'
}

const reducer = (state = intialState, action = {}) => {
  switch(action.type){
    case 'add': {
      return {
        byId: [ ...state.byId, action.id],
        byHash: {
          ...state.byHash,
          [action.id]: action.payload
        }
      }
    }
      
    case 'update': {
      state.byHash[action.id] = {
        ...state.byHash[action.id],
        ...action.payload
      }
      return {
        ...state
      }
    }  
      
    case 'remove': {
      const prunedIds = state.byId.filter(item => {
        return item !== action.id // return all the items not matching the action.id
      })
      delete state.byHash[action.id] // delete the hash associated with the action.id
      
      return {
        byId: prunedIds,
        byHash: state.byHash
      }
    }
    
    default: {
      return state
    }
  }
}