inogen/app/admin/components/ArchiveButton.tsx
2026-10-18 18:12:59 +03:30

29 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Archive, Trash2 } from "lucide-react";
import { useState } from "react";
import { api } from "../lib/api";
import type { ArchivePreview } from "../types";
export function ArchiveButton({entityType,id,rowVersion,onArchived}:{entityType:string;id:string;rowVersion:string;onArchived?:()=>void}) {
const [busy,setBusy]=useState<"archive"|"delete"|null>(null);
async function archive() {
setBusy("archive");
try {
const preview=await api<ArchivePreview>(`/api/v1/archive/${entityType}/${id}/preview`);
if(!preview.canArchive){window.alert("این رکورد به دلیل وابستگی فعال قابل بایگانی نیست.");return;}
if(!window.confirm("رکورد موقتاً غیرفعال و به بایگانی منتقل شود؟ امکان بازگردانی آن وجود دارد."))return;
await api(`/api/v1/archive/${entityType}/${id}/archive`,{method:"POST",body:JSON.stringify({rowVersion})});
onArchived?.();
} catch(reason){window.alert(reason instanceof Error?reason.message:"بایگانی ناموفق بود.");} finally {setBusy(null);}
}
async function remove() {
const phrase="حذف دائمی";
const answer=window.prompt(`این عملیات غیرقابل بازگشت است و رکورد واقعاً از دیتابیس حذف می‌شود.\nبرای ادامه دقیقاً بنویسید: ${phrase}`);
if(answer!==phrase){if(answer!==null)window.alert("عبارت تأیید صحیح نیست.");return;}
if(!window.confirm("تأیید نهایی حذف دائمی رکورد؟"))return;
setBusy("delete");
try {await api(`/api/v1/delete/${entityType}/${id}`,{method:"DELETE",body:JSON.stringify({rowVersion})});onArchived?.();}
catch(reason){window.alert(reason instanceof Error?reason.message:"حذف دائمی ناموفق بود.");} finally {setBusy(null);}
}
return <><button type="button" className="row-action" disabled={!!busy} onClick={()=>void archive()}><Archive size={15}/>{busy==="archive"?"در حال بایگانی…":"بایگانی"}</button><button type="button" className="row-action danger-link" disabled={!!busy} onClick={()=>void remove()}><Trash2 size={15}/>{busy==="delete"?"در حال حذف…":"حذف"}</button></>;
}