109 lines
3.8 KiB
TypeScript
109 lines
3.8 KiB
TypeScript
"use client";
|
|
|
|
import { ImagePlus } from "lucide-react";
|
|
import { useRouter } from "next/navigation";
|
|
import { useTranslations } from "next-intl";
|
|
import { useState } from "react";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
import { ErrorNotice } from "@/components/ui/notice";
|
|
|
|
// Widmung pflegen (nur Admin): Name, ein paar Sätze, ein Foto. Das Foto bleibt auf
|
|
// der Box (/app/data), nicht im Code (Kai, 7.11.2026).
|
|
export function DedicationForm({ name, text, hasPhoto }: { name: string; text: string; hasPhoto: boolean }) {
|
|
const t = useTranslations("more.dedication");
|
|
const router = useRouter();
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState(false);
|
|
const [saved, setSaved] = useState(false);
|
|
const [file, setFile] = useState<string | null>(null);
|
|
|
|
async function send(form: FormData) {
|
|
setBusy(true);
|
|
setError(false);
|
|
setSaved(false);
|
|
const res = await fetch("/api/household/dedication", { method: "PUT", body: form }).catch(() => null);
|
|
setBusy(false);
|
|
if (!res?.ok) return setError(true);
|
|
setSaved(true);
|
|
setFile(null);
|
|
router.refresh();
|
|
}
|
|
|
|
return (
|
|
<form
|
|
className="flex flex-col gap-3 rounded-2xl border border-border p-4"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
void send(new FormData(e.currentTarget));
|
|
}}
|
|
>
|
|
<h3 className="font-semibold">{t("edit")}</h3>
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="dedication-name">{t("name")}</Label>
|
|
<Input id="dedication-name" name="name" maxLength={40} defaultValue={name} placeholder="Maike" />
|
|
</div>
|
|
<div className="flex flex-col gap-2">
|
|
<Label htmlFor="dedication-text">{t("text")}</Label>
|
|
<textarea
|
|
id="dedication-text"
|
|
name="text"
|
|
maxLength={800}
|
|
rows={4}
|
|
defaultValue={text}
|
|
placeholder={t("textPlaceholder")}
|
|
className="rounded-xl border border-border bg-background px-3 py-2 text-base"
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-2">
|
|
{/* Eigener Knopf statt des Dateifelds: das zeigte die Beschriftung des Browsers („Choose File“). */}
|
|
<input
|
|
id="dedication-photo"
|
|
name="photo"
|
|
type="file"
|
|
accept="image/jpeg,image/png,image/webp,image/heic,image/heif"
|
|
className="peer sr-only"
|
|
onChange={(e) => setFile(e.target.files?.[0]?.name ?? null)}
|
|
/>
|
|
<Label
|
|
htmlFor="dedication-photo"
|
|
className="inline-flex min-h-11 cursor-pointer items-center gap-2 self-start rounded-full border border-border px-5 font-semibold peer-focus-visible:ring-2 peer-focus-visible:ring-ring"
|
|
>
|
|
<ImagePlus aria-hidden className="size-5" />
|
|
{hasPhoto ? t("newPhoto") : t("photo")}
|
|
</Label>
|
|
{file ? <p className="text-sm">{t("chosen", { name: file })}</p> : null}
|
|
<p className="text-sm text-muted-foreground">{t("photoHint")}</p>
|
|
</div>
|
|
{error ? <ErrorNotice>{t("error")}</ErrorNotice> : null}
|
|
{saved ? (
|
|
<p role="status" className="text-sm">
|
|
{t("saved")}
|
|
</p>
|
|
) : null}
|
|
<div className="flex flex-wrap gap-2">
|
|
<Button type="submit" disabled={busy}>
|
|
{t("save")}
|
|
</Button>
|
|
{hasPhoto ? (
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
disabled={busy}
|
|
onClick={() => {
|
|
const form = new FormData();
|
|
form.set("name", name);
|
|
form.set("text", text);
|
|
form.set("removePhoto", "1");
|
|
void send(form);
|
|
}}
|
|
>
|
|
{t("removePhoto")}
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|