"use client"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; const schema = z.object({ name: z.string().min(1, "Name required").max(100), description: z.string().max(300).optional(), }); type FormData = z.infer; interface Props { shootDayId: string; open: boolean; onOpenChange: (open: boolean) => void; onCreated: (setup: { id: string; name: string }) => Promise | void; } export function NewSetupDialog({ shootDayId, open, onOpenChange, onCreated }: Props) { const [loading, setLoading] = useState(false); const { register, handleSubmit, reset, formState: { errors } } = useForm({ resolver: zodResolver(schema), defaultValues: { name: "" }, }); async function onSubmit(data: FormData) { setLoading(true); try { const res = await fetch(`/api/shoot-log/days/${shootDayId}/setups`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }); if (!res.ok) throw new Error("Failed to create setup"); const { setup } = await res.json(); onCreated(setup); reset(); onOpenChange(false); } finally { setLoading(false); } } return ( New Setup
{errors.name &&

{errors.name.message}

}
); }