"use client" import { zodResolver } from "@hookform/resolvers/zod" import { format } from "date-fns" import { de, enUS } from "date-fns/locale" import { CalendarIcon } from "lucide-react" import { useForm } from "react-hook-form" import { z } from "zod" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Calendar } from "@/components/ui/calendar" import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover" import { toast } from "sonner" import { useRouter } from "next/navigation" interface CreatePlanFormProps { dict: any; lang: string; } export function CreatePlanForm({ dict, lang }: CreatePlanFormProps) { const router = useRouter() const formSchema = z.object({ dateRange: z.object({ from: z.date(), to: z.date(), }), password: z.string().min(4, { message: dict.passwordError, }), instructions: z.string().optional(), }) const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { password: "", instructions: "", }, }) const dateLocale = lang === "de" ? de : enUS async function onSubmit(values: z.infer) { try { const response = await fetch("/api/plan", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ startDate: values.dateRange.from, endDate: values.dateRange.to, password: values.password, instructions: values.instructions, }), }) if (!response.ok) { throw new Error("Failed to create plan") } const data = await response.json() toast.success(dict.success) router.push(`/${lang}/dashboard/${data.planId}`) } catch (error) { toast.error(dict.error) console.error(error) } } return (
( {dict.travelDates} date < new Date(new Date().setHours(0, 0, 0, 0)) } initialFocus locale={dateLocale} /> {dict.dateRangeDesc} )} /> ( {dict.groupPassword} {dict.passwordDesc} )} /> ) }