All files / entities index.js

100% Statements 12/12
100% Branches 2/2
100% Functions 6/6
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48              2x 2x   4x 5x                   2x 2x           1x     1x     2x 2x   2x       2x            
import { buildReducer } from '../lib/buildReducer'
import omit from 'lodash/omit'
import { get, set } from 'dot-prop-immutable'
import * as ActionTypes from './actionTypes'
export * from './actions'
export * from './selectors'
 
const initialState = {}
const actions = {
  [ActionTypes.MERGE]: (state, { payload }) => {
    return Object.keys(payload).reduce((acc, domain) => {
      return {
        ...acc,
        [domain]: {
          ...acc[domain],
          ...payload[domain]
        }
      }
    }, state)
  },
  [ActionTypes.REMOVE]: (state, { payload }) => {
    const { domain, keys } = payload
    return {
      ...state,
      [domain]: omit(state[domain], keys)
    }
  },
  [ActionTypes.REMOVE_ALL]: (state, { payload }) => {
    return omit(state, payload.domain)
  },
  [ActionTypes.RESET]: () => {
    return initialState
  },
  [ActionTypes.UPDATE]: (state, { payload }) => {
    const { domain, id, data } = payload
    const existing = get(state, `${domain}.${id}`) || {}
    //console.log('Update existing', existing, data)
    const merged = {
      ...existing,
      ...data
    }
    return set(state, `${domain}.${id}`, merged)
  }
}
 
 
export default buildReducer(initialState, actions)