Fixed event-dialog
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
|
||||||
|
fmt.Print(" [up migration] ")
|
||||||
|
_, err := db.NewDropColumn().
|
||||||
|
Model((*models.Event)(nil)).
|
||||||
|
ColumnExpr("status").
|
||||||
|
Exec(ctx)
|
||||||
|
return err
|
||||||
|
}, func(ctx context.Context, db *bun.DB) error {
|
||||||
|
fmt.Print(" [down migration] ")
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -73,8 +73,10 @@ const isCalendarEventExternal = (
|
|||||||
|
|
||||||
export const EventForm: React.FC<{
|
export const EventForm: React.FC<{
|
||||||
event: ICalendarEvent | Omit<ICalendarEvent, "id">;
|
event: ICalendarEvent | Omit<ICalendarEvent, "id">;
|
||||||
onSubmit: (data: EventFormValues) => any;
|
setForm: React.Dispatch<
|
||||||
}> = ({ event, onSubmit }) => {
|
React.SetStateAction<UseFormReturn<EventFormValues> | undefined>
|
||||||
|
>;
|
||||||
|
}> = ({ event, setForm }) => {
|
||||||
const _start = new Date(event.start ?? Date.now());
|
const _start = new Date(event.start ?? Date.now());
|
||||||
const _end = new Date(event.end ?? Date.now());
|
const _end = new Date(event.end ?? Date.now());
|
||||||
const form = useForm<EventFormValues>({
|
const form = useForm<EventFormValues>({
|
||||||
@@ -91,56 +93,15 @@ export const EventForm: React.FC<{
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setForm(form);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const frequency = form.watch("frequency");
|
const frequency = form.watch("frequency");
|
||||||
|
|
||||||
// async function onSubmit(data: EventFormValues) {
|
|
||||||
// try {
|
|
||||||
// const validatedData = eventFormSchema.parse(data);
|
|
||||||
// onSubmitEvent(validatedData);
|
|
||||||
// } catch (error) {
|
|
||||||
// console.error("On submit error : ", error);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// useEffect(() => {
|
|
||||||
// try {
|
|
||||||
// const validatedData = eventFormSchema.parse(form);
|
|
||||||
// setEvent((old) => {
|
|
||||||
// const rrule = mapFrequencyToRrule(
|
|
||||||
// validatedData.frequency,
|
|
||||||
// validatedData.frequencyEndDate,
|
|
||||||
// );
|
|
||||||
// const [sHours, sMinutes] = validatedData.startTime.split(":");
|
|
||||||
// validatedData.startDate.setHours(
|
|
||||||
// parseInt(sHours),
|
|
||||||
// parseInt(sMinutes),
|
|
||||||
// );
|
|
||||||
// const [eHours, eMinutes] = validatedData.endTime.split(":");
|
|
||||||
// validatedData.endDate.setHours(
|
|
||||||
// parseInt(eHours),
|
|
||||||
// parseInt(eMinutes),
|
|
||||||
// );
|
|
||||||
// return {
|
|
||||||
// ...old,
|
|
||||||
// start: validatedData.startDate.toISOString(),
|
|
||||||
// end: validatedData.endDate.toISOString(),
|
|
||||||
// title: `${validatedData.title}`,
|
|
||||||
// fullDay: validatedData.fullDay,
|
|
||||||
// rrule: rrule,
|
|
||||||
// isVisible: validatedData.isVisible,
|
|
||||||
// };
|
|
||||||
// });
|
|
||||||
// } catch (e) {
|
|
||||||
// console.log(e);
|
|
||||||
// }
|
|
||||||
// }, [form]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form
|
<form className="w-full max-w-md space-y-4">
|
||||||
onSubmit={form.handleSubmit(onSubmit)}
|
|
||||||
className="w-full max-w-md space-y-4"
|
|
||||||
>
|
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="title"
|
name="title"
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import { Button } from "@/components/ui/button";
|
|||||||
import { KeyedMutator } from "swr";
|
import { KeyedMutator } from "swr";
|
||||||
import { getCookie } from "cookies-next";
|
import { getCookie } from "cookies-next";
|
||||||
import { useTheme } from "next-themes";
|
import { useTheme } from "next-themes";
|
||||||
import { EventForm, eventFormSchema, EventFormValues } from "./event-dialog";
|
import { EventForm, EventFormValues } from "./event-dialog";
|
||||||
import ICalendarEvent from "@/interfaces/ICalendarEvent";
|
import ICalendarEvent from "@/interfaces/ICalendarEvent";
|
||||||
import { UseFormReturn } from "react-hook-form";
|
import { UseFormReturn } from "react-hook-form";
|
||||||
import mapFrequencyToRrule from "@/lib/mapFrequencyToRrule";
|
import mapFrequencyToRrule from "@/lib/mapFrequencyToRrule";
|
||||||
@@ -67,6 +67,8 @@ const Planning: React.FC<{
|
|||||||
});
|
});
|
||||||
if (res.status === "Error") {
|
if (res.status === "Error") {
|
||||||
// calendar?.events?.update(oldEvent);
|
// calendar?.events?.update(oldEvent);
|
||||||
|
} else {
|
||||||
|
mutate?.();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
@@ -145,6 +147,7 @@ const Planning: React.FC<{
|
|||||||
parseInt(eHours),
|
parseInt(eHours),
|
||||||
parseInt(eMinutes),
|
parseInt(eMinutes),
|
||||||
);
|
);
|
||||||
|
console.log(formValues.endDate);
|
||||||
const event: Omit<ICalendarEvent, "id"> = {
|
const event: Omit<ICalendarEvent, "id"> = {
|
||||||
...newEvent,
|
...newEvent,
|
||||||
start: formValues.startDate.toISOString(),
|
start: formValues.startDate.toISOString(),
|
||||||
@@ -185,7 +188,7 @@ const Planning: React.FC<{
|
|||||||
`/events/${id}/delete`,
|
`/events/${id}/delete`,
|
||||||
{
|
{
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
requiresAuth: false,
|
requiresAuth: true,
|
||||||
csrfToken: false,
|
csrfToken: false,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -245,17 +248,9 @@ const EventDialog: React.FC<
|
|||||||
> = ({ open, onOpenChange, onDelete, onUpdate, onAdd, event }) => {
|
> = ({ open, onOpenChange, onDelete, onUpdate, onAdd, event }) => {
|
||||||
const [form, setForm] = useState<UseFormReturn<EventFormValues>>();
|
const [form, setForm] = useState<UseFormReturn<EventFormValues>>();
|
||||||
|
|
||||||
const handleOnAdd = (data: EventFormValues) => {
|
const submitForm = (event: "add" | "update") => {
|
||||||
onAdd?.(data);
|
const callback = event === "add" ? onAdd : onUpdate;
|
||||||
};
|
if (callback) form?.handleSubmit(callback)();
|
||||||
|
|
||||||
const onSubmit = (data: EventFormValues) => {
|
|
||||||
try {
|
|
||||||
const validatedData = eventFormSchema.parse(data);
|
|
||||||
handleOnAdd(data);
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -265,24 +260,12 @@ const EventDialog: React.FC<
|
|||||||
<DialogTitle>{event.title}</DialogTitle>
|
<DialogTitle>{event.title}</DialogTitle>
|
||||||
<DialogDescription>{event.description}</DialogDescription>
|
<DialogDescription>{event.description}</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<EventForm event={event} onSubmit={onSubmit} />
|
<EventForm event={event} setForm={setForm} />
|
||||||
<DialogFooter className="flex flex-row justify-end">
|
<DialogFooter className="flex flex-row justify-end">
|
||||||
{onUpdate && (
|
{onUpdate && (
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
onClick={() => {
|
onClick={() => submitForm("update")}
|
||||||
form?.handleSubmit((data) => {
|
|
||||||
console.log(data);
|
|
||||||
try {
|
|
||||||
const validatedData =
|
|
||||||
eventFormSchema.parse(data);
|
|
||||||
console.log(validatedData);
|
|
||||||
onUpdate(validatedData);
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
type="submit"
|
type="submit"
|
||||||
>
|
>
|
||||||
Actualiser
|
Actualiser
|
||||||
@@ -298,10 +281,7 @@ const EventDialog: React.FC<
|
|||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{onAdd && !onUpdate && !onDelete && (
|
{onAdd && !onUpdate && !onDelete && (
|
||||||
<Button
|
<Button onClick={() => submitForm("add")} type="submit">
|
||||||
onClick={() => form?.handleSubmit((data) => {})}
|
|
||||||
type="submit"
|
|
||||||
>
|
|
||||||
Créer
|
Créer
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user