All files / src/features/Products/Invoices/modals ResendOverdueInvoiceModal.tsx

100% Statements 11/11
50% Branches 1/2
100% Functions 3/3
100% Lines 10/10

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                                1x       1x   6x 6x 6x       6x   6x             6x 1x     6x                                                                                      
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { yupResolver } from "@hookform/resolvers/yup";
import * as Yup from "yup";
import { Stack } from "@mui/material";
import GiveButton from "@shared/Button/GiveButton";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import GiveText from "@shared/Text/GiveText";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import { useResendInvoice } from "../useResendInvoice";
 
type FormInputs = {
  notes: string;
};
 
const schema = Yup.object().shape({
  note: Yup.string(),
});
 
const ResendOverdueInvoiceModal = NiceModal.create(
  ({ id, reference }: { id: string; reference: string }) => {
    const modal = useModal();
    const handleCloseModal = () => modal.hide();
    const { mutateAsync, isLoading } = useResendInvoice({
      productId: id,
      onSuccessCb: handleCloseModal,
    });
    const { isMobileView } = useCustomThemeV2();
 
    const methods = useForm<FormInputs>({
      resolver: yupResolver(schema),
      defaultValues: {
        notes: `Your invoice ${reference} is overdue. Please make a payment at your earliest convenience to avoid any late fees`,
      },
    });
 
    const onSubmit: SubmitHandler<FormInputs> = async (data) => {
      await mutateAsync({ notes: data.notes });
    };
 
    return (
      <GiveBaseModal
        title="Resend Invoice"
        open={modal.visible}
        onClose={handleCloseModal}
        width={isMobileView ? "100%" : `560px`}
        buttons={
          <>
            <GiveButton
              size="large"
              variant="ghost"
              onClick={handleCloseModal}
              label="Cancel"
            />
            <GiveButton
              size="large"
              type="submit"
              label="Resend Invoice"
              form="resend-overdue-invoice"
              disabled={isLoading}
            />
          </>
        }
      >
        <FormProvider {...methods}>
          <Stack
            spacing={1.5}
            component="form"
            id="resend-overdue-invoice"
            onSubmit={methods.handleSubmit(onSubmit)}
          >
            <GiveText variant="bodyS" color="primary">
              Note (Optional)
            </GiveText>
            <HFGiveInput name="notes" maxLength={500} multiline rows={6} />
          </Stack>
        </FormProvider>
      </GiveBaseModal>
    );
  },
);
 
export default ResendOverdueInvoiceModal;