All files / src/features/Merchants/MerchantSidePanel/Modals InvitePAHModal.tsx

78.37% Statements 29/37
58.62% Branches 17/29
60% Functions 6/10
80% Lines 28/35

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 224 225 226 227 228 229                                                                2x   99x 99x 99x 99x         99x   99x     99x   99x       144x 136x           99x           99x                     99x 99x   99x 7x     99x 21x             99x         99x             99x 7x             7x 7x     99x   99x         99x                                                                                                                                                                                                                  
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import { Box, Divider, Grid, Stack } from "@mui/material";
import { yupResolver } from "@hookform/resolvers/yup";
import * as Yup from "yup";
import { useEffect } from "react";
import { useUpdateMerchantInfo } from "@components/Merchants/MerchantPreview/hooks/useUpdateMerchantInfo";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import GiveButton from "@shared/Button/GiveButton";
import GiveText from "@shared/Text/GiveText";
import HFGiveSwitch from "@shared/HFInputs/HFGiveSwitch/HFGiveSwitch";
import { useAppTheme } from "@theme/v2/Provider";
import useUserReducer from "@hooks/Reducers/useUserReducer";
import { IdentificationBadgeIcon } from "@phosphor-icons/react";
import { SAVE_CHANGES_MODAL } from "modals/modal_names";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
 
type FormInputs = {
  email: string;
  invite: boolean;
  assignToMe: boolean;
};
 
type Props = {
  data: FormInputs;
  onClose: (data?: FormInputs) => void;
  merchantId?: number;
  isEdit?: boolean;
  isEnterprise?: boolean;
};
 
const InvitePAHModal = NiceModal.create(
  ({ data, merchantId, onClose, isEdit = false, isEnterprise }: Props) => {
    const modal = useModal();
    const { isMobileView } = useCustomThemeV2();
    const open = modal.visible;
    const { handleSubmit, isLoading } = useUpdateMerchantInfo(
      merchantId || 0,
      undefined,
    );
 
    const user = useUserReducer();
 
    const name = [user.globalName.firstName, user.globalName.lastName].join(
      " ",
    );
    const dynamicPlaceholder = [name, user.email].join(" - ");
 
    const schema = Yup.object({
      email: Yup.string()
        .email("Email not valid")
        .when("assignToMe", {
          is: (value: boolean) => !value,
          then: (schema) => schema.required("Enter account holder email"),
        }),
      invite: Yup.boolean(),
      assignToMe: Yup.boolean(),
    });
 
    const defaultValues = {
      email: "",
      invite: true,
      assignToMe: false,
    };
 
    const methods = useForm<FormInputs>({
      mode: "onChange",
      resolver: yupResolver(schema),
      defaultValues,
    });
 
    const {
      reset,
      formState: { isValid, isDirty },
      watch,
      clearErrors,
    } = methods;
    const values = watch();
 
    useEffect(() => {
      clearErrors("email");
    }, [data.assignToMe]);
 
    useEffect(() => {
      reset({
        invite: true,
        email: data.email,
        assignToMe: data.assignToMe,
      });
    }, [data, modal.visible]);
 
    const handleCancel = () => {
      reset();
      modal.hide();
    };
 
    const handleOpenModal = () =>
      NiceModal.show(SAVE_CHANGES_MODAL, {
        modalName: "Primary Account Holder",
        handleCancel,
        handleSave: () => onSubmit(values),
      });
 
    const onSubmit: SubmitHandler<FormInputs> = async (data) => {
      Iif (merchantId) {
        await handleSubmit("primary_account_holder", {
          ...data,
          invite: isEdit,
          inviteOwner: !data.assignToMe,
        });
      }
      Eif (onClose) onClose(data);
      modal.hide();
    };
 
    const theme = useAppTheme();
 
    const handleExitModal = () => {
      if (isValid && isDirty) return handleOpenModal();
      handleCancel();
    };
 
    return (
      <GiveBaseModal
        open={open}
        title={`${isEdit ? "Edit " : ""}Primary Account Holder`}
        width={isEdit ? "546px" : "720px"}
        {...(isMobileView && {
          height: isEdit ? "40%" : "73%",
        })}
        onClose={handleExitModal}
        headerLeftContent={<IdentificationBadgeIcon />}
        buttons={
          <Stack gap="12px" flexDirection="row">
            <GiveButton
              onClick={handleCancel}
              label="Cancel"
              variant="ghost"
              size="large"
            />
            <GiveButton
              label={"Invite"}
              variant="filled"
              size="large"
              disabled={!isValid || isLoading || (isEdit && !isDirty)}
              type="submit"
              form="edit-primary-account-holder"
              sx={{ border: "none" }}
              data-testid="submit-pah-button"
            />
          </Stack>
        }
        closeIconProps={{
          bgColor: "solidWhite",
        }}
      >
        {!isEdit && (
          <GiveText pb="24px" variant="bodyS">
            The Primary Account Holder has full control over the payment
            account, including the ability to add users, manage funds, and
            update settings.
          </GiveText>
        )}
        <FormProvider {...methods}>
          <Box
            component="form"
            id="edit-primary-account-holder"
            onSubmit={methods.handleSubmit(onSubmit)}
          >
            {!isEdit && (
              <>
                <Stack width="100%">
                  <Stack
                    sx={{
                      backgroundColor: theme.palette.surface?.secondary,
                      borderRadius: theme.spacing(1.5),
                      padding: theme.spacing(2),
                    }}
                    direction="row"
                    gap={1.5}
                  >
                    <HFGiveSwitch
                      name="assignToMe"
                      size="small"
                      inputProps={{
                        ["data-testid" as string]: "assign-to-me-switch",
                      }}
                      disabled={!!values.email}
                    />
                    <Stack>
                      <GiveText variant="bodyS" color="primary">
                        I am the Primary Account Holder
                      </GiveText>
                      <GiveText variant="bodyS" color="secondary">
                        {dynamicPlaceholder} will be assigned as the PAH of this
                        {isEnterprise ? " provider" : " merchant"}
                      </GiveText>
                    </Stack>
                  </Stack>
                </Stack>
                <Divider sx={{ marginY: 1.5 }}>
                  <GiveText variant="bodyS" color="secondary">
                    OR
                  </GiveText>
                </Divider>
              </>
            )}
 
            <Grid container spacing={2}>
              <Grid item xs={12}>
                <HFGiveInput
                  name="email"
                  label="Email"
                  placeholder=""
                  fullWidth
                  disabled={Boolean(values.assignToMe)}
                />
              </Grid>
            </Grid>
          </Box>
        </FormProvider>
      </GiveBaseModal>
    );
  },
);
 
export default InvitePAHModal;