All files / store/pages/Packages Packages.tsx

92.3% Statements 12/13
100% Branches 4/4
75% Functions 3/4
92.3% Lines 12/13

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 51 52 53 54 55 56 57                5x 3x 1x 1x             1x               5x 5x   5x         5x 3x     5x   5x                                
import { useEffect } from "react";
import { useQuery } from "react-query";
import { useLocation, useSearchParams } from "react-router-dom";
import { PackageList } from "../../components/PackageList/PackageList";
import { v4 as uuidv4 } from "uuid";
import { EmptyResultSection } from "../../components/EmptyResultSection";
 
function Packages() {
  const getData = async () => {
    const response = await fetch(`/store.json${search}`);
    const data = await response.json();
    const packagesWithId = data.packages.map((item: string[]) => {
      return {
        ...item,
        id: uuidv4(),
      };
    });
 
    return {
      total_items: data.total_items,
      total_pages: data.total_pages,
      packages: packagesWithId,
      categories: data.categories,
    };
  };
 
  const { search } = useLocation();
  const [searchParams] = useSearchParams();
 
  const { data, status, refetch, isFetching } = useQuery(
    ["data", search],
    getData
  );
 
  useEffect(() => {
    refetch();
  }, [searchParams]);
 
  const isResultEmpty = data && data.packages.length === 0;
 
  return (
    <>
      {isResultEmpty ? (
        <EmptyResultSection
          isFetching={isFetching}
          searchTerm={searchParams.get("q")}
          data={data}
        />
      ) : (
        <PackageList isFetching={isFetching} status={status} data={data} />
      )}
    </>
  );
}
 
export default Packages;