All files / src/list listSelectors.js

92.31% Statements 24/26
100% Branches 0/0
90.91% Functions 10/11
90.48% Lines 19/21
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        14x   7x   7x 1x   1x 3x     7x 1x 1x     7x 1x 1x 3x   7x 3x 3x     7x   7x         7x      
import _ from 'lodash'
 
export default (list, modelSelectors) => {
  
  const getListObj = (state) => modelSelectors.getModelObj(state).lists[list]
  
  const getTotal = (state) => getListObj(state).total
  
  const getInstances = (state) => {
    const result = getListObj(state).result
    //modelSelectors.getInstances() won't work, as it returns Array, instead of Object Map
    const instances = modelSelectors.getModelObj(state).instances 
    return _.map(result, id => instances[id])
  }
 
  const getCurrentPage = (state) => {
    const listObj = getListObj(state)
    return Math.ceil(listObj.offset/listObj.pageSize)
  }
 
  const getPages = (state) => {
    const listObj = getListObj(state)
    const pages = Math.ceil(listObj.total/listObj.pageSize)
    return [...Array(pages)].map((_, i) => i++)
  }
  const hasNext = (state) => {
    const listObj = getListObj(state)
    return (listObj.offset + listObj.pageSize < listObj.total)
  }
 
  const hasPrev = (state) => getListObj(state).offset > 0
 
  const getPageSize = (state) => {
    const listObj = getListObj(state)
    return listObj.pageSize
  }
 
  return {
    getListObj, getInstances, getTotal, getPages, getCurrentPage, hasNext, hasPrev, getPageSize
  }
}