All files / src/components/Merchants/BulkInvite/modals GiveEditInviteModal.tsx

82.35% Statements 28/34
46.42% Branches 13/28
80% Functions 8/10
83.87% Lines 26/31

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                                                                2x               60x 60x   60x 34x 33x     60x         60x                     60x   60x 60x   60x 9x       9x     60x 1x 1x     60x 1x     60x 9x                                 60x                                                                                                                                   2x     2x 257x     51x              
import { useEffect } from "react";
import { Box, Stack } from "@mui/material";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import NiceModal from "@ebay/nice-modal-react";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import GiveButton from "@shared/Button/GiveButton";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { styled } from "@theme/v2/Provider";
import { TInviteElement } from "../types";
import { VALIDATION_MESSAGES } from "@validation/messages";
import { InviteFormFields } from "../components/InviteFormFields";
import { getInviteSchema } from "../utils/validation";
 
type TInputs = {
  merchantName: string;
  pahEmail: string;
};
 
type Props = {
  errorType?: "email" | "merchantName";
  merchantName?: string;
  pahEmail?: string;
  checkIsUnique: (
    merchantName: string,
    originalMerchantName?: string,
  ) => boolean;
  handleDelete: (cb?: VoidFunction) => void;
  handleSubmit: (newValue: Partial<TInviteElement>) => void;
};
 
const GiveEditInviteBase = ({
  merchantName,
  pahEmail,
  checkIsUnique,
  handleDelete,
  handleSubmit,
  errorType,
}: Props) => {
  const { open, onClose } = useNiceModal();
  const { isMobileView } = useCustomThemeV2();
 
  const schema = getInviteSchema((value) => {
    if (!value) return false;
    return checkIsUnique(value, merchantName);
  });
 
  const defaultValues: TInputs = {
    merchantName: merchantName || "",
    pahEmail: pahEmail || "",
  };
 
  const methods = useForm<TInputs>({
    resolver: yupResolver(schema),
    defaultValues,
    mode: "onChange",
  });
 
  const {
    trigger,
    formState: { isDirty, isValid, errors },
    setError,
    watch,
  } = methods;
 
  const watchedEmail = watch("pahEmail");
  const watchedMerchantName = watch("merchantName");
 
  useEffect(() => {
    methods.reset({
      merchantName: merchantName || "",
      pahEmail: pahEmail || "",
    });
    trigger();
  }, [merchantName, pahEmail]);
 
  const onSubmit: SubmitHandler<TInputs> = (data) => {
    handleSubmit({ merchantName: data.merchantName, pahEmail: data.pahEmail });
    onClose();
  };
 
  const onDelete = () => {
    handleDelete(onClose);
  };
 
  useEffect(() => {
    Eif (!errorType) return;
 
    const timeoutId = setTimeout(() => {
      const errorField = errorType === "email" ? "pahEmail" : "merchantName";
      const errorMessage =
        errorType === "email"
          ? VALIDATION_MESSAGES.PROVIDE_EMAIL
          : !merchantName
          ? VALIDATION_MESSAGES.PROVIDE_MERCHANT_NAME
          : VALIDATION_MESSAGES.UNIQUE_MERCHANT_NAME;
 
      setError(errorField, { message: errorMessage });
    }, 1);
 
    return () => clearTimeout(timeoutId);
  }, [errorType, merchantName, pahEmail]);
 
  return (
    <GiveBaseModal
      open={open}
      onClose={onClose}
      title="Edit Invitation"
      width={isMobileView ? "100%" : "720px"}
      height="fit-content"
      showFooter
      buttons={
        <Stack
          direction="row"
          justifyContent="space-between"
          alignItems="center"
          width="100%"
        >
          <GiveButton
            variant="ghost"
            color="destructive"
            size="large"
            onClick={onDelete}
            label="Delete"
            sx={{
              width: "auto !important",
            }}
          />
          <Stack direction="row" gap={1.5} alignItems="center">
            <GiveButton
              variant="ghost"
              size="large"
              onClick={onClose}
              label="Cancel"
            />
            <GiveButton
              variant="filled"
              size="large"
              type="submit"
              form="edit-invitation"
              disabled={!isDirty || !isValid}
              label="Save"
            />
          </Stack>
        </Stack>
      }
    >
      <FormProvider {...methods}>
        <InputFormContainer
          component="form"
          id="edit-invitation"
          onSubmit={methods.handleSubmit(onSubmit)}
          isMobileView={isMobileView}
        >
          <InviteFormFields
            isMobileView={isMobileView}
            errors={errors}
            values={{
              pahEmail: watchedEmail,
              merchantName: watchedMerchantName,
            }}
            setValue={methods.setValue}
          />
        </InputFormContainer>
      </FormProvider>
    </GiveBaseModal>
  );
};
 
const GiveEditInviteModal = NiceModal.create(GiveEditInviteBase);
export default GiveEditInviteModal;
 
const InputFormContainer = styled(Box, {
  shouldForwardProp: (prop) => prop !== "isMobileView",
})<{
  isMobileView: boolean;
}>(({ isMobileView }) => ({
  display: "flex",
  flexDirection: isMobileView ? "column" : "row",
  gap: isMobileView ? "20px" : "12px",
  alignItems: isMobileView ? "stretch" : "flex-start",
  paddingTop: 0,
}));