All files / src/components/Settlement/hooks useSettlementInfiniteMerchants.tsx

59.45% Statements 22/37
52.5% Branches 21/40
37.5% Functions 3/8
68.75% Lines 22/32

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                          30x         1129x   1129x 1129x 1129x 1294x   1129x           1129x                           1129x   1129x 141x 85x 85x     141x           109x 109x 32x 32x       141x     1129x                   1129x           1129x    
import React, { useEffect, useRef, useState } from "react";
import { useGetMerchants } from "@services/api/merchants";
import { useAppSelector } from "@redux/hooks";
import { QKEY_LIST_ACQUIRER_MERCHANTS } from "@constants/queryKeys";
import { selectQueryString } from "@redux/slices/search";
import { useGetCurrentMerchantId } from "@hooks/common";
 
type Props = {
  disableStats?: boolean;
  query?: string;
  sorting?: string;
};
 
export const useInfiniteMerchants = ({
  disableStats = false,
  query,
  sorting,
}: Props) => {
  const { merchantId } = useGetCurrentMerchantId();
 
  const [data, setData] = useState<any[]>([]);
  const [page, setPage] = useState(1);
  const searchQuery = useAppSelector((state) =>
    selectQueryString(state, query ?? QKEY_LIST_ACQUIRER_MERCHANTS),
  );
  const parentID = useRef(0);
 
  const {
    data: pageData,
    isLoading,
    isFetching,
  } = useGetMerchants(
    {
      queryString: "",
      page: data.length > 20 && searchQuery ? 1 : page,
      searchQuery,
      sorting: sorting,
    },
    {
      refetchOnWindowFocus: false,
    },
    "",
    disableStats,
  );
 
  const BOTTOM_OFFSET_PX = 7;
 
  useEffect(() => {
    if (parentID.current !== merchantId) {
      setPage(1);
      setData([]);
    }
 
    if (
      (data.length === 0 && pageData?.data) ||
      searchQuery ||
      (searchQuery && !pageData?.data) ||
      parentID.current !== merchantId
    ) {
      setPage(1);
      setData(pageData?.data || []);
    } else if (data.length <= 20 && !searchQuery && page === 1) {
      setData(pageData?.data || []);
    } else Eif (data.length > 0) {
      setData((prev) => [...prev, ...(pageData?.data || [])]);
    }
    parentID.current = merchantId;
  }, [pageData, searchQuery, merchantId]);
 
  const handleScroll = (e: React.UIEvent<HTMLElement>) => {
    if (isLoading) return;
    const { scrollTop, clientHeight, scrollHeight } = e.currentTarget;
    const isNearBottom =
      scrollHeight - (scrollTop + clientHeight) <= BOTTOM_OFFSET_PX;
    if (isNearBottom && pageData?.total > data.length) {
      setPage((prev) => prev + 1);
    }
  };
 
  const loadNextPage = () => {
    if (isLoading) return;
    if (pageData?.total > data.length) {
      setPage((prev) => prev + 1);
    }
  };
  return { handleScroll, data, isLoading, setPage, loadNextPage, isFetching };
};