All files / src/hooks/acquirer-api/merchants useListAllEnterprises.tsx

93.75% Statements 15/16
78.94% Branches 15/19
100% Functions 3/3
93.75% Lines 15/16

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                                    90x                     1231x 1231x                       1231x   1231x 57x   57x   57x       57x 57x   57x           1231x     57x 17x                   1231x              
import { customInstance } from "@services/api";
import { checkPortals } from "@utils/routing";
import { useQuery } from "react-query";
import { QKEY_LIST_ENTERPRISES } from "@constants/queryKeys";
import { safeParse } from "@utils/index";
 
interface Props {
  page?: number;
  max?: number;
  sort?: string;
  searchText?: string;
  filter?: string;
  enabled?: boolean;
  tabFilter?: string;
  cacheTime?: number;
  staleTime?: number;
}
 
export const useListAllEnterprises = ({
  page = 0,
  max = 20,
  sort = "name",
  searchText = "",
  filter = "",
  enabled = true,
  tabFilter = "",
  cacheTime = 0,
  staleTime = 0,
}: Props) => {
  const { isAcquirerPortal } = checkPortals();
  const params = new URLSearchParams(
    safeParse(
      // TODO: this does not make any sense.
      JSON.stringify({
        page: `${page}`,
        max: `${max}`,
        sort: sort,
        q: searchText || undefined,
      }),
    ) as Record<string, string>,
  );
 
  const searchString = params.toString();
 
  const getEnterprises = () => {
    const endpoint = tabFilter ? `/enterprises${tabFilter}` : "/enterprises";
 
    const params = new URLSearchParams(searchString);
 
    Iif (filter) {
      params.set("filter", filter);
    }
 
    const query = params.toString();
    const url = query ? `${endpoint}?${query}` : endpoint;
 
    return customInstance({
      url,
      method: "GET",
    });
  };
 
  const { data, isLoading, error, isFetching } = useQuery(
    [QKEY_LIST_ENTERPRISES, page, searchText, filter, tabFilter],
    async () => {
      const providers = await getEnterprises();
      return providers;
    },
    {
      refetchOnWindowFocus: false,
      enabled: isAcquirerPortal && enabled,
      cacheTime,
      staleTime,
    },
  );
 
  return {
    data,
    isLoading,
    error,
    isFetching,
  };
};