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

65 lines
2.0 KiB
TypeScript

"use client";
import { useState, useTransition } from "react";
import { entferneLogo, ladeLogoHoch } from "@/app/firma/actions";
import { Alert } from "@/components/ui/Alert";
import { Button } from "@/components/ui/Button";
import { FIELD_CLASSES, FormField } from "@/components/ui/FormField";
export function LogoForm({ hatLogo }: { hatLogo: boolean }) {
const [fehler, setFehler] = useState<string | null>(null);
const [isPending, startTransition] = useTransition();
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
const form = event.currentTarget;
const formData = new FormData(form);
startTransition(async () => {
const ergebnis = await ladeLogoHoch(formData);
setFehler(ergebnis.success ? null : ergebnis.error);
if (ergebnis.success) form.reset();
});
}
function handleEntfernen() {
startTransition(async () => {
const ergebnis = await entferneLogo();
setFehler(ergebnis.success ? null : ergebnis.error);
});
}
return (
<form onSubmit={handleSubmit}>
{fehler && (
<div className="mb-4">
<Alert>{fehler}</Alert>
</div>
)}
{hatLogo && (
// eslint-disable-next-line @next/next/no-img-element -- eigene Logo-Route mit Session-Cookie
<img src="/firma/logo" alt="Aktuelles Logo" className="mb-4 h-12 w-auto max-w-[220px] object-contain" />
)}
<FormField label="Logo (PNG, JPG oder SVG, max. 1 MB)" htmlFor="firma-logo">
<input
id="firma-logo"
name="logo"
type="file"
accept="image/png,image/jpeg,image/svg+xml"
required
className={FIELD_CLASSES}
/>
</FormField>
<div className="flex flex-col gap-3">
<Button type="submit" disabled={isPending}>
Logo hochladen
</Button>
{hatLogo && (
<Button type="button" variant="secondary" disabled={isPending} onClick={handleEntfernen}>
Logo entfernen
</Button>
)}
</div>
</form>
);
}