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 | import NiceModal, { useModal } from "@ebay/nice-modal-react";
import ModalDrawer from "@common/Modal/ModalDrawer/ModalDrawer";
import { Box, Stack } from "@mui/material";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import { TPaymentFormModalInputs } from "./types";
import { yupResolver } from "@hookform/resolvers/yup";
import {
PaymentFormModalSchema as schema,
PaymentFormModalDefaults as defaultValues,
} from "./utils";
import { useEffect } from "react";
import StyleSection from "../StyleSection";
import ConfigurationSection from "../ConfigurationSection";
import { useCreateProduct } from "../hooks";
import { stringToEditorState } from "@utils/draft.editor.helpers";
import { useQueryClient } from "react-query";
import EditProductSkeleton from "../EditProductSkeleton";
import PaymentFormsAbout from "../FundraisersAbout";
import PaymentFormsPayment from "./PaymentFormsPayment";
import { useFindPaymentFormById } from "@hooks/payment-forms/useFindPaymentFormById";
type EditPaymentFormModalProps = {
id?: string;
handleClose?: () => void;
noModal?: boolean;
};
export const EditPaymentForm = ({
id,
handleClose,
noModal,
}: EditPaymentFormModalProps) => {
const queryClient = useQueryClient();
const { data, isLoading } = useFindPaymentFormById(id);
const { submitProduct, isDisabled } = useCreateProduct("payment-form", {
id,
noModal,
});
const methods = useForm<TPaymentFormModalInputs>({
resolver: yupResolver(schema),
defaultValues,
});
const {
reset,
formState: { isDirty },
} = methods;
useEffect(() => {
if (data && !isLoading) {
const formattedList = data.payment.amountsList.map((amount: any) => {
if (amount.min_max) {
return {
...amount,
description: stringToEditorState(amount.description),
};
}
return {
...amount,
description: amount.description?.text || "",
};
});
reset({
about: {
title: data.general.title,
description: data.general.description,
},
style: {
image: data.general.featuredImage.image,
useAsBackground: data.general.featuredImage.useAsBackground,
},
payment_set_up: {
payment_types: data.payment.payment_types,
amountsList: formattedList,
},
configuration: {
customer_pays_fees: data.payment.customer_pays_credit.active,
},
});
}
}, [isLoading]);
const onClose = () => {
reset();
if (handleClose) {
handleClose();
queryClient.removeQueries("find-payment-form-by-id");
}
};
const onSubmit: SubmitHandler<TPaymentFormModalInputs> = async (data) => {
submitProduct(data, onClose);
};
return (
<ModalDrawer
onModalClose={onClose}
HeaderProps={{
title: "Edit",
}}
primaryAction={{
label: "Save changes",
type: "submit",
disabled: !isDirty || isDisabled,
form: "edit-payment-form",
}}
isBody={noModal}
>
<FormProvider {...methods}>
<Box
component="form"
flexGrow={1}
display="flex"
id="edit-payment-form"
onSubmit={methods.handleSubmit(onSubmit)}
>
{isLoading ? (
<EditProductSkeleton />
) : (
<Stack direction="column" gap={4} flexGrow={1}>
<PaymentFormsAbout
title="About"
titleSize="medium"
placeHolderText="What’s the purpose of this Payment Form?"
isEdit
/>
<StyleSection title="Customize" titleSize="medium" isEdit />
<PaymentFormsPayment
title="Payment set up"
/>
<ConfigurationSection
title="Configuration"
titleSize="medium"
isEdit
/>
</Stack>
)}
</Box>
</FormProvider>
</ModalDrawer>
);
};
const EditPaymentFormModal = NiceModal.create(
({ id }: EditPaymentFormModalProps) => {
const modal = useModal();
const handleClose = () => {
modal.hide();
};
return <EditPaymentForm id={id} handleClose={handleClose} />;
},
);
export default EditPaymentFormModal;
|