All files / src/features/Merchants/MerchantSidePanel/AgreementAndSnapshot/Agreement/wraper AgreementStatusWrapper.tsx

88.88% Statements 24/27
78.57% Branches 22/28
100% Functions 3/3
88.88% Lines 24/27

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                                                    54x 54x 54x 54x 29x                                                     29x   29x       29x                         29x   29x 8x 21x 20x   1x   54x 54x               54x 54x   54x         54x 114x   114x       114x                                                 54x                
import React, { useMemo, useState } from "react";
import { useMerchantSidePanelContext } from "../../../Provider/MerchantSidePanelProvider";
import moment from "moment";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
import { Stack } from "@mui/material";
import { AgreementState as Status, StatusInfo } from "../../agreements.types";
import { checkPortals } from "@utils/routing";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { TMerchantBaseContextData } from "@components/Merchants/MerchantPreview/data.types";
 
// Define the props for the AgreementStatusWrapper
interface AgreementStatusWrapperProps {
  children: React.ReactNode;
  defaultContext?: TMerchantBaseContextData;
  p?: string;
  isEnterprise?: boolean;
}
 
// The AgreementStatusWrapper component
function AgreementStatusWrapper({
  children,
  defaultContext,
  p = "20px",
  isEnterprise = false,
}: AgreementStatusWrapperProps) {
  // Retrieve data from props or context only when needed
  const context = useMerchantSidePanelContext();
  const { data } = defaultContext ?? context;
  const merchantAgreement = data?.merchantAgreement;
  const statusInfo: StatusInfo = useMemo(() => {
    Iif (!merchantAgreement)
      return {
        msaLastAcceptedVersion: null,
        tcVersion: null,
        signedOn: null,
        status: "not_signed" as Status,
        signedBy: null,
        signatureURL: null,
        msaPreviousTerminationProcessorName: null,
        msaPreviousTerminationAt: null,
        msaPreviousTerminationReason: null,
        msaPreviousTermination: false,
        msaPCICompliant: false,
      };
 
    const {
      msaLastAcceptedVersion,
      tcVersion,
      msaHadAgreed,
      signedBy,
      msaLastUpdatedAt,
      signatureURL,
      msaPreviousTerminationProcessorName,
      msaPreviousTerminationAt,
      msaPreviousTerminationReason,
      msaPreviousTermination,
      msaPCICompliant,
    } = merchantAgreement;
 
    const signedOn = msaLastUpdatedAt
      ? moment.unix(msaLastUpdatedAt).tz(PLATFORM_TIMEZONE).format("MM/DD/YYYY")
      : null;
 
    const defaultPayload = {
      msaLastAcceptedVersion,
      tcVersion,
      signedBy,
      signedOn,
      signatureURL,
      msaPreviousTerminationProcessorName,
      msaPreviousTerminationAt,
      msaPreviousTerminationReason,
      msaPreviousTermination,
      msaPCICompliant,
    };
 
    const isCorrectVersion = msaLastAcceptedVersion === tcVersion;
 
    if (msaHadAgreed && isCorrectVersion)
      return { ...defaultPayload, status: "signed" as Status };
    if ((!msaHadAgreed && isCorrectVersion) || !msaLastAcceptedVersion)
      return { ...defaultPayload, status: "not_signed" as Status };
 
    return { ...defaultPayload, status: "newVersion" as Status };
  }, [merchantAgreement]);
  const isNewVersion = statusInfo.status === "newVersion";
  const [radioState, setRadioButtons] = useState({
    isPciChecked: isNewVersion
      ? false
      : data?.merchantAgreement?.msaPCICompliant,
    isPreviousTerminationChecked:
      data?.merchantAgreement?.msaPreviousTermination,
  });
 
  const { agreement_signing } = useEnterprisePermissions();
  const { isEnterprisePortal } = checkPortals();
  const isHide =
    statusInfo.status === "not_signed" &&
    !agreement_signing &&
    isEnterprisePortal;
 
  // Clone children and pass statusInfo as a prop
  const childrenWithStatusInfo = React.Children.map(children, (child) => {
    Eif (React.isValidElement(child)) {
      // Some children can be hidden based on the permissions
      Iif (isHide && child.props.canBeHidden) {
        return null; // This child will not be rendered
      }
      // Clone the child and add statusInfo prop
      return React.cloneElement(child, {
        statusInfo,
        radioState,
        setRadioButtons,
        defaultContext,
        isEnterprise,
      } as {
        statusInfo: StatusInfo;
        radioState: {
          isPciChecked: boolean;
          isPreviousTerminationChecked: boolean;
        };
        defaultContext?: TMerchantBaseContextData;
        isEnterprise?: boolean;
        setRadioButtons: React.Dispatch<
          React.SetStateAction<{
            isPciChecked: boolean;
            isPreviousTerminationChecked: boolean;
          }>
        >;
      });
    }
    return child; // Return child as is if it's not a valid React element
  });
 
  return (
    <Stack p={p} gap="20px">
      {childrenWithStatusInfo}
    </Stack>
  );
}
 
export default AgreementStatusWrapper;