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 | import NiceModal, { useModal } from "@ebay/nice-modal-react";
import ModalDrawer from "@common/Modal/ModalDrawer/ModalDrawer";
import { Box, Stack } from "@mui/material";
import LoadingSpinner from "@components/Snipper/LoadingSpinner";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import { TEventModalInputs } from "./types";
import { yupResolver } from "@hookform/resolvers/yup";
import {
EventModalSchema as schema,
EventModalDefaults as defaultValues,
validateURL,
} from "./utils";
import FundraisersAbout from "../FundraisersAbout";
import { useEffect, useRef } from "react";
import StyleSection from "../StyleSection";
import { useCreateProduct } from "../hooks";
import { useFindEventById } from "@hooks/merchant-api/events";
import EventDateLocation from "./EventDateLocation";
import EventPayment from "./EventPayment";
import EventConfiguration from "./EventConfiguration";
import { useQueryClient } from "react-query";
import EditProductSkeleton from "../EditProductSkeleton";
type EditEventModalProps = {
id?: string;
handleClose?: () => void;
noModal?: boolean;
};
export const EditEvent = ({
id,
handleClose,
noModal,
}: EditEventModalProps) => {
const queryClient = useQueryClient();
const { data, isLoading } = useFindEventById(id);
const { submitProduct, isDisabled } = useCreateProduct("event", {
id,
noModal,
});
const isMounted = useRef<boolean>(false);
const methods = useForm<TEventModalInputs>({
resolver: yupResolver(schema),
defaultValues,
});
const {
reset,
formState: { isDirty },
} = methods;
useEffect(() => {
if (data && !isLoading) {
const formattedList = data.tickets.list.map((ticket: any) => {
return {
...ticket,
title: ticket.name,
display: Boolean(ticket.display),
description: ticket.description?.text || "",
};
});
reset({
about: {
title: data.general.title,
description: data.general.description,
},
style: {
image: data.general.featuredImage.image,
useAsBackground: data.general.featuredImage.useAsBackground,
},
date_and_location: data.date_and_location,
payment_set_up: {
payment_types: {
default: "one_time",
one_time: true,
},
amountsList: formattedList,
},
configuration: {
customer_pays_fees: data.tickets.customer_pays_credit.active,
maxTickets: data.tickets.customer_pays_credit.maxTickets,
},
});
isMounted.current = true;
}
}, [isLoading]);
const onClose = () => {
reset();
if (handleClose) {
handleClose();
queryClient.removeQueries("find-event-by-id");
}
isMounted.current = false;
isMounted.current = false;
};
const onSubmit: SubmitHandler<TEventModalInputs> = async (data) => {
const formattedData = {
...data,
date_and_location: {
...data.date_and_location,
location: {
...data.date_and_location.location,
event_url: data.date_and_location.location.event_url
? validateURL(data.date_and_location.location.event_url)
: "",
},
},
};
submitProduct(formattedData, onClose);
};
return (
<ModalDrawer
onModalClose={onClose}
HeaderProps={{
title: "Edit",
}}
primaryAction={{
label: "Save changes",
type: "submit",
disabled: !isDirty || isDisabled,
form: "edit-event",
}}
isBody={noModal}
>
<FormProvider {...methods}>
<Box
component="form"
flexGrow={1}
display="flex"
id="edit-event"
onSubmit={methods.handleSubmit(onSubmit)}
>
{isLoading ? (
<EditProductSkeleton />
) : (
<Stack direction="column" gap={4} flexGrow={1}>
<FundraisersAbout title="About" titleSize="medium" isEdit />
<StyleSection title="Customize" titleSize="medium" isEdit />
<EventDateLocation isModalMounted={isMounted.current} />
<EventPayment title="Set up a range of payment choices" />
<EventConfiguration title="Configuration" />
</Stack>
)}
</Box>
</FormProvider>
</ModalDrawer>
);
};
const EditEventModal = NiceModal.create(({ id }: EditEventModalProps) => {
const modal = useModal();
const handleClose = () => {
modal.hide();
};
return <EditEvent id={id} handleClose={handleClose} />;
});
export default EditEventModal;
|