diff --git a/app/admin/components/BulkArchive.tsx b/app/admin/components/BulkArchive.tsx index 8d27234..8e4cbcd 100644 --- a/app/admin/components/BulkArchive.tsx +++ b/app/admin/components/BulkArchive.tsx @@ -2,12 +2,67 @@ 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 onChange(e.target.checked)} aria-label={label}/>;} -export function BulkArchiveActions({entityType,records,selected,onSelectionChange,onArchived}:{entityType:string;records:BulkArchiveRecord[];selected:Set;onSelectionChange:(next:Set)=>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
{chosen.length} رکورد انتخاب شده
; +export type BulkArchiveRecord = { id: string; rowVersion: string }; +export function SelectionCheckbox({ checked, onChange, label }: { + checked: boolean; onChange: (checked: boolean) => void; label: string; +}) { + return onChange(e.target.checked)} aria-label={label}/>; +} + +export function BulkArchiveActions({ entityType, records, selected, onSelectionChange, onArchived }: { + entityType: string; records: BulkArchiveRecord[]; selected: Set; + onSelectionChange: (next: Set) => 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
+ {result &&
+ {result.success} مورد انجام شد؛ {result.failures.length} مورد ناموفق بود. + {result.failures.length > 0 &&
    {result.failures.map((failure, index) =>
  • {failure}
  • )}
} +
} + + {chosen.length} رکورد انتخاب شده + + +
; } diff --git a/app/admin/components/RelatedEntitySelect.tsx b/app/admin/components/RelatedEntitySelect.tsx new file mode 100644 index 0000000..5b8c0f2 --- /dev/null +++ b/app/admin/components/RelatedEntitySelect.tsx @@ -0,0 +1,54 @@ +import { useEffect, useMemo, useState } from "react"; +import { api } from "../lib/api"; +import { useDebouncedValue } from "./ListControls"; +import { Input, Select } from "./Ui"; + +type Option = { id: string; title: string; projectNumber?: string }; +type Page = { items: Option[]; totalCount: number }; + +export function RelatedEntitySelect({ name, path, value, onChange, placeholder, required = true }: { + name: string; path: "/api/v1/ideas" | "/api/v1/projects"; + value: string; onChange: (id: string) => void; placeholder: string; required?: boolean; +}) { + const [search, setSearch] = useState(""); + const query = useDebouncedValue(search); + const [items, setItems] = useState([]); + const [selected, setSelected] = useState