Fixed event-dialog
This commit is contained in:
@@ -23,7 +23,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { KeyedMutator } from "swr";
|
||||
import { getCookie } from "cookies-next";
|
||||
import { useTheme } from "next-themes";
|
||||
import { EventForm, eventFormSchema, EventFormValues } from "./event-dialog";
|
||||
import { EventForm, EventFormValues } from "./event-dialog";
|
||||
import ICalendarEvent from "@/interfaces/ICalendarEvent";
|
||||
import { UseFormReturn } from "react-hook-form";
|
||||
import mapFrequencyToRrule from "@/lib/mapFrequencyToRrule";
|
||||
@@ -67,6 +67,8 @@ const Planning: React.FC<{
|
||||
});
|
||||
if (res.status === "Error") {
|
||||
// calendar?.events?.update(oldEvent);
|
||||
} else {
|
||||
mutate?.();
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
@@ -145,6 +147,7 @@ const Planning: React.FC<{
|
||||
parseInt(eHours),
|
||||
parseInt(eMinutes),
|
||||
);
|
||||
console.log(formValues.endDate);
|
||||
const event: Omit<ICalendarEvent, "id"> = {
|
||||
...newEvent,
|
||||
start: formValues.startDate.toISOString(),
|
||||
@@ -185,7 +188,7 @@ const Planning: React.FC<{
|
||||
`/events/${id}/delete`,
|
||||
{
|
||||
method: "DELETE",
|
||||
requiresAuth: false,
|
||||
requiresAuth: true,
|
||||
csrfToken: false,
|
||||
},
|
||||
);
|
||||
@@ -245,17 +248,9 @@ const EventDialog: React.FC<
|
||||
> = ({ open, onOpenChange, onDelete, onUpdate, onAdd, event }) => {
|
||||
const [form, setForm] = useState<UseFormReturn<EventFormValues>>();
|
||||
|
||||
const handleOnAdd = (data: EventFormValues) => {
|
||||
onAdd?.(data);
|
||||
};
|
||||
|
||||
const onSubmit = (data: EventFormValues) => {
|
||||
try {
|
||||
const validatedData = eventFormSchema.parse(data);
|
||||
handleOnAdd(data);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
const submitForm = (event: "add" | "update") => {
|
||||
const callback = event === "add" ? onAdd : onUpdate;
|
||||
if (callback) form?.handleSubmit(callback)();
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -265,24 +260,12 @@ const EventDialog: React.FC<
|
||||
<DialogTitle>{event.title}</DialogTitle>
|
||||
<DialogDescription>{event.description}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<EventForm event={event} onSubmit={onSubmit} />
|
||||
<EventForm event={event} setForm={setForm} />
|
||||
<DialogFooter className="flex flex-row justify-end">
|
||||
{onUpdate && (
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
form?.handleSubmit((data) => {
|
||||
console.log(data);
|
||||
try {
|
||||
const validatedData =
|
||||
eventFormSchema.parse(data);
|
||||
console.log(validatedData);
|
||||
onUpdate(validatedData);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
}}
|
||||
onClick={() => submitForm("update")}
|
||||
type="submit"
|
||||
>
|
||||
Actualiser
|
||||
@@ -298,10 +281,7 @@ const EventDialog: React.FC<
|
||||
</Button>
|
||||
)}
|
||||
{onAdd && !onUpdate && !onDelete && (
|
||||
<Button
|
||||
onClick={() => form?.handleSubmit((data) => {})}
|
||||
type="submit"
|
||||
>
|
||||
<Button onClick={() => submitForm("add")} type="submit">
|
||||
Créer
|
||||
</Button>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user