All files / reducers ChecklistReducer.js

38.46% Statements 5/13
50% Branches 5/10
50% Functions 1/2
38.46% Lines 5/13
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                                          4x   1x   1x   1x       1x      
import matchSorter from 'match-sorter';
import { request, invalidate, receive, initState, toArray } from '../boilerplate';
import { REQUEST_CHECKLIST, RECEIVE_CHECKLIST, INVALIDATE_CHECKLIST, SEARCH_CHECKLIST } from '../actionTypes';
 
function search(state, action) {
  let searchResults = [];
  if (action.searchString.length > 1 && state.data.items.length > 0) {
    const checkLists = state.data.items;
    const checkListResults = matchSorter(toArray(checkLists), action.searchString, {
      keys: ['checklistCdDescr', 'information'],
      threshold: matchSorter.rankings.WORD_STARTS_WITH,
    });
    searchResults = checkListResults;
  }
  const nextState = {
    searchResults,
  };
  return { ...state, ...nextState };
}
 
export default function(state = initState, action) {
  switch (action.type) {
    case REQUEST_CHECKLIST:
      return request(state, action);
    case RECEIVE_CHECKLIST:
      return receive(state, action);
    case INVALIDATE_CHECKLIST:
      return invalidate(state, action);
    case SEARCH_CHECKLIST:
      return search(state, action);
    default:
      return state;
  }
}