All files / src/features/Merchants/MerchantSidePanel/AgreementAndSnapshot/Agreement/components PDFLink.tsx

80.95% Statements 17/21
50% Branches 4/8
100% Functions 3/3
89.47% Lines 17/19

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 92                                          59x         21x   21x               21x 21x   21x     21x     21x       21x   21x 2x     21x 2x 2x 2x                         2x       21x                                
import { pdf } from "@react-pdf/renderer";
import { IParsedData } from "@components/Merchants/MerchantPreview/data.types";
import { DownloadSimpleIcon } from "@phosphor-icons/react";
import { useAppTheme } from "@theme/v2/Provider";
import MerchantAgreementPDF from "@components/Merchants/CreateMerchantPanel/modals/components/MerchantAgreement/pdf";
import { saveAs } from "file-saver";
import { showMessage } from "@common/Toast";
import GiveButton from "@shared/Button/GiveButton";
import { DOWNLOAD_MERCHANT_AGREEMENT } from "@components/Merchants/CreateMerchantPanel/modals/utils";
import { StatusInfo } from "../../agreements.types";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { usePublishedDocumentByType } from "@pages/AcquirerPortal/LegalDocuments/api/usePublishedDocumentByType";
import { formatPublishedDate } from "@pages/AcquirerPortal/LegalDocuments/publishedDate";
 
interface IComponentProps {
  snapshotData: any;
  isSnapshotLoading: boolean;
  statusInfo?: StatusInfo;
}
 
const PDFLink = ({
  snapshotData,
  isSnapshotLoading,
  statusInfo,
}: IComponentProps) => {
  const { palette } = useAppTheme();
  const { isMerchantProcessorEnabled, isDocumentManagementEnabled } =
    useGetFeatureFlagValues();
 
  // The version/date stamped on the PDF come from the acquirer's PUBLISHED
  // legal document, the same source MerchantAgreementModal renders on screen
  // (its real version + published date, fetched with the current account's OWN
  // accID — the backend resolves the hierarchy). Falls back to the legacy
  // signed-version fields inside MerchantAgreementPDF when nothing is
  // published (or the Legal Documents feature is off).
  const { data: currentMerchant } = useGetMerchantById();
  const accountID = currentMerchant?.accID;
  const { data: publishedDoc, isResolving: isDocResolving } =
    usePublishedDocumentByType(accountID, "merchant_agreement", {
      enabled: isDocumentManagementEnabled,
    });
  const publishedVersion = publishedDoc?.version;
  // published_at is a calendar date (midnight UTC) — format in UTC, or the
  // PDF stamps the previous day. See publishedDate.ts.
  const publishedLastUpdated = publishedDoc?.publishedDate
    ? formatPublishedDate(publishedDoc.publishedDate, "MMMM dd, yyyy")
    : undefined;
 
  const isPdfDataLoading = isSnapshotLoading || isDocResolving;
 
  const showErrorToast = () => {
    showMessage("Error", "An Error has occurred, please Try again later");
  };
 
  const handleDownloadPdf = async () => {
    Iif (isPdfDataLoading) return;
    try {
      const blob = await pdf(
        <MerchantAgreementPDF
          statusInfo={statusInfo}
          data={snapshotData as IParsedData}
          isMerchantProcessorEnabled={isMerchantProcessorEnabled}
          publishedVersion={publishedVersion}
          publishedLastUpdated={publishedLastUpdated}
        />,
      ).toBlob();
 
      if (blob) saveAs(blob, "merchant_agreement.pdf");
      else showErrorToast();
    } catch (e) {
      showErrorToast();
    }
  };
 
  return (
    <GiveButton
      disabled={isPdfDataLoading}
      startIcon={
        <DownloadSimpleIcon fill={palette.icon?.["icon-primary"]} size={18} />
      }
      onClick={handleDownloadPdf}
      label={DOWNLOAD_MERCHANT_AGREEMENT}
      size="large"
      fullWidth
      color="light"
    />
  );
};
 
export default PDFLink;