All files / src/components/Merchants/MerchantPreview/modals GiveCreateMerchantAgreementModal.tsx

67.39% Statements 31/46
61.53% Branches 24/39
55.55% Functions 5/9
71.42% Lines 30/42

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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223                                                  2x 10x                 2x   10x 10x 10x 10x             10x     10x       10x         10x 20x             10x 10x                         10x 3x 3x 3x                       10x                 10x                                                                     10x         10x   10x       10x   10x 6x 3x 3x 3x                     10x                                                                                                            
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import { FormProvider, useWatch } from "react-hook-form";
import { useAddMerchantAgreement } from "@components/Merchants/CreateMerchantPanel/hooks/useAddMerchantAgreement";
import { TMerchantAgreementPayload } from "@components/Merchants/CreateMerchantPanel/hooks/TMerchantAgreementPayload";
import { IParsedData } from "@components/Merchants/MerchantPreview/data.types";
import GiveButton from "@shared/Button/GiveButton";
import { Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { useCallback, useEffect, useMemo, useState } from "react";
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { CertificateIcon } from "@phosphor-icons/react";
import GiveProviderAgreementContent from "../MerchantAgreement/GiveProviderAgreementContent";
import GiveMerchantAgreementContent from "../MerchantAgreement/GiveMerchantAgreementContent";
 
interface ModalProps {
  onClose: (data: TMerchantAgreementPayload) => void;
  data?: TMerchantAgreementPayload & {
    isEnterprise?: boolean;
  };
  merchantId?: number;
  merchantName?: string;
  merchantData?: Partial<IParsedData>;
}
 
const titleComponent = (isEnterprise: boolean) => (
  <Stack gap="8px" alignItems="center" flexDirection="row">
    <CertificateIcon size={22} />
    <GiveText color="primary" variant="bodyS">
      {isEnterprise ? "Provider" : "Merchant"} Agreement
    </GiveText>
  </Stack>
);
 
//AGREEMENT used in merchant/provider creation
const GiveCreateMerchantAgreementModal = NiceModal.create(
  ({ onClose, data, merchantId, merchantName, merchantData }: ModalProps) => {
    const modal = useModal();
    const open = modal.visible;
    const { isMobileView } = useCustomThemeV2();
    const isEnterprise = data?.isEnterprise || false;
 
    const {
      methods,
      isSubmitButtonDisabled,
      handleSubmit,
      getDefaultValuesFromData,
    } = useAddMerchantAgreement(onClose, data, merchantId, isEnterprise);
 
    //state to hold the Agreement Confirmation checkbox value from provider child component
    const [enabledAgreementCheckbox, setEnabledAgreementCheckbox] = useState(
      Boolean(data?.msaHadAgreed),
    );
 
    const [formSignatureFile] = useWatch({
      control: methods.control,
      name: ["signatureFile", "msaHadAgreed"],
    });
 
    const getFile = (value: any): File | undefined => {
      Eif (!value) return undefined;
      if (value instanceof File) return value;
      if (value?.file instanceof File) return value.file;
      return undefined;
    };
 
    const signatureFile =
      getFile(formSignatureFile) ?? getFile(data?.signatureFile);
    const addSignature = (file: File) => {
      methods?.setValue("signatureFile", file, {
        shouldValidate: true,
        shouldDirty: true,
      });
      // Signing implies agreement acceptance; required by validation schema.
      methods?.setValue("msaHadAgreed", true, {
        shouldValidate: true,
        shouldDirty: true,
      });
    };
 
    //this function is to handle the type error in the content component
    const agreementContentData = useMemo(() => {
      Iif (!merchantData && !data) return undefined;
      const baseMerchantAgreement = merchantData?.merchantAgreement ?? data;
      return {
        ...(merchantData ?? {}),
        merchantAgreement: baseMerchantAgreement
          ? ({
              ...baseMerchantAgreement,
              ...(signatureFile ? { signatureFile } : {}), //pass the local file, when signed from creation panel
            } as any)
          : undefined,
      } satisfies Partial<IParsedData>;
    }, [merchantData, data, signatureFile]);
 
    //state to hold the Agreement Confirmation checkbox value from merchant child component
    const [radioState, setRadioState] = useState({
      isPciChecked: Boolean(
        agreementContentData?.merchantAgreement?.msaPCICompliant,
      ),
      isPreviousTerminationChecked: Boolean(
        agreementContentData?.merchantAgreement?.msaPreviousTermination,
      ),
    });
 
    const setRadioButtons = useCallback(
      (
        updater:
          | {
              isPciChecked: boolean;
              isPreviousTerminationChecked: boolean;
            }
          | ((prev: {
              isPciChecked: boolean;
              isPreviousTerminationChecked: boolean;
            }) => {
              isPciChecked: boolean;
              isPreviousTerminationChecked: boolean;
            }),
      ) => {
        setRadioState((prev) => {
          const next = typeof updater === "function" ? updater(prev) : updater;
          methods.setValue("msaPCICompliant", next.isPciChecked, {
            shouldValidate: true,
            shouldDirty: true,
          });
          methods.setValue(
            "msaPreviousTermination",
            next.isPreviousTerminationChecked,
            {
              shouldValidate: true,
              shouldDirty: true,
            },
          );
          return next;
        });
      },
      [methods],
    );
 
    const handleClose = () => {
      methods.reset();
      modal.remove();
    };
 
    const isMerchantPciChecked = Boolean(radioState.isPciChecked);
 
    const isCheckboxChecked = isEnterprise
      ? enabledAgreementCheckbox
      : isMerchantPciChecked;
    const isSaveDisabled =
      isSubmitButtonDisabled || !isCheckboxChecked || !signatureFile;
 
    useEffect(() => {
      if (open && data) {
        methods.reset(getDefaultValuesFromData(data));
        setEnabledAgreementCheckbox(Boolean(data?.msaHadAgreed));
        setRadioState({
          isPciChecked: Boolean(
            agreementContentData?.merchantAgreement?.msaPCICompliant,
          ),
          isPreviousTerminationChecked: Boolean(
            agreementContentData?.merchantAgreement?.msaPreviousTermination,
          ),
        });
      }
    }, [open, data?.msaLastAcceptedVersion]); // Only reset when modal opens or data version changes
 
    return (
      <GiveBaseModal
        data-testid="give-agreement-modal"
        open={open}
        onClose={handleClose}
        title={titleComponent(isEnterprise) as any}
        width={isMobileView ? "100%" : "auto"}
        buttons={
          <GiveButton
            label="Save"
            size="large"
            variant="filled"
            type="submit"
            form="create-agreement-form"
            sx={{
              width: "auto !important",
            }}
            data-testid="agreement-submit-btn"
            disabled={isSaveDisabled}
          />
        }
      >
        <FormProvider {...methods}>
          <form
            id="create-agreement-form"
            onSubmit={methods.handleSubmit(handleSubmit)}
          >
            <Stack gap="20px">
              {isEnterprise ? (
                <GiveProviderAgreementContent
                  data={agreementContentData}
                  addSignature={addSignature}
                  onEnabledAgreementCheckboxChange={setEnabledAgreementCheckbox}
                  tosMinHeight="400px"
                />
              ) : (
                <GiveMerchantAgreementContent
                  data={agreementContentData}
                  addSignature={addSignature}
                  isOnboarding
                  radioState={radioState}
                  setRadioButtons={setRadioButtons}
                  canBePending={false}
                />
              )}
            </Stack>
          </form>
        </FormProvider>
      </GiveBaseModal>
    );
  },
);
 
export default GiveCreateMerchantAgreementModal;