All files / hooks useKiwtSASQuery.js

95% Statements 19/20
100% Branches 6/6
75% Functions 3/4
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            27x 218x 218x       218x         57x 46x   11x         27x 937x 937x 937x 937x   937x   937x 31x     937x 218x 218x   937x        
import { useState } from 'react';
import { useLocation, useHistory } from 'react-router-dom';
 
import buildUrl from '../utils/buildUrl';
import useQindex from './useQIndex';
 
const locationQuerySetter = ({ location, history, nsValues, state }) => {
  const { pathname, search } = location;
  const url = buildUrl(location, nsValues);
 
  // Do not push to history if the url didn't change
  // https://issues.folio.org/browse/STSMACOM-637
  if (
    `${pathname}${search}` !== url
  ) {
    // Only PUSH to history if we're not doing an initialisation OR changing from external-location
    // This is the SASQ state being used here
    if (['external-location', 'init.reset'].includes(state?.changeType)) {
      history.replace(url);
    } else {
      history.push(url);
    }
  }
};
 
const useKiwtSASQuery = () => {
  const history = useHistory();
  const location = useLocation();
  const [query, setQuery] = useState({});
  const queryGetter = () => query;
 
  const [qindex] = useQindex();
  // Ensure we update the query along with the querySetter
  if (query.qindex !== qindex) {
    setQuery({ ...query, qindex });
  }
 
  const querySetter = ({ nsValues, ...rest }) => {
    setQuery({ ...query, ...nsValues, qindex });
    locationQuerySetter({ ...rest, location, history, nsValues });
  };
  return { query, queryGetter, querySetter };
};
 
export default useKiwtSASQuery;