Files
kai 8d88e90a07
release / which version (push) Successful in 3s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m2s
release / tag and release (push) Successful in 2s
26.11.53: Mails zur Runde (#55)
2026-11-09 07:38:56 +00:00

97 lines
3.6 KiB
TypeScript

"use client";
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";
export type NotifyMember = {
id: string;
name: string;
role: string;
notifyEmail: string | null;
notifyVerified: boolean;
off: boolean;
loginEmail: string | null;
};
// Eine Zeile je Person: Adresse, Stand, Bestätigung schicken, Mails an/aus (26.11.53).
export function NotifyAdmin({ members }: { members: NotifyMember[] }) {
return (
<ul className="flex flex-col gap-4">
{members.map((m) => (
<NotifyRow key={m.id} member={m} />
))}
</ul>
);
}
function NotifyRow({ member: m }: { member: NotifyMember }) {
const t = useTranslations("more.roundMails");
const router = useRouter();
const [email, setEmail] = useState(m.notifyEmail ?? m.loginEmail ?? "");
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [sent, setSent] = useState<string | null>(null);
const active = m.notifyVerified ? m.notifyEmail : m.loginEmail;
const status = m.off && active ? "off" : active ? "active" : m.notifyEmail ? "pending" : "none";
async function call(method: string, body: object) {
setBusy(true);
setError(null);
const res = await fetch("/api/household/notify", { method, headers: { "content-type": "application/json" }, body: JSON.stringify({ memberId: m.id, ...body }) }).catch(() => null);
const data = await res?.json().catch(() => ({}));
setBusy(false);
if (!res?.ok) {
setError(["mailNotConfigured", "mailFailed", "tooMany", "invalid"].includes(data?.error) ? data.error : "unknown");
return false;
}
router.refresh();
return true;
}
return (
<li className="flex flex-col gap-3 rounded-2xl bg-muted p-4" aria-label={m.name}>
<div className="flex items-baseline justify-between gap-2">
<h2 className="text-lg font-semibold">{m.name}</h2>
<span className="text-sm text-muted-foreground">{t(`status.${status}`, { email: active ?? m.notifyEmail ?? "" })}</span>
</div>
<form
className="flex flex-col gap-2"
onSubmit={async (e) => {
e.preventDefault();
if (await call("PUT", { email })) setSent(email);
}}
>
<Label htmlFor={`notify-${m.id}`}>{t("address", { name: m.name })}</Label>
<div className="flex gap-2">
<Input id={`notify-${m.id}`} type="email" inputMode="email" autoComplete="off" value={email} onChange={(e) => (setEmail(e.target.value), setSent(null))} />
<Button type="submit" variant="outline" disabled={busy || !email.includes("@")}>
{t("send")}
</Button>
</div>
</form>
{sent ? (
<p role="status" className="text-sm">
{t("sent", { email: sent })}
</p>
) : null}
{active ? (
<label className="flex min-h-11 items-center gap-3">
<input type="checkbox" className="size-5 accent-primary" checked={!m.off} disabled={busy} onChange={(e) => call("PATCH", { off: !e.target.checked })} />
{t("gets", { name: m.name })}
</label>
) : null}
{m.notifyEmail ? (
<Button type="button" variant="ghost" className="self-start" disabled={busy} onClick={() => call("DELETE", {})}>
{t("remove")}
</Button>
) : null}
{error ? <ErrorNotice>{t(`errors.${error}`)}</ErrorNotice> : null}
</li>
);
}