"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Image from "next/image"; import { Input, Label, Textarea, FieldError } from "@/components/ui/Input"; import { Button } from "@/components/ui/Button"; import { formatPrice } from "@/lib/utils"; export interface EditorApartment { id: string; slug: string; name: string; tagline: string; shortDescription: string; description: string; priceFrom: number; // Cent maxGuests: number; bedrooms: number; sizeSqm: number; features: string[]; images: string[]; airbnbUrl: string | null; bookingUrl: string | null; published: boolean; } export function ApartmentEditor({ apartment }: { apartment: EditorApartment }) { const router = useRouter(); const [form, setForm] = useState(apartment); const [priceEuro, setPriceEuro] = useState(String(Math.round(apartment.priceFrom / 100))); const [airbnbUrl, setAirbnbUrl] = useState(apartment.airbnbUrl ?? ""); const [bookingUrl, setBookingUrl] = useState(apartment.bookingUrl ?? ""); const [featureInput, setFeatureInput] = useState(""); const [imageInput, setImageInput] = useState(""); const [saving, setSaving] = useState(false); const [message, setMessage] = useState<{ kind: "ok" | "err"; text: string } | null>(null); function update(key: K, value: EditorApartment[K]) { setForm((f) => ({ ...f, [key]: value })); } function addFeature() { const v = featureInput.trim(); if (!v) return; update("features", [...form.features, v]); setFeatureInput(""); } function removeFeature(i: number) { update("features", form.features.filter((_, idx) => idx !== i)); } function addImage() { const v = imageInput.trim(); if (!v) return; try { new URL(v); } catch { setMessage({ kind: "err", text: "Bild-URL ist ungültig." }); return; } update("images", [...form.images, v]); setImageInput(""); setMessage(null); } function removeImage(i: number) { update("images", form.images.filter((_, idx) => idx !== i)); } async function save() { setSaving(true); setMessage(null); try { const res = await fetch(`/api/admin/apartments/${form.id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: form.name, tagline: form.tagline, shortDescription: form.shortDescription, description: form.description, priceFrom: Math.max(0, Math.round(Number(priceEuro) * 100)), maxGuests: Number(form.maxGuests), bedrooms: Number(form.bedrooms), sizeSqm: Number(form.sizeSqm), features: form.features, images: form.images, airbnbUrl: airbnbUrl.trim() || "", bookingUrl: bookingUrl.trim() || "", published: form.published, }), }); if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.error ?? "Speichern fehlgeschlagen."); } setMessage({ kind: "ok", text: "Gespeichert." }); router.refresh(); } catch (err) { setMessage({ kind: "err", text: err instanceof Error ? err.message : "Unbekannter Fehler.", }); } finally { setSaving(false); } } return (
/{form.slug}

{form.name}

ab {formatPrice(form.priceFrom)} · bis {form.maxGuests} Gäste · {form.sizeSqm} m²
update("name", e.target.value)} />
update("tagline", e.target.value)} />