All files / reducers NavigationCollectionReducer.js

18.75% Statements 6/32
31.25% Branches 5/16
12.5% Functions 1/8
20.69% Lines 6/29
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          1x                                                                                                                                                   4x   1x   1x   1x       1x      
import matchSorter from 'match-sorter';
import Fuse from 'fuse.js';
import { request, invalidate, toArray, receiveNested as receive } from '../boilerplate';
import { REQUEST_NAVCOL, RECEIVE_NAVCOL, INVALIDATE_NAVCOL, SEARCH_NAVCOL } from '../actionTypes';
 
const initState = {
  isFetching: false,
  didInvalidate: false,
  data: {},
};
 
function search(state, action) {
  const searchResults = [];
  if (action.searchString.length > 2) {
    for (const object in state.data) {
      const nav = state.data[object];
      try {
        const fuse = new Fuse(toArray(nav.contents).filter(item => !item.contents), {
          keys: [
            {
              name: 'label',
              weight: 0.9,
            },
            {
              name: 'description',
              weight: 0.1,
            },
          ],
          shouldSort: true,
          threshold: 0.3,
          location: 0,
          distance: 80,
          maxPatternLength: 32,
          minMatchCharLength: 2,
          include: ['score'],
        });
        const NavItems = fuse.search(action.searchString);
        toArray(nav.contents).filter(item => item.contents).map((item) => {
          fuse.set(toArray(item.contents));
          fuse.search(action.searchString).map(childItem => NavItems.push(childItem));
        });
        if (NavItems.length > 0) {
          searchResults.push({
            Name: nav.name,
            Label: nav.label,
            HasResults: NavItems.length > 0,
            NavItems,
            Score: NavItems.length > 0
              ? NavItems.reduce((acc, item) => acc + item.score, 0) / NavItems.length
              : 0,
          });
        }
      } catch (exception) {
        console.log(exception);
      }
    }
  }
 
  const returnResults = searchResults.sort((first, second) => {
    if (first.Score > second.Score) {
      return 1;
    }
    if (first.Score < second.Score) {
      return -1;
    }
    return 0;
  });
 
  const nextState = {
    searchResults: returnResults,
    lastAction: {
      type: SEARCH_NAVCOL,
    },
  };
 
  return { ...state, ...nextState };
}
 
export default function navCollectionReducer(state = initState, action) {
  switch (action.type) {
    case REQUEST_NAVCOL:
      return request(state, action);
    case RECEIVE_NAVCOL:
      return receive(state, action);
    case INVALIDATE_NAVCOL:
      return invalidate(state, action);
    case SEARCH_NAVCOL:
      return search(state, action);
    default:
      return state;
  }
}