All files / src/pages/AcquirerPortal/LegalDocuments/api useLegalDocuments.ts

100% Statements 22/22
80% Branches 8/10
100% Functions 10/10
100% Lines 22/22

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                    8x 303x 303x 303x       48x     45x             8x 289x 289x 289x     43x               8x 289x 289x 289x     6x               8x 261x 261x 261x       9x     9x        
import { useQuery } from "react-query";
import { customInstance } from "@services/api";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { LEGAL_DOC_KEYS } from "./keys";
import type {
  LegalDocument,
  DocumentVersion,
  VersionHistoryItem,
} from "../types";
 
export const useListLegalDocuments = () => {
  const { data } = useGetMerchantById();
  const accID = data?.accID;
  return useQuery<LegalDocument[]>(
    [LEGAL_DOC_KEYS.list, accID],
    () =>
      // Endpoint returns a { total, data } envelope; unwrap to the array.
      customInstance({
        url: `/merchants/${accID}/legal-documents`,
        method: "GET",
      }).then((res) => res?.data ?? []),
    // retry: 1 (not the default 3) bounds how long the loading skeletons show
    // before a failure surfaces as the table's error state.
    { enabled: !!accID, refetchOnWindowFocus: false, retry: 1 },
  );
};
 
export const useLegalDocument = (documentID?: string | number) => {
  const { data } = useGetMerchantById();
  const accID = data?.accID;
  return useQuery<DocumentVersion>(
    [LEGAL_DOC_KEYS.detail, accID, documentID],
    () =>
      customInstance({
        url: `/merchants/${accID}/legal-documents/${documentID}`,
        method: "GET",
      }),
    { enabled: !!accID && !!documentID, refetchOnWindowFocus: false },
  );
};
 
export const useDocumentVersion = (versionID?: string | number) => {
  const { data } = useGetMerchantById();
  const accID = data?.accID;
  return useQuery<DocumentVersion>(
    [LEGAL_DOC_KEYS.version, accID, versionID],
    () =>
      customInstance({
        url: `/merchants/${accID}/legal-documents/versions/${versionID}`,
        method: "GET",
      }),
    { enabled: !!accID && !!versionID, refetchOnWindowFocus: false },
  );
};
 
export const useVersionHistory = (documentID?: string | number) => {
  const { data } = useGetMerchantById();
  const accID = data?.accID;
  return useQuery<VersionHistoryItem[]>(
    [LEGAL_DOC_KEYS.history, accID, documentID],
    () =>
      // Endpoint returns a { total, data } envelope; unwrap to the array.
      customInstance({
        url: `/merchants/${accID}/legal-documents/${documentID}/versions`,
        method: "GET",
      }).then((res) => res?.data ?? []),
    { enabled: !!accID && !!documentID, refetchOnWindowFocus: false },
  );
};