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

0% Statements 0/27
0% Branches 0/8
0% Functions 0/7
0% Lines 0/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 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                                                                                                                                                                                                                                                                                                                                                                   
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import { Box } from "@mui/material";
import { yupResolver } from "@hookform/resolvers/yup";
import * as Yup from "yup";
import { RHFInput } from "@common/Input";
import { Button } from "@common/Button";
import { useEffect } from "react";
import EditMerchantBaseModal from "../components/EditMerchantBaseModal";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { MedalIcon } from "@assets/icons/RebrandedIcons";
import { useMutation, useQueryClient } from "react-query";
import { customInstance } from "@services/api";
import { showMessage } from "@common/Toast/ShowToast";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import { VALIDATION_MESSAGES } from "@validation/messages";
 
type FormInputs = {
  email: string;
};
 
const schema = Yup.object().shape({
  email: Yup.string()
    .email(VALIDATION_MESSAGES.INVALID_EMAIL)
    .required(VALIDATION_MESSAGES.ACCOUNT_HOLDER_EMAIL_REQUIRED),
});
 
type Props = {
  data: {
    email: string;
  };
  ownerAccId: number;
  id: number;
};
 
const UpdatePrimaryAcccountHolderEmail = NiceModal.create(
  ({ data, ownerAccId, id }: Props) => {
    const modal = useModal();
    const open = modal.visible;
    const { isMobileView } = useCustomTheme();
    const queryClient = useQueryClient();
    const { isDesktopView } = useCustomTheme();
 
    const { mutate, isLoading } = useMutation((data: any) => {
      return customInstance({
        url: `/users/${ownerAccId}`,
        method: "PATCH",
        data,
      });
    });
    const methods = useForm<FormInputs>({
      resolver: yupResolver(schema),
      defaultValues: {
        email: data?.email,
      },
    });
 
    const {
      reset,
      formState: { isDirty },
    } = methods;
 
    useEffect(() => {
      reset({ email: data?.email });
    }, [data]);
 
    const handleCancel = () => {
      reset();
      modal.remove();
    };
 
    const onSubmit: SubmitHandler<FormInputs> = async (data) => {
      mutate(
        { email: data.email },
        {
          onError: (err: any) => {
            const errMessage = err.response?.data?.input[0]?.message;
            console.log("ERROR", errMessage);
            if (errMessage == 'The provided "email" value is already taken.') {
              showMessage(
                "Error",
                "This email address is already in use, please try again with a another email",
                isDesktopView,
              );
              return;
            }
          },
          onSuccess: () => {
            // TODO: show correct message once it's provided
            showMessage("Info", "Validation email has been sent");
            queryClient.invalidateQueries([
              MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
              id,
            ]);
            modal.hide();
          },
        },
      );
    };
 
    return (
      <EditMerchantBaseModal
        title="Primary Account Holder"
        description="An invitation email will be sent to complete the merchant
        creation process. They will have full control over the payment
        account, including the ability to add users, manage funds, and
        update settings."
        open={open}
        handleCancel={handleCancel}
        icon={<MedalIcon />}
        PaperProps={{
          style: {
            top: "25%",
          },
          sx: {
            "& .MuiDialogTitle-root + .MuiDialogContent-root": {
              paddingTop: "0 !important",
              paddingBottom: "0 !important",
            },
          },
        }}
        actionsSx={{
          padding: "24px",
        }}
        actions={
          <>
            <Button
              size="medium"
              background="tertiary"
              onClick={handleCancel}
              sx={{
                ...(isMobileView && {
                  width: "50%",
                }),
              }}
            >
              Cancel
            </Button>
            <Button
              size="medium"
              background="primary"
              type="submit"
              form="edit-primary-account-holder"
              disabled={!isDirty || isLoading}
              sx={{
                marginLeft: "0px !important",
                ...(isMobileView && {
                  width: "50%",
                }),
              }}
            >
              Send
            </Button>
          </>
        }
      >
        <FormProvider {...methods}>
          <Box
            component="form"
            id="edit-primary-account-holder"
            onSubmit={methods.handleSubmit(onSubmit)}
            sx={{ marginTop: 2 }}
          >
            <RHFInput
              name="email"
              label="Email"
              placeholder="Email"
              fullWidth
            />
          </Box>
        </FormProvider>
      </EditMerchantBaseModal>
    );
  },
);
 
export default UpdatePrimaryAcccountHolderEmail;