"use client"; import * as React from "react"; import { CalendarIcon } from "lucide-react"; import { format } from "date-fns"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Calendar } from "@/components/ui/calendar"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { useForm } from "react-hook-form"; import { useEffect } from "react"; import ICalendarEvent from "@/interfaces/ICalendarEvent"; import { Command, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem, } from "@/components/ui/command"; import { Textarea } from "@/components/ui/textarea"; import { useApi } from "@/hooks/use-api"; import { Location } from "@/types/types"; import openNavigationApp from "@/lib/openNavigationMap"; import formatLocation from "@/lib/formatLocation"; export const eventFormSchema = z.object({ title: z.string().min(1, "Titre requis"), startDate: z.date({ required_error: "Date de début requise" }), startTime: z.string(), endDate: z.date({ required_error: "Date finale requise" }), endTime: z.string(), fullDay: z.boolean().default(false), frequency: z.enum(["unique", "quotidien", "hebdomadaire", "mensuel"]), frequencyEndDate: z.date().optional(), isVisible: z.boolean().default(true), description: z.string().optional(), location: z.string().optional(), // Store as a formatted string }); export type EventFormValues = z.infer; const frequencies = [ { label: "Unique", value: "unique" }, { label: "Quotidien", value: "quotidien" }, { label: "Hebdomadaire", value: "hebdomadaire" }, { label: "Mensuel", value: "mensuel" }, ]; export const EventForm: React.FC<{ event: ICalendarEvent | Omit; setForm: React.Dispatch< React.SetStateAction< ReturnType> | undefined > >; }> = ({ event, setForm }) => { const locations = useApi("/locations/all"); const _start = new Date(event.start ?? Date.now()); const _end = new Date(event.end ?? Date.now()); const form = useForm({ resolver: zodResolver(eventFormSchema), defaultValues: { title: event.title || "", startDate: _start, startTime: format(_start, "HH:mm"), endDate: _end, endTime: format(_end, "HH:mm"), fullDay: event.fullday ?? false, frequency: "unique", isVisible: event.isVisible ?? true, location: event.location, description: event.description, }, }); useEffect(() => { setForm(form); }, [form, setForm]); const frequency = form.watch("frequency"); return (
( Titre )} />
{/* Start Date */} Début
{ if (date) { form.setValue( "startDate", date, { shouldValidate: true }, ); } }} initialFocus />
( )} /> Until {/* End Date */} Fin
{ if (date) { form.setValue("endDate", date, { shouldValidate: true, }); } }} initialFocus />
( )} />
( Journée complète )} />
( Fréquence )} /> {frequency !== "unique" && ( Jusqu'au
{ if (date) { form.setValue( "frequencyEndDate", date, { shouldValidate: true, }, ); } }} initialFocus />
)}
( Évènement visible ? )} /> {/* Updated Location Field with Command */} ( Lieu field.onChange(value) } /> {locations.isLoading && ( Chargement... )} {!locations.isLoading && !locations.data?.length && ( Aucun lieu trouvé. )} {!locations.isLoading && locations.data?.length && ( {locations.data .filter((location) => formatLocation( location, ) .toLowerCase() .includes( ( field.value || "" ).toLowerCase(), ), ) .map((location) => ( { const formatted = formatLocation( location, ); field.onChange( formatted, ); }} > {formatLocation( location, )} ))} )} )} /> ( Description