350 lines
12 KiB
TypeScript
350 lines
12 KiB
TypeScript
"use client";
|
||
|
||
import { useState } from "react";
|
||
import { CircleAlert, CircleCheck, LoaderCircle } from "lucide-react";
|
||
|
||
interface OeffnungsZeit { tag: string; von: string; bis: string }
|
||
|
||
interface ContactProps {
|
||
telefon?: string;
|
||
email?: string;
|
||
adresseZeile1?: string;
|
||
adresseZeile2?: string;
|
||
oeffnungszeiten?: OeffnungsZeit[];
|
||
}
|
||
|
||
type Status = "idle" | "loading" | "success" | "error";
|
||
type Zielgruppe = "privat" | "geschaeftlich";
|
||
|
||
const themen: Record<Zielgruppe, string[]> = {
|
||
privat: [
|
||
"Mein WLAN ist langsam oder instabil",
|
||
"PC / Notebook läuft nicht richtig",
|
||
"Neuen Computer einrichten",
|
||
"Daten sichern / Fotos retten",
|
||
"Familienschutz (FamilyGuard)",
|
||
"Eigene Cloud (Nextcloud)",
|
||
"Sonstiges",
|
||
],
|
||
geschaeftlich: [
|
||
"IT-Betreuung & Support",
|
||
"Netzwerk & WLAN",
|
||
"Sicherer Fernzugriff (VPN)",
|
||
"Dokumentenarchiv (Nextcloud + Paperless)",
|
||
"Datensicherung & Backup",
|
||
"IT-Sicherheit & Datenschutz",
|
||
"Server, Cloud & Hosting",
|
||
"Webseite / Webanwendung",
|
||
"Sonstiges",
|
||
],
|
||
};
|
||
|
||
const inputClass = "form__input";
|
||
const labelClass = "form__label";
|
||
|
||
export default function Contact({
|
||
telefon = "+49 171 9345193",
|
||
email = "kontakt@mbo-tech-it.de",
|
||
adresseZeile1 = "Mörikestr. 2",
|
||
adresseZeile2 = "74564 Crailsheim",
|
||
oeffnungszeiten,
|
||
}: ContactProps = {}) {
|
||
const contactItems = [
|
||
{
|
||
label: "Telefon",
|
||
value: telefon,
|
||
href: `tel:${telefon.replace(/\s/g, "")}`,
|
||
},
|
||
{
|
||
label: "E-Mail",
|
||
value: email,
|
||
href: `mailto:${email}`,
|
||
},
|
||
{
|
||
label: "Standort",
|
||
value: `${adresseZeile1}, ${adresseZeile2}`,
|
||
href: `https://maps.google.com/?q=${encodeURIComponent(adresseZeile1 + " " + adresseZeile2)}`,
|
||
},
|
||
];
|
||
|
||
const [name, setName] = useState("");
|
||
const [emailInput, setEmailInput] = useState("");
|
||
const [zielgruppe, setZielgruppe] = useState<Zielgruppe>("privat");
|
||
const [firma, setFirma] = useState("");
|
||
const [telefonInput, setTelefonInput] = useState("");
|
||
const [betreff, setBetreff] = useState("");
|
||
const [nachricht, setNachricht] = useState("");
|
||
const [website, setWebsite] = useState(""); // Honeypot
|
||
const [startedAt, setStartedAt] = useState(() => Date.now());
|
||
const [status, setStatus] = useState<Status>("idle");
|
||
const [errorMsg, setErrorMsg] = useState("");
|
||
|
||
async function handleSubmit(e: React.FormEvent) {
|
||
e.preventDefault();
|
||
setStatus("loading");
|
||
setErrorMsg("");
|
||
|
||
try {
|
||
const res = await fetch("/api/contact", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({
|
||
website,
|
||
_t: startedAt,
|
||
name,
|
||
email: emailInput,
|
||
telefon: telefonInput.trim() || undefined,
|
||
betreff: `${zielgruppe === "privat" ? "Privat" : "Geschäftlich"}: ${betreff}`,
|
||
nachricht:
|
||
zielgruppe === "geschaeftlich" && firma.trim()
|
||
? `Firma: ${firma.trim()}${nachricht ? `
|
||
|
||
${nachricht}` : ""}`
|
||
: nachricht,
|
||
}),
|
||
});
|
||
const data = await res.json();
|
||
|
||
if (!res.ok || !data.ok) {
|
||
setErrorMsg(data.error ?? "Unbekannter Fehler");
|
||
setStatus("error");
|
||
} else {
|
||
setStatus("success");
|
||
}
|
||
} catch {
|
||
setErrorMsg("Netzwerkfehler – bitte versuchen Sie es erneut.");
|
||
setStatus("error");
|
||
}
|
||
}
|
||
|
||
return (
|
||
<section id="contact" className="sec band" aria-labelledby="contact-title">
|
||
<div className="wrap grid12">
|
||
<div className="ct__info">
|
||
<h2 id="contact-title">Projekt anfragen</h2>
|
||
<p className="lead">
|
||
Sie haben ein IT-Projekt oder eine Frage? Schreiben Sie uns, wir melden uns schnellstmöglich bei Ihnen.
|
||
</p>
|
||
|
||
<dl className="facts">
|
||
{contactItems.map((item) => (
|
||
<div key={item.label}>
|
||
<dt>{item.label}</dt>
|
||
<dd>
|
||
<a href={item.href} data-source-element="kontakt-section">
|
||
{item.value}
|
||
</a>
|
||
</dd>
|
||
</div>
|
||
))}
|
||
{oeffnungszeiten && oeffnungszeiten.length > 0 && (
|
||
<div>
|
||
<dt>Öffnungszeiten</dt>
|
||
<dd>
|
||
<ul className="hours">
|
||
{oeffnungszeiten.map((oz) => (
|
||
<li key={oz.tag}>
|
||
<span>{oz.tag}</span>
|
||
<span>
|
||
{oz.von} bis {oz.bis}
|
||
</span>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</dd>
|
||
</div>
|
||
)}
|
||
</dl>
|
||
</div>
|
||
|
||
<div className="ct__form">
|
||
<div className="well">
|
||
{status === "success" ? (
|
||
<div className="form__done" role="status">
|
||
<CircleCheck className="icon" aria-hidden="true" />
|
||
<h3>Anfrage gesendet</h3>
|
||
<p>Wir haben Ihre Nachricht erhalten und melden uns schnellstmöglich bei Ihnen.</p>
|
||
<button
|
||
type="button"
|
||
onClick={() => {
|
||
setStatus("idle");
|
||
setName("");
|
||
setEmailInput("");
|
||
setTelefonInput("");
|
||
setFirma("");
|
||
setBetreff("");
|
||
setNachricht("");
|
||
setStartedAt(Date.now());
|
||
}}
|
||
className="btn-secondary btn-lg"
|
||
>
|
||
Neue Anfrage
|
||
</button>
|
||
</div>
|
||
) : (
|
||
<form className="form" onSubmit={handleSubmit}>
|
||
{/* Honeypot: für Menschen unsichtbar, Bots füllen es aus */}
|
||
<div aria-hidden="true" className="absolute -left-[9999px] h-0 w-0 overflow-hidden">
|
||
<label htmlFor="contact-website">Website</label>
|
||
<input
|
||
id="contact-website"
|
||
type="text"
|
||
name="website"
|
||
tabIndex={-1}
|
||
autoComplete="off"
|
||
value={website}
|
||
onChange={(e) => setWebsite(e.target.value)}
|
||
/>
|
||
</div>
|
||
<fieldset>
|
||
<legend className={labelClass}>Ich melde mich als</legend>
|
||
<div className="seg seg--form">
|
||
{([
|
||
["privat", "Privatkunde"],
|
||
["geschaeftlich", "Geschäftskunde"],
|
||
] as const).map(([value, label]) => (
|
||
<label key={value} className={`seg__key${zielgruppe === value ? " is-on" : ""}`}>
|
||
<input
|
||
type="radio"
|
||
name="zielgruppe"
|
||
value={value}
|
||
checked={zielgruppe === value}
|
||
onChange={() => {
|
||
setZielgruppe(value);
|
||
setBetreff("");
|
||
}}
|
||
className="sr-only"
|
||
/>
|
||
{label}
|
||
</label>
|
||
))}
|
||
</div>
|
||
</fieldset>
|
||
|
||
<div className="form__row">
|
||
<div>
|
||
<label htmlFor="contact-name" className={labelClass}>Name</label>
|
||
<input
|
||
id="contact-name"
|
||
name="name"
|
||
type="text"
|
||
required
|
||
autoComplete="name"
|
||
value={name}
|
||
onChange={(e) => setName(e.target.value)}
|
||
placeholder="Max Mustermann"
|
||
className={inputClass}
|
||
/>
|
||
</div>
|
||
{zielgruppe === "geschaeftlich" ? (
|
||
<div>
|
||
<label htmlFor="contact-firma" className={labelClass}>Firma (optional)</label>
|
||
<input
|
||
id="contact-firma"
|
||
name="firma"
|
||
type="text"
|
||
autoComplete="organization"
|
||
value={firma}
|
||
onChange={(e) => setFirma(e.target.value)}
|
||
placeholder="Musterfirma GmbH"
|
||
className={inputClass}
|
||
/>
|
||
</div>
|
||
) : null}
|
||
<div>
|
||
<label htmlFor="contact-email" className={labelClass}>E-Mail</label>
|
||
<input
|
||
id="contact-email"
|
||
name="email"
|
||
type="email"
|
||
required
|
||
autoComplete="email"
|
||
value={emailInput}
|
||
onChange={(e) => setEmailInput(e.target.value)}
|
||
placeholder="max@beispiel.de"
|
||
className={inputClass}
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label htmlFor="contact-telefon" className={labelClass}>Telefon (optional, für Rückruf)</label>
|
||
<input
|
||
id="contact-telefon"
|
||
name="telefon"
|
||
type="tel"
|
||
autoComplete="tel"
|
||
value={telefonInput}
|
||
onChange={(e) => setTelefonInput(e.target.value)}
|
||
placeholder="0171 1234567"
|
||
className={inputClass}
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
<div>
|
||
<label htmlFor="contact-betreff" className={labelClass}>Worum geht es?</label>
|
||
<select
|
||
id="contact-betreff"
|
||
name="betreff"
|
||
required
|
||
value={betreff}
|
||
onChange={(e) => setBetreff(e.target.value)}
|
||
className={inputClass}
|
||
>
|
||
<option value="">Thema auswählen…</option>
|
||
{themen[zielgruppe].map((t) => (
|
||
<option key={t} value={t}>{t}</option>
|
||
))}
|
||
</select>
|
||
</div>
|
||
|
||
<div>
|
||
<label htmlFor="contact-nachricht" className={labelClass}>Nachricht</label>
|
||
<textarea
|
||
id="contact-nachricht"
|
||
name="nachricht"
|
||
rows={5}
|
||
value={nachricht}
|
||
onChange={(e) => setNachricht(e.target.value)}
|
||
placeholder={
|
||
zielgruppe === "privat"
|
||
? "Beschreiben Sie kurz Ihr Problem oder Ihren Wunsch…"
|
||
: "Beschreiben Sie Ihr Anliegen, z. B. Anzahl Arbeitsplätze und Standorte…"
|
||
}
|
||
className={inputClass}
|
||
/>
|
||
</div>
|
||
|
||
{status === "error" && (
|
||
<div className="form__error" role="alert">
|
||
<CircleAlert className="icon--s" aria-hidden="true" />
|
||
<span>
|
||
Nachricht konnte nicht gesendet werden. Bitte versuchen Sie es erneut oder kontaktieren Sie uns direkt.
|
||
{errorMsg && <small>{errorMsg}</small>}
|
||
</span>
|
||
</div>
|
||
)}
|
||
|
||
<button
|
||
type="submit"
|
||
disabled={status === "loading"}
|
||
aria-busy={status === "loading"}
|
||
className="btn-primary btn-lg"
|
||
>
|
||
{status === "loading" ? (
|
||
<>
|
||
<LoaderCircle className="icon--s spin keep-motion" aria-hidden="true" />
|
||
Wird gesendet…
|
||
</>
|
||
) : (
|
||
"Nachricht senden"
|
||
)}
|
||
</button>
|
||
</form>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
);
|
||
}
|