All files / src/features/TransactionPanel/modals ResendReceiptModalV2.tsx

80% Statements 20/25
57.14% Branches 8/14
66.66% Functions 6/9
90.9% Lines 20/22

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                                          1x             4x 4x         4x         4x         3x 3x           3x 3x 3x         4x                     4x 4x     4x       4x 1x                         4x 2x     4x                                                                               8x                                                                                    
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import NiceModal from "@ebay/nice-modal-react";
import { Stack } from "@mui/material";
import GiveButton from "@shared/Button/GiveButton";
import GiveEmailInputWithTags from "@shared/InputTags/GiveEmailInputWithTags";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import GiveRadio from "@shared/Radio/GiveRadio";
import GiveText from "@shared/Text/GiveText";
import { FormProvider, useForm } from "react-hook-form";
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import { useResendReceipt } from "../hooks/useResendReceipt";
import { useEffect, useMemo } from "react";
 
type Props = {
  isChargebackReversal: boolean;
  transactionID: string;
  customerID: number;
  sourceAccountEmail: string;
};
 
const ResendReceiptModalV2 = NiceModal.create(
  ({
    customerID,
    isChargebackReversal,
    transactionID,
    sourceAccountEmail,
  }: Props) => {
    const { open, onClose } = useNiceModal();
    const { handleSubmit, isLoading } = useResendReceipt({
      customerID,
      isChargebackReversal,
      transactionID,
    });
    const defaultValues = {
      recipient: "original",
      emails: [],
    };
 
    const schema = yup.object().shape({
      emails: yup
        .array()
        .of(yup.string().email("Invalid email address in the list"))
        .test("empty-string", "Invalid email address in the list", (emails) => {
          Iif (!emails) return true;
          else return !emails.some((email) => email === "");
        })
        .test(
          "unique-emails",
          "Duplicate email addresses in the list",
          (emails) => {
            Iif (!emails) return true;
            const uniqueEmails = new Set(emails);
            return uniqueEmails.size === emails.length;
          },
        ),
    });
 
    const methods = useForm({
      mode: "onChange",
      resolver: yupResolver(schema),
      defaultValues: defaultValues,
    });
 
    const {
      watch,
      setValue,
      reset,
      formState: { isValid },
    } = methods;
    const values = watch();
 
    const disabledSubmit =
      !isValid ||
      isLoading ||
      (values.recipient === "others" && !values.emails.length);
 
    const radios = useMemo(() => {
      return [
        {
          type: "original",
          label: "Original transaction recipient: ",
          description: sourceAccountEmail,
        },
        {
          type: "others",
          label: "Send to other recipient(s)",
        },
      ];
    }, [sourceAccountEmail]);
 
    useEffect(() => {
      reset(defaultValues);
    }, [open]);
 
    return (
      <GiveBaseModal
        open={open}
        title="Resend Receipt"
        width="560px"
        height="fit-content"
        onClose={onClose}
        buttons={
          <>
            <GiveButton
              variant="ghost"
              size="large"
              label="Cancel"
              onClick={onClose}
              disabled={isLoading}
            />
            <GiveButton
              size="large"
              variant="filled"
              label="Send"
              onClick={() =>
                handleSubmit({
                  recipients:
                    values.recipient === "original" ? [] : values.emails,
                })
              }
              disabled={disabledSubmit}
              sx={{ border: "none" }}
            />
          </>
        }
      >
        <GiveText variant="bodyS" mb={2}>
          Please confirm if you would like to send the email receipt to the
          original recipient or different email address.
        </GiveText>
        <FormProvider {...methods}>
          <Stack spacing={1.5}>
            <Stack spacing={2}>
              {radios.map((radio) => (
                <Stack
                  key={radio.type}
                  direction="row"
                  spacing={1.5}
                  alignItems="flex-start"
                >
                  <GiveRadio
                    onClick={() => setValue("recipient", radio.type)}
                    checked={values.recipient === radio.type}
                  />
                  <Stack>
                    <GiveText variant="bodyS" color="secondary">
                      {radio.label}
                    </GiveText>
                    {radio.description && (
                      <GiveText variant="bodyS" fontWeight="medium">
                        {radio.description}
                      </GiveText>
                    )}
                  </Stack>
                </Stack>
              ))}
            </Stack>
            {values.recipient === "others" && (
              <GiveEmailInputWithTags
                name="emails"
                chipProps={{ color: "blue" }}
                inputProps={{
                  placeholder: "Enter email",
                  label: "",
                  sx: { "& .MuiInputBase-root": { padding: "6px 12px" } },
                }}
              />
            )}
          </Stack>
        </FormProvider>
      </GiveBaseModal>
    );
  },
);
 
export default ResendReceiptModalV2;