14 lines
2.7 KiB
TypeScript
14 lines
2.7 KiB
TypeScript
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 chosen=records.filter(x=>selected.has(x.id));
|
||
function done(){const ids=chosen.map(x=>x.id);onSelectionChange(new Set());onArchived(ids);}
|
||
async function archive(){if(!chosen.length||!window.confirm(`${chosen.length} رکورد به بایگانی منتقل شود؟`))return;setBusy(true);try{for(const x of chosen)await api(`/api/v1/archive/${entityType}/${x.id}/archive`,{method:"POST",body:JSON.stringify({rowVersion:x.rowVersion})});done();}catch(e){window.alert(e instanceof Error?e.message:"بایگانی گروهی ناموفق بود.");}finally{setBusy(false);}}
|
||
async function remove(){if(!chosen.length)return;const phrase=`حذف دائمی ${chosen.length} رکورد`;const answer=window.prompt(`رکوردها واقعاً از دیتابیس حذف میشوند و قابل بازگشت نیستند.\nبرای ادامه دقیقاً بنویسید:\n${phrase}`);if(answer!==phrase){if(answer!==null)window.alert("عبارت تأیید صحیح نیست.");return;}if(!window.confirm("تأیید نهایی حذف دائمی؟"))return;setBusy(true);try{for(const x of chosen)await api(`/api/v1/delete/${entityType}/${x.id}`,{method:"DELETE",body:JSON.stringify({rowVersion:x.rowVersion})});done();}catch(e){window.alert(e instanceof Error?e.message:"حذف گروهی ناموفق بود.");}finally{setBusy(false);}}
|
||
const all=records.length>0&&records.every(x=>selected.has(x.id));return <div className="bulk-archive-bar"><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 archive()}><Archive size={16}/>بایگانی گروهی</button><button type="button" className="button danger" disabled={!chosen.length||busy} onClick={()=>void remove()}><Trash2 size={16}/>حذف دائمی گروهی</button></div>;
|
||
}
|