All files / src/features/Settings/Team/hooks teamMembersInfiniteQueryFunction.ts

0% Statements 0/20
0% Branches 0/23
0% Functions 0/2
0% Lines 0/20

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                                                                                                                         
import { customInstance } from "@services/api";
import { ROWS_PER_PAGE } from "@hooks/common/usePagination";
 
export const teamMembersInfiniteQueryFunction =
  (params: {
    merchantId: number;
    sorting?: string;
    searchQuery?: string;
    memberStatus?: string;
    rowsPerPage?: number;
  }) =>
  async ({
    pageParam = 1,
    signal,
  }: {
    pageParam?: number;
    signal?: AbortSignal;
  }) => {
    const { merchantId, sorting, searchQuery, memberStatus, rowsPerPage } =
      params;
    const sortParam = sorting || "-user.accID";
    let baseURL = `/accounts/${merchantId}/members?sort=${sortParam}`;
 
    if (searchQuery) {
      baseURL += `&q="${searchQuery}"`;
    }
    baseURL += `&page=${pageParam}&max=${rowsPerPage || ROWS_PER_PAGE}`;
 
    const filters = [];
    if (memberStatus && memberStatus === "joined") {
      filters.push(`memberStatus:"joined"`);
    }
    if (filters.length > 0) {
      baseURL += `&filter=${filters.join("%3B")}`;
    }
 
    try {
      const data = await customInstance({
        url: baseURL,
        method: "GET",
        signal,
      });
 
      const totalPages = Math.ceil(
        Number(data.total ?? 0) / (rowsPerPage || ROWS_PER_PAGE),
      );
      const hasMorePages = pageParam < totalPages;
 
      return {
        data: data.data || [],
        nextCursor: hasMorePages ? pageParam + 1 : null,
        total: data.total || 0,
        currentPage: pageParam,
        totalPages,
      };
    } catch (error) {
      console.error("Error fetching team members:", error);
      throw error;
    }
  };