"use client"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { useState } from "react"; import Link from "next/link"; import { inquirySchema, type InquiryInput } from "@/lib/validations"; import { Input, Textarea, Label, FieldError } from "@/components/ui/Input"; import { Button } from "@/components/ui/Button"; import { cn } from "@/lib/utils"; interface ApartmentOption { slug: string; name: string; } interface Props { apartments: ApartmentOption[]; defaultSlug?: string; defaultArrival?: string; defaultDeparture?: string; } export function InquiryForm({ apartments, defaultSlug, defaultArrival, defaultDeparture }: Props) { const [state, setState] = useState<"idle" | "loading" | "success" | "error">("idle"); const [serverError, setServerError] = useState(null); const { register, handleSubmit, reset, formState: { errors }, } = useForm({ resolver: zodResolver(inquirySchema), defaultValues: { apartmentSlug: defaultSlug && apartments.some((a) => a.slug === defaultSlug) ? defaultSlug : apartments[0]?.slug, arrival: defaultArrival ?? "", departure: defaultDeparture ?? "", guests: 2, gdpr: false as unknown as true, website: "", }, }); async function onSubmit(values: InquiryInput) { setState("loading"); setServerError(null); try { const res = await fetch("/api/inquiries", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(values), }); if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.error ?? "Anfrage konnte nicht gesendet werden."); } reset(); setState("success"); window.scrollTo({ top: 0, behavior: "smooth" }); } catch (err) { setState("error"); setServerError(err instanceof Error ? err.message : "Unbekannter Fehler."); } } if (state === "success") { return (
Danke!

Ihre Anfrage ist bei uns.

Wir haben Ihnen eine Bestätigung per E-Mail geschickt und melden uns in der Regel innerhalb von 24 Stunden.

Zur Startseite
); } return (
{/* Honeypot */} {/* Wohnung */}
{/* Datum + Gäste */}

{/* Kontakt */}
{/* Nachricht */}