All files / src/components/ProfilePage/EnterpriseAgreement EnterpriseAgreement.tsx

92.68% Statements 38/41
81.25% Branches 13/16
100% Functions 12/12
97.36% Lines 37/38

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                                            19x 29x   29x 29x 29x 29x 29x   29x             29x 5x                     29x 29x 7x 7x 7x 7x                   29x           29x 29x     29x 6x 1x 1x 1x             29x   1x 1x 1x               29x   5x 5x 1x               29x 6x                     29x                                   29x                                                                           29x                                                        
import KotoStepper from "@common/SignUp/KotoStepper";
import { Box, Stack } from "@mui/material";
import React, { useCallback, useMemo, useRef, useState, useEffect } from "react";
import {
  ProfileSetupFormActions,
  ProfileSetupFormContainer,
} from "../form.components";
import { HiddenComponent } from "@containers/HiddenComponent";
import { MerchantType } from "@customTypes/merchant.preview.types";
import useEnterpriseAgreement from "./useEnterpriseAgreement";
import {
  primaryAccountHolderParser,
  merchantAgreementParser,
  useMerchantInfoParser,
} from "@components/Merchants/MerchantPreview/helpers/parsers";
import GiveProviderAgreementContent from "@components/Merchants/MerchantPreview/MerchantAgreement/GiveProviderAgreementContent";
 
type Props = {
  backLink: () => void;
  data?: MerchantType["merchant"];
};
//AGREEMENT used in enterprise onboarding
const EnterpriseAgreement = ({ backLink, data }: Props) => {
  const { merchantInfoParser } = useMerchantInfoParser();
 
  const [step, setStep] = useState(StepsEnum.AGREEMENT);
  const [isLoaded, setIsLoaded] = useState<boolean>(false);
  const [agreementInView, setAgreementInView] = useState<boolean>(false);
  const stepperRef = useRef<any>(null);
  const signatureSectionRef = useRef<HTMLDivElement>(null);
 
  const [statusBar, setStatusBar] = useState([
    {
      label: StepsEnum.AGREEMENT,
      barValue: 0,
    },
  ]);
 
  const merchantData = useMemo(
    () => ({
      merchantInfo: merchantInfoParser(data),
      primaryAccountHolder: primaryAccountHolderParser({ data: data?.owner }),
      merchantAgreement: merchantAgreementParser(data),
      status: {
        statusName: "pending",
      },
    }),
    [data],
  );
 
  const handleUpdateStatusValue = useCallback(
    (label: string) => (value: number) => {
      setStatusBar((prev) =>
        prev.map((s) => {
          Iif (s.label !== label) return s;
          return {
            ...s,
            barValue: value,
          };
        }),
      );
    },
    [],
  );
 
  const { methods, onSubmit, isDisabled } = useEnterpriseAgreement(
    handleUpdateStatusValue(StepsEnum.AGREEMENT),
    merchantData,
    backLink,
  );
 
  const { setValue, getValues, watch } = methods;
  const signatureValue = watch("signature");
 
  // Auto-scroll to signature section when user reaches bottom of agreement
  useEffect(() => {
    if (agreementInView && signatureSectionRef.current) {
      setTimeout(() => {
        const element = signatureSectionRef.current;
        Iif (element && typeof element.scrollIntoView === "function") {
          element.scrollIntoView({ behavior: "smooth" });
        }
      }, 100);
    }
  }, [agreementInView]);
 
  const handleAddSignature = useCallback(
    (file: File) => {
      const current = getValues("signature");
      Iif (current === file) return;
      setValue("signature", file, {
        shouldDirty: true,
        shouldValidate: true,
      });
    },
    [getValues, setValue],
  );
 
  const handleEnabledAgreementCheckboxChange = useCallback(
    (enabled: boolean) => {
      const current = getValues("hasAgreed");
      if (current === enabled) return;
      setValue("hasAgreed", enabled, {
        shouldDirty: true,
        shouldValidate: true,
      });
    },
    [getValues, setValue],
  );
 
  const agreementContentData = useMemo(
    () => ({
      ...merchantData,
      merchantAgreement: {
        ...merchantData.merchantAgreement,
        // Used by GiveProviderAgreementContent to detect a locally-added (not-yet-uploaded) signature
        signatureFile: signatureValue,
      } as any,
    }),
    [merchantData, signatureValue],
  );
 
  const content = [
    {
      node: (
        <GiveProviderAgreementContent
          data={agreementContentData as any}
          addSignature={handleAddSignature}
          onEnabledAgreementCheckboxChange={
            handleEnabledAgreementCheckboxChange
          }
          isOnboarding
          signatureSectionRef={signatureSectionRef}
           tosMinHeight="400px"
        />
      ),
      visible: step === StepsEnum.AGREEMENT && isLoaded,
    },
  ];
 
  return (
    <Stack
      direction="column"
      justifyContent="stretch"
      height="inherit"
      width="100%"
    >
      <Stack
        direction="row"
        alignItems="center"
        gap={2}
        sx={{
          position: "sticky",
          top: 0,
          zIndex: 10,
          backgroundColor: "background.default",
          paddingBottom: 2,
        }}
      >
        <KotoStepper
          sx={{ flexGrow: 1 }}
          setStep={setStep}
          defaultStep={StepsEnum.AGREEMENT}
          customSteps={statusBar}
          ref={stepperRef}
          setIsLoaded={setIsLoaded}
        />
      </Stack>
      <Box
        component="form"
        flexGrow={1}
        id="enterprise-agreement-form"
        display="flex"
        onSubmit={methods.handleSubmit(onSubmit)}
      >
        <ProfileSetupFormContainer>
          <Stack direction="column" gap={4} height="min-content">
            {content.map(({ node, visible }, index) => (
              <HiddenComponent key={index} hidden={!visible}>
                {React.cloneElement(node, {
                  onAgreementInViewChange: setAgreementInView,
                } as any)}
              </HiddenComponent>
            ))}
          </Stack>
          <ProfileSetupFormActions
            secondaryAction={{
              onClick: backLink,
            }}
            primaryAction={{
              disabled: isDisabled,
              children: "Finish",
              form: "enterprise-agreement-form",
            }}
          />
        </ProfileSetupFormContainer>
      </Box>
    </Stack>
  );
};
 
export default EnterpriseAgreement;
 
enum StepsEnum {
  AGREEMENT = "Agreement",
}