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

94 lines
3.4 KiB
TypeScript

"use client";
import { useState, useTransition } from "react";
import { speichereFirmaEinstellungen } from "@/app/firma/actions";
import { Alert } from "@/components/ui/Alert";
import { Button } from "@/components/ui/Button";
import { FIELD_CLASSES, FormField } from "@/components/ui/FormField";
import { STANDARD_FARBE_DUNKEL, STANDARD_FARBE_PRIMAER } from "@/lib/firma/farben";
import type { Firma } from "@/lib/firma/queries";
export function FirmaEinstellungenForm({ firma }: { firma: Firma }) {
const [fehler, setFehler] = useState<string | null>(null);
const [gespeichert, setGespeichert] = useState(false);
const [standard, setStandard] = useState(!firma.farbe_primaer && !firma.farbe_dunkel);
const [isPending, startTransition] = useTransition();
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
const formData = new FormData(event.currentTarget);
startTransition(async () => {
const ergebnis = await speichereFirmaEinstellungen(formData);
setGespeichert(ergebnis.success);
setFehler(ergebnis.success ? null : ergebnis.error);
});
}
return (
<form onSubmit={handleSubmit}>
{fehler && (
<div className="mb-4">
<Alert>{fehler}</Alert>
</div>
)}
<FormField label="Firmenname" htmlFor="firma-name">
<input id="firma-name" name="name" required maxLength={100} defaultValue={firma.name} className={FIELD_CLASSES} />
</FormField>
<FormField label="WhatsApp-Empfänger für neue Aufträge (optional)" htmlFor="firma-whatsapp">
<input
id="firma-whatsapp"
name="whatsapp_empfaenger"
type="tel"
inputMode="tel"
placeholder="+49 151 2345678"
defaultValue={firma.whatsapp_empfaenger ?? ""}
className={FIELD_CLASSES}
/>
</FormField>
<label className="mb-4 flex min-h-11 items-center gap-3 text-asphalt-grey">
<input
type="checkbox"
name="standard_farben"
checked={standard}
onChange={(e) => setStandard(e.target.checked)}
className="h-5 w-5"
/>
Standardfarben verwenden
</label>
{!standard && (
<div className="grid grid-cols-2 gap-4">
<FormField label="Hauptfarbe" htmlFor="firma-farbe-primaer">
<input
id="firma-farbe-primaer"
name="farbe_primaer"
type="color"
defaultValue={firma.farbe_primaer ?? STANDARD_FARBE_PRIMAER}
className="h-11 w-full rounded-md border border-aluminium-grey"
/>
</FormField>
<FormField label="Dunkle Farbe" htmlFor="firma-farbe-dunkel">
<input
id="firma-farbe-dunkel"
name="farbe_dunkel"
type="color"
defaultValue={firma.farbe_dunkel ?? STANDARD_FARBE_DUNKEL}
className="h-11 w-full rounded-md border border-aluminium-grey"
/>
</FormField>
</div>
)}
<p className="mb-4 text-sm text-asphalt-grey">
Beide Farben brauchen genug Kontrast zu weißer Schrift (mindestens 4,5:1).
</p>
{gespeichert && (
<p role="status" className="mb-4 text-status-success">
Gespeichert.
</p>
)}
<Button type="submit" disabled={isPending}>
Speichern
</Button>
</form>
);
}