"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { validateContact } from "@/lib/contacts"; type Contact = { id: number; name: string; email: string | null; telegramChatId: string | null }; export default function ContactsPage() { const [contacts, setContacts] = useState([]); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [telegramChatId, setTelegramChatId] = useState(""); const [errors, setErrors] = useState([]); async function load() { const res = await fetch("/api/contacts"); if (res.ok) setContacts(await res.json()); } useEffect(() => { load(); }, []); async function onSubmit(e: React.FormEvent) { e.preventDefault(); const input = { name, email: email || null, telegramChatId: telegramChatId || null }; const errs = validateContact(input); setErrors(errs); if (errs.length) return; const res = await fetch("/api/contacts", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(input), }); if (!res.ok) { const data = await res.json().catch(() => ({})); setErrors(data.errors ?? ["speichern"]); return; } setName(""); setEmail(""); setTelegramChatId(""); setErrors([]); load(); } return (
← Zurück

Kontakte

setName(e.target.value)} className="mt-1 w-full rounded border px-3 py-2" />
setEmail(e.target.value)} className="mt-1 w-full rounded border px-3 py-2" />
setTelegramChatId(e.target.value)} className="mt-1 w-full rounded border px-3 py-2" />
{errors.length > 0 &&

Ungültig: {errors.join(", ")}

}
    {contacts.map((c) => (
  • {c.name} {c.email && · {c.email}} {c.telegramChatId && · Telegram {c.telegramChatId}}
  • ))}
); }