import { zodResolver } from "@hookform/resolvers/zod"; import { Plus } from "lucide-react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { useCreateVolume } from "~/hooks/useCreateVolume"; import { slugify } from "~/lib/utils"; import { Button } from "./ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "./ui/dialog"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "./ui/form"; import { Input } from "./ui/input"; type Props = { open: boolean; setOpen: (open: boolean) => void; }; const formSchema = z.object({ name: z .string() .min(2, { message: "Volume name must be at least 2 characters long", }) .max(32, { message: "Volume name must be at most 32 characters long", }), }); export const CreateVolumeDialog = ({ open, setOpen }: Props) => { const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { name: "", }, }); const nameValue = form.watch("name"); const createVolume = useCreateVolume(); const onSubmit = (values: { name: string }) => { createVolume.mutate(values, { onSuccess: () => { form.reset(); setOpen(false); }, }); }; return ( Create volume Enter a name for the new volume.
( Name field.onChange(slugify(e.target.value))} max={32} min={1} /> Unique identifier for the volume. )} /> {createVolume.error && (
{createVolume.error.message}
)}
); };