"use client"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { format } from "date-fns"; import { CalendarIcon, 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({ date: z.string().min(1, "Date is required"), unit: z.string().max(20).default("A"), label: z.string().max(120).optional(), notes: z.string().optional(), }); type FormData = z.infer; interface Props { projectId: string; open: boolean; onOpenChange: (open: boolean) => void; onCreate: (day: { id: string; date: string; unit: string; label: string | null }) => void; } export function NewShootDayDialog({ projectId, open, onOpenChange, onCreate }: Props) { const [loading, setLoading] = useState(false); const { register, handleSubmit, reset, formState: { errors }, } = useForm({ resolver: zodResolver(schema), defaultValues: { date: format(new Date(), "yyyy-MM-dd"), unit: "A", }, }); async function onSubmit(data: FormData) { setLoading(true); try { const res = await fetch("/api/shoot-log/days", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ projectId, ...data }), }); if (!res.ok) throw new Error("Failed to create shoot day"); const { day } = await res.json(); onCreate(day); reset({ date: format(new Date(), "yyyy-MM-dd"), unit: "A" }); onOpenChange(false); } finally { setLoading(false); } } return ( New Shoot Day
{errors.date && (

{errors.date.message}

)}
); }