"use client" import { zodResolver } from "@hookform/resolvers/zod" import { format } from "date-fns" 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" const formSchema = z.object({ dateRange: z.object({ from: z.date(), to: z.date(), }), password: z.string().min(4, { message: "Password must be at least 4 characters.", }), instructions: z.string().optional(), }) export function CreatePlanForm() { const router = useRouter() const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { password: "", instructions: "", }, }) 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("Plan created successfully!") router.push(`/dashboard/${data.planId}`) } catch (error) { toast.error("Something went wrong. Please try again.") console.error(error) } } return (
( Travel Dates date < new Date(new Date().setHours(0, 0, 0, 0)) } initialFocus /> Select the days you will be away. )} /> ( Group Password Share this password with your cat sitters. )} /> ) }