1
0
Fork 0
fahrPool/web/components/firma/SmtpForm.tsx

119 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useState, useTransition } from "react";
import { entferneSmtp, sendeTestmail, speichereSmtp } from "@/app/firma/smtp-actions";
import { Alert } from "@/components/ui/Alert";
import { Button } from "@/components/ui/Button";
import { FIELD_CLASSES, FormField } from "@/components/ui/FormField";
import type { SmtpAnzeige } from "@/lib/firma/smtp";
export function SmtpForm({ smtp }: { smtp: SmtpAnzeige | null }) {
const [fehler, setFehler] = useState<string | null>(null);
const [meldung, setMeldung] = useState<string | null>(null);
const [isPending, startTransition] = useTransition();
function zeige(ergebnis: { success: true } | { success: false; error: string }, erfolg: string) {
setFehler(ergebnis.success ? null : ergebnis.error);
setMeldung(ergebnis.success ? erfolg : null);
}
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
const formData = new FormData(event.currentTarget);
startTransition(async () => zeige(await speichereSmtp(formData), "Gespeichert."));
}
return (
<form onSubmit={handleSubmit}>
<p className="mb-4 text-sm text-asphalt-grey">
Ohne eigenen Server gehen Protokoll-Mails über den Absender der Plattform raus.
</p>
{fehler && (
<div className="mb-4">
<Alert>{fehler}</Alert>
</div>
)}
<FormField label="SMTP-Server" htmlFor="smtp-host">
<input id="smtp-host" name="host" required defaultValue={smtp?.host ?? ""} className={FIELD_CLASSES} />
</FormField>
<FormField label="Port" htmlFor="smtp-port">
<input
id="smtp-port"
name="port"
type="number"
inputMode="numeric"
min={1}
max={65535}
required
defaultValue={smtp?.port ?? 587}
className={FIELD_CLASSES}
/>
</FormField>
<label className="mb-4 flex min-h-11 items-center gap-3 text-asphalt-grey">
<input type="checkbox" name="secure" defaultChecked={smtp?.secure ?? false} className="h-5 w-5" />
TLS ab Verbindungsbeginn (meist Port 465)
</label>
<FormField label="Benutzer (optional)" htmlFor="smtp-benutzer">
<input
id="smtp-benutzer"
name="benutzer"
autoComplete="off"
defaultValue={smtp?.benutzer ?? ""}
className={FIELD_CLASSES}
/>
</FormField>
<FormField label={smtp?.passwortGesetzt ? "Passwort (gesetzt – leer lassen zum Beibehalten)" : "Passwort"} htmlFor="smtp-passwort">
<input id="smtp-passwort" name="passwort" type="password" autoComplete="new-password" className={FIELD_CLASSES} />
</FormField>
<FormField label="Absenderadresse" htmlFor="smtp-absender">
<input
id="smtp-absender"
name="absender_adresse"
type="email"
required
defaultValue={smtp?.absender_adresse ?? ""}
className={FIELD_CLASSES}
/>
</FormField>
<FormField label="Absendername (optional)" htmlFor="smtp-absender-name">
<input
id="smtp-absender-name"
name="absender_name"
defaultValue={smtp?.absender_name ?? ""}
className={FIELD_CLASSES}
/>
</FormField>
{meldung && (
<p role="status" className="mb-4 text-status-success">
{meldung}
</p>
)}
<div className="flex flex-col gap-3">
<Button type="submit" disabled={isPending}>
Speichern
</Button>
{smtp && (
<>
<Button
type="button"
variant="secondary"
disabled={isPending}
onClick={() => startTransition(async () => zeige(await sendeTestmail(), "Testmail verschickt."))}
>
Testmail senden
</Button>
<Button
type="button"
variant="destructive"
disabled={isPending}
onClick={() => startTransition(async () => zeige(await entferneSmtp(), "SMTP-Einstellungen entfernt."))}
>
SMTP entfernen
</Button>
</>
)}
</div>
</form>
);
}