All files / src/model modelReducer.js

77.36% Statements 41/53
83.58% Branches 56/67
83.33% Functions 5/6
83.67% Lines 41/49
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119              12x 63x 3x   60x   1x     2x 2x       57x       12x 63x 3x   60x           60x       12x 12x 63x 3x   60x               6x                 6x       48x       12x 12x 63x 3x     60x       60x   1x   1x   1x   1x   1x   1x     1x 1x 1x 1x 1x     1x           53x       12x              
import { schema, normalize } from 'normalizr'
import { combineReducers } from 'redux'
import _ from 'lodash'
import { REQUEST, RESPONSE, ERROR, SELECTED, RECEIVED, CLEAR } from '../constants'
import { createListReducers } from '../list/listReducer'
 
export default (model) => {
  const instances = (state = {}, { type, payload }) => {
    if (payload && payload.modelName !== model.modelName) {
      return state
    }
    switch (type) {
      case RECEIVED:
        return { ...state, ...payload.instances }
      case RESPONSE.DELETE_BY_ID:
      case RESPONSE.DELETE:
        const newState = { ...state }
        return _.pick(newState, _.difference(_.keys(newState), [payload.id]))
      case CLEAR:
        return {}
      default:
        return state
    }
  }
 
  const error = (state = null, { type, payload }) => {
    if (payload && payload.modelName !== model.modelName) {
      return state
    }
    switch (type) {
      case ERROR:
        return payload
      case CLEAR:
        return null
      default:
        return state
    }
  }
 
  const DEFAULT_REQUEST = { loading: false }
  const request = (state = DEFAULT_REQUEST, { type, payload }) => {
    if (payload && payload.modelName !== model.modelName) {
      return state
    }
    switch (type) {
      case REQUEST.FIND:
      case REQUEST.FIND_BY_ID:
      case REQUEST.CREATE:
      case REQUEST.UPDATE:
      case REQUEST.DELETE_BY_ID:
      case REQUEST.DELETE:
      case REQUEST.CUSTOM:
        return { ...state, loading: true }
      case RESPONSE.FIND:
      case RESPONSE.FIND_BY_ID:
      case RESPONSE.CREATE:
      case RESPONSE.UPDATE:
      case RESPONSE.DELETE_BY_ID:
      case RESPONSE.DELETE:
      case RESPONSE.CUSTOM:
      case ERROR:
        return { ...state, loading: false }
      case CLEAR:
        return { ...DEFAULT_REQUEST }
      default:
        return state
    }
  }
 
  const DEFAULT_LAST_STATE = { find: [], delete: [], custom: {} }
  const last = (state = DEFAULT_LAST_STATE, { type, payload }) => {
    if (payload && payload.modelName !== model.modelName) {
      return state
    }
 
    Iif (payload && payload.listName) {
      return state
    }
 
    switch (type) {
      case RESPONSE.FIND:
        return { ...state, find: payload.ids }
      case RESPONSE.FIND_BY_ID:
        return { ...state, findById: payload.id }
      case RESPONSE.CREATE:
        return { ...state, create: payload.id }
      case RESPONSE.UPDATE:
        return { ...state, update: payload.id }
      case RESPONSE.UPDATE_ALL:
        return { ...state, updateAll: payload.count }
      case RESPONSE.COUNT:
        return { ...state, count: payload.count }
      case RESPONSE.DELETE_BY_ID:
        //case RESPONSE.DELETE:
        const newState = { ...state, deleteById: payload.id }
        Iif (newState.findById == payload.id) newState.findById = null
        Iif (newState.create == payload.id) newState.create = null
        Iif (newState.update == payload.id) newState.update = null
        Iif (newState.find.indexOf(payload.id) > -1) {
          newState.find = newState.find.filter(id => id !== payload.id)
        }
        return newState
      case RESPONSE.CUSTOM:
        return { ...state, custom: { ...state.custom, [payload.name]: payload.response } }
      case CLEAR:
        return { ...DEFAULT_LAST_STATE }
      default:
        return state
    }
  }
  //console.log('listREducer', listReducer)
  return combineReducers({
    instances,
    last,
    request,
    error,
    lists: createListReducers(model)
  })
}