All files / hooks useQIndex.js

56.25% Statements 9/16
16.66% Branches 1/6
66.66% Functions 2/3
56.25% Lines 9/16

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 47 48 49 50        27x 937x 937x   937x 937x   937x                                             937x 664x                   937x        
import React, { useEffect, useState } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import queryString from 'query-string';
 
const useQIndex = () => {
  const history = useHistory();
  const location = useLocation();
 
  const query = queryString.parse(location.search);
  const [currentQindex, setCurrentQindex] = useState(query?.qindex);
 
  const handleQindexChange = (newQindex) => {
    setCurrentQindex(newQindex);
    if (newQindex !== query?.qindex) {
      let newQuery;
      if (newQindex !== '') {
        newQuery = {
          ...query,
          qindex: newQindex
        };
      } else {
        newQuery = {
          ...query,
          qindex: undefined
        };
      }
 
      history.push({
        pathname: location.pathname,
        search: `?${queryString.stringify(newQuery)}`
      });
    }
  };
 
  useEffect(() => {
    Iif (currentQindex !== query?.qindex) {
      setCurrentQindex(query?.qindex);
    }
  }, [
    currentQindex,
    history,
    location,
    query
  ]);
 
  return [currentQindex, handleQindexChange];
};
 
export default useQIndex;