forked from fahrPool/fahrPool
67 lines
2.4 KiB
TypeScript
67 lines
2.4 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useTransition } from "react";
|
|
import { entferneAppIcon, ladeAppIconHoch } 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 AppIconForm({ hatAppIcon, vorschauUrl }: { hatAppIcon: boolean; vorschauUrl: string }) {
|
|
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 ladeAppIconHoch(formData);
|
|
setFehler(ergebnis.success ? null : ergebnis.error);
|
|
if (ergebnis.success) form.reset();
|
|
});
|
|
}
|
|
|
|
function handleEntfernen() {
|
|
startTransition(async () => {
|
|
const ergebnis = await entferneAppIcon();
|
|
setFehler(ergebnis.success ? null : ergebnis.error);
|
|
});
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit}>
|
|
{fehler && (
|
|
<div className="mb-4">
|
|
<Alert>{fehler}</Alert>
|
|
</div>
|
|
)}
|
|
{/* eslint-disable-next-line @next/next/no-img-element -- oeffentliche App-Icon-Route, zeigt auch den Fallback */}
|
|
<img src={vorschauUrl} alt="Aktuelles App-Icon" className="mb-4 h-24 w-24 rounded-2xl object-cover" />
|
|
<FormField label="App-Icon (PNG, JPG oder SVG, quadratisch, max. 1 MB)" htmlFor="firma-app-icon">
|
|
<input
|
|
id="firma-app-icon"
|
|
name="app_icon"
|
|
type="file"
|
|
accept="image/png,image/jpeg,image/svg+xml"
|
|
required
|
|
className={FIELD_CLASSES}
|
|
/>
|
|
</FormField>
|
|
<p className="mb-4 text-sm text-asphalt-grey/70">
|
|
Erscheint auf dem Startbildschirm. Ohne eigenes App-Icon wird es aus dem Logo erzeugt. Bereits installierte
|
|
iPhone-Apps übernehmen ein neues Icon erst nach Neuinstallation.
|
|
</p>
|
|
<div className="flex flex-col gap-3">
|
|
<Button type="submit" disabled={isPending}>
|
|
App-Icon hochladen
|
|
</Button>
|
|
{hatAppIcon && (
|
|
<Button type="button" variant="secondary" disabled={isPending} onClick={handleEntfernen}>
|
|
App-Icon entfernen
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|