"use client"; import { useState, useTransition } from "react"; import Button from "@/components/ui/Button"; import Input from "@/components/ui/Input"; import Textarea from "@/components/ui/Textarea"; import Alert from "@/components/ui/Alert"; import { createHouse } from "@/actions/houses/create"; import { updateHouse } from "@/actions/houses/update"; interface HouseFormProps { house?: { id: string; name: string; description: string | null; }; onSuccess?: () => void; onCancel?: () => void; } export default function HouseForm({ house, onSuccess, onCancel, }: HouseFormProps) { const [name, setName] = useState(house?.name || ""); const [description, setDescription] = useState(house?.description || ""); const [error, setError] = useState(null); const [isPending, startTransition] = useTransition(); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); setError(null); startTransition(async () => { const result = house ? await updateHouse(house.id, { name, description: description || null }) : await createHouse({ name, description: description || null }); if (result.success) { onSuccess?.(); } else { setError(result.error || "Une erreur est survenue"); } }); }; return (
{error && {error}} setName(e.target.value)} required minLength={3} maxLength={50} disabled={isPending} />