All files / src/hooks/merchant-api/sweepstakes useListSweepstakes.tsx

0% Statements 0/24
0% Branches 0/13
0% Functions 0/9
0% Lines 0/22

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 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                                                                                                                                                                                   
import React, { useMemo, useRef } from "react";
import { useGetSweepstake } from "@services/api/products/sweepstakes";
import { useAppSelector } from "@redux/hooks";
import { selectQueryFilters } from "@redux/slices/fundraisers";
import { encodedQueryFilterMap } from "@services/filtering";
import { ROWS_PER_PAGE, usePagination } from "@hooks/common/usePagination";
import { detectMobile } from "@utils/index";
import { useCachedList } from "@hooks/common/useCachedList";
import { sorting as sortingReducer } from "@redux/slices/fundraisers";
import { QKEY_LIST_SWEEPSTAKES } from "@constants/queryKeys";
import { selectQueryString } from "@redux/slices/search";
import { useStateEffect } from "@hooks/customReactCore";
 
const useSweepstakes = () => {
  const queryFilters = useAppSelector(selectQueryFilters);
  const sorting = useAppSelector(sortingReducer);
  const searchQuery = useAppSelector((state) =>
    selectQueryString(state, QKEY_LIST_SWEEPSTAKES),
  );
 
  const queryFilter = useMemo(
    () => encodedQueryFilterMap(queryFilters),
    [queryFilters],
  );
 
  const { page, setPage } = usePagination(0, queryFilter.products);
 
  const { allData, invalidateCache } = useCachedList(
    QKEY_LIST_SWEEPSTAKES,
    false,
    page,
  );
 
  const loadingRef = useRef<boolean>(false);
  const queryString = queryFilter.products ? `%3B${queryFilter.products}` : "";
 
  const { data, isLoading, isError } = useGetSweepstake(
    {
      queryString,
      page,
      sorting,
      searchQuery,
    },
    {
      refetchOnWindowFocus: false,
 
      onSuccess(_data) {
        setTimeout(() => {
          loadingRef.current = false;
        }, 700);
      },
    },
  );
 
  const handlePageChange = (
    event: React.ChangeEvent<unknown>,
    value: number,
  ) => {
    setPage(value);
  };
 
  useStateEffect(() => {
    if (detectMobile()) invalidateCache();
    setPage(1);
  }, [sorting, searchQuery, queryString]);
 
  const usedData = detectMobile() ? allData : data?.data ?? [];
 
  return {
    data,
    isLoading,
    isError,
    page,
    setPage: () => setPage((current) => current + 1),
    setPageDispatcher: setPage,
    handlePageChange,
    totalRows: data?.total ?? 0,
    loadingRef,
    rowsPerPage: ROWS_PER_PAGE,
    currentPageRows: usedData,
    allRows: usedData,
    state: {
      isEmpty: !queryString && !searchQuery && data?.total === 0,
      isError,
    },
  };
};
 
export default useSweepstakes;