import { zodResolver } from '@hookform/resolvers/zod' import { useState } from 'react' import { useForm } from 'react-hook-form' import { z } from 'zod' import { Alert, AlertDescription } from '@/components/ui/alert' import { Button } from '@/components/ui/button' import { Field, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field' import { Input } from '@/components/ui/input' import { useCreateList } from '@/hooks/use-lists' import { ApiError } from '@/lib/api-client' const createListSchema = z.object({ name: z.string().trim().min(1, 'El nombre es obligatorio').max(80, 'Máximo 80 caracteres'), }) type CreateListValues = z.infer type CreateListFormProps = { onCreated?: () => void } /** Inline form to create a shopping list. Used by the lists page. */ export function CreateListForm({ onCreated }: CreateListFormProps) { const [error, setError] = useState(null) const createList = useCreateList() const form = useForm({ resolver: zodResolver(createListSchema), defaultValues: { name: '' }, }) const onSubmit = form.handleSubmit((values) => { setError(null) createList.mutate(values, { onSuccess: () => { form.reset() onCreated?.() }, onError: (mutationError) => { setError( mutationError instanceof ApiError ? (mutationError.problem.detail ?? mutationError.problem.title) : 'No pudimos crear la lista.', ) }, }) }) return (
{error && ( {error} )} Nombre de la lista
) }