All files / src/features/GiveConversation/hooks useInfiniteTeamMembers.ts

62.5% Statements 5/8
25% Branches 1/4
66.66% Functions 2/3
62.5% Lines 5/8

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                                6x             34x   34x 34x                   6x                                    
import { useInfiniteQuery } from "react-query";
import { ROWS_PER_PAGE } from "@hooks/common/usePagination";
import { getAllTeamMembers } from "@services/api/manage-team";
import { ProductParams } from "@services/api/products/queryFactory";
import { QKEY_GET_MENTION_MEMBERS } from "@constants/queryKeys";
import { GENERAL_STALE_TIME } from "@features/Merchants/MerchantSidePanel/constants";
import { useGetCurrentMerchantId } from "@hooks/common";
 
type InfiniteTeamMembersParams = ProductParams & {
  id: number;
  searchQuery?: string;
  sorting?: string;
  memberStatus?: string;
  isEnabled?: boolean;
};
 
export const useInfiniteTeamMembers = ({
  id,
  searchQuery,
  sorting,
  memberStatus,
  isEnabled = true,
}: InfiniteTeamMembersParams) => {
  const { selectedUser } = useGetCurrentMerchantId();
 
  const loggedInUserAccountId = selectedUser?.userAccID;
  return useInfiniteQuery(
    [
      QKEY_GET_MENTION_MEMBERS,
      id,
      searchQuery,
      sorting,
      memberStatus,
      loggedInUserAccountId,
    ],
    async ({ pageParam = 1 }) => {
      const data = await getAllTeamMembers({
        ...{ id, searchQuery, sorting, memberStatus },
        page: pageParam,
        loggedInUserAccountId,
      });
      return { ...data, page: pageParam };
    },
    {
      getNextPageParam: (lastPage) => {
        const totalFetched = lastPage.page * ROWS_PER_PAGE;
        return totalFetched < lastPage.total ? lastPage.page + 1 : undefined;
      },
      staleTime: GENERAL_STALE_TIME,
      refetchOnWindowFocus: false,
      enabled: isEnabled,
    },
  );
};