44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
"use client";
|
|
|
|
import { useRouter } from "next/navigation";
|
|
import { useTranslations } from "next-intl";
|
|
import { useState } from "react";
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
// Entfernen mit zweitem Tipp statt Dialog: Der erste Tipp fragt im Knopf
|
|
// selbst nach, der zweite entfernt.
|
|
export function RemoveButton({ url, id }: { url: string; id: string }) {
|
|
const t = useTranslations("more");
|
|
const te = useTranslations("auth.errors");
|
|
const router = useRouter();
|
|
const [armed, setArmed] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
return (
|
|
<span className="flex flex-col items-end gap-1">
|
|
<Button
|
|
type="button"
|
|
variant={armed ? "default" : "ghost"}
|
|
className="min-h-11 px-3 text-sm"
|
|
onBlur={() => setArmed(false)}
|
|
onClick={async () => {
|
|
if (!armed) return setArmed(true);
|
|
const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ id }) });
|
|
if (res.ok) router.refresh();
|
|
else {
|
|
const data = await res.json().catch(() => ({}));
|
|
setError(te.has(data.error) ? te(data.error) : te("unknown"));
|
|
setArmed(false);
|
|
}
|
|
}}
|
|
>
|
|
{armed ? t("confirmRemove") : t("remove")}
|
|
</Button>
|
|
{error ? (
|
|
<span role="alert" className="text-xs">
|
|
{error}
|
|
</span>
|
|
) : null}
|
|
</span>
|
|
);
|
|
}
|