All files / src/features/Merchants/MerchantSidePanel/WithRepository useWithRepository.ts

42.85% Statements 6/14
33.33% Branches 2/6
42.85% Functions 3/7
42.85% Lines 6/14

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 91        21x     126x                                                                                                                 126x             126x   13x             126x                    
import { QueryKey, useQuery, UseQueryOptions } from "react-query";
import { IComponentRepository, RepositoryConfig } from "./types";
import { UNDERWRITING_API_KEY_BASE } from "../constants";
 
export const createRepositoryHooks = <T>(
  config: RepositoryConfig,
  repository: IComponentRepository<T>,
) => ({
  useGetItemById: (id: string, options?: UseQueryOptions<T, Error>) => {
    const queryKey: QueryKey = ["item", config.tag, config.baseUrl, id];
 
    const result = useQuery({
      queryKey,
      queryFn: () => repository.getById(config.baseUrl, id),
      ...options,
      ...(typeof config?.enabled === "boolean" && {
        enabled: config?.enabled,
      }),
    });
 
    return {
      data: result.data,
      isLoading: result.isLoading,
      error: result.error,
      refetch: result.refetch,
    };
  },
  useGetItemsByQueryParams: (
    queryParams: string,
    options?: UseQueryOptions<T, Error>,
  ) => {
    const queryKey: QueryKey = [
      "item",
      config.tag,
      config.baseUrl,
      queryParams,
    ];
 
    const result = useQuery({
      queryKey,
      queryFn: () => repository.getByQueryParams(config.baseUrl, queryParams),
      ...options,
      ...(typeof config?.enabled === "boolean" && {
        enabled: config?.enabled,
      }),
    });
 
    return {
      data: result.data,
      isLoading: result.isLoading,
      error: result.error,
      refetch: result.refetch,
    };
  },
 
  useGetItems: (
    urlParams: string,
    options?: UseQueryOptions<
      { total: number; data: T[] },
      Error,
      { total: number; data: T[] },
      QueryKey
    >,
  ) => {
    const queryKey: QueryKey = [
      UNDERWRITING_API_KEY_BASE,
      config.tag,
      config.baseUrl,
      urlParams,
    ];
 
    const result = useQuery({
      queryKey,
      queryFn: () => repository.getAll(config.baseUrl + urlParams),
      ...options,
      ...(typeof config?.enabled === "boolean" && {
        enabled: config?.enabled,
      }),
    });
 
    return {
      data: result.data,
      total: result.data?.total,
      isLoading: result.isLoading,
      isFetching: result.isFetching,
      error: result.error,
      refetch: result.refetch,
    };
  },
});