All files / src/list listReducer.js

72% Statements 18/25
76.19% Branches 16/21
75% Functions 3/4
75% Lines 18/24
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    7x 7x                   3x 3x   7x       7x 2x     2x     5x 5x   1x   1x   1x   1x   1x           3x       12x 12x          
import { combineReducers } from 'redux'
import { LIST, RESPONSE, CLEAR } from '../constants'
const { SET_OPTIONS, PAGE, NEXT, PREV, LAST, FIRST, SET_PARAMS } = LIST
const DEFAULT = {
  offset: 0,
  pageSize: 10,
  total: null,
  result: [],
  headers: {},
  params: {}
}
 
export function listReducer(model, list) {
  const defaultState = { ...DEFAULT, ...list.options }
  const reducer = (state = defaultState, { payload, type }) => {
    //REJECT actions without payloads & modelName
    Iif (!payload || !payload.modelName) {
      return state
    }
 
    if (payload.modelName !== model.modelName || payload.listName !== list.name) {
      Iif (payload.modelName == model.modelName && type == CLEAR) {
        return { ...defaultState }
      }
      return state
    }
 
    console.log('reducing for ', list.name, payload.listName, payload.modelName, model.modelName);
    switch (type) {
      case SET_OPTIONS:
        return { ...state, ...payload }
      case SET_PARAMS:
        return { ...state, params: { ...state.params, ...payload } }
      case PAGE:
        return { ...state, offset: state.pageSize * payload.page }
      case RESPONSE.FIND:
        return { ...state, result: payload.ids }
      case RESPONSE.COUNT:
        return { ...state, total: payload.count }
      default:
        return state
    }
  }
 
  return reducer
}
 
export function createListReducers(model) {
  Eif (!model.lists) {
    return {}
  }
  const listReducers = {};
  model.lists.forEach(list => listReducers[list.name] = listReducer(model, list))
  return combineReducers(listReducers)
}