inogen/app/admin/components/BulkArchive.tsx

69 lines
3.5 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";
export type BulkArchiveRecord = { id: string; rowVersion: string };
export function SelectionCheckbox({ checked, onChange, label }: {
checked: boolean; onChange: (checked: boolean) => void; label: string;
}) {
return <input type="checkbox" checked={checked} onChange={e => onChange(e.target.checked)} aria-label={label}/>;
}
export function BulkArchiveActions({ entityType, records, selected, onSelectionChange, onArchived }: {
entityType: string; records: BulkArchiveRecord[]; selected: Set<string>;
onSelectionChange: (next: Set<string>) => void; onArchived: (ids: string[]) => void;
}) {
const [busy, setBusy] = useState(false);
const [result, setResult] = useState<{ success: number; failures: string[] } | null>(null);
const chosen = records.filter(x => selected.has(x.id));
async function run(action: "archive" | "delete") {
if (!chosen.length) return;
if (action === "archive") {
if (!window.confirm(`${chosen.length} رکورد به بایگانی منتقل شود؟`)) return;
} else {
const phrase = `حذف دائمی ${chosen.length} رکورد`;
if (window.prompt(`رکوردها واقعاً از دیتابیس حذف می‌شوند و قابل بازگشت نیستند.\nبرای ادامه دقیقاً بنویسید:\n${phrase}`) !== phrase) return;
if (!window.confirm("تأیید نهایی حذف دائمی؟")) return;
}
setBusy(true);
setResult(null);
const succeeded: string[] = [];
const failures: string[] = [];
try {
for (const record of chosen) {
try {
const path = action === "archive"
? `/api/v1/archive/${entityType}/${record.id}/archive`
: `/api/v1/delete/${entityType}/${record.id}`;
await api(path, { method: action === "archive" ? "POST" : "DELETE",
body: JSON.stringify({ rowVersion: record.rowVersion }) });
succeeded.push(record.id);
} catch (reason) {
failures.push(`${record.id}: ${reason instanceof Error ? reason.message : "عملیات ناموفق بود."}`);
}
}
} finally {
if (succeeded.length) {
onSelectionChange(new Set([...selected].filter(id => !succeeded.includes(id))));
onArchived(succeeded);
}
setBusy(false);
}
setResult({ success: succeeded.length, failures });
}
const all = records.length > 0 && records.every(x => selected.has(x.id));
return <div className="bulk-archive-bar">
{result && <div role="status" className="bulk-archive-result">
<strong>{result.success} مورد انجام شد؛ {result.failures.length} مورد ناموفق بود.</strong>
{result.failures.length > 0 && <ul>{result.failures.map((failure, index) => <li key={index}>{failure}</li>)}</ul>}
</div>}
<label><SelectionCheckbox checked={all} onChange={on => onSelectionChange(on ? new Set(records.map(x => x.id)) : new Set())} label="انتخاب همه رکوردهای صفحه"/>انتخاب همه صفحه</label>
<span>{chosen.length} رکورد انتخاب شده</span>
<button type="button" className="button" disabled={!chosen.length || busy} onClick={() => void run("archive")}><Archive size={16}/>بایگانی گروهی</button>
<button type="button" className="button danger" disabled={!chosen.length || busy} onClick={() => void run("delete")}><Trash2 size={16}/>حذف دائمی گروهی</button>
</div>;
}