65 lines
5.2 KiB
TypeScript
65 lines
5.2 KiB
TypeScript
import { Pencil, Plus } from "lucide-react";
|
||
import { useEffect, useState } from "react";
|
||
import { Link } from "react-router";
|
||
import { Card, EmptyState, ErrorBanner, PageTitle, Select } from "../components/Ui";
|
||
import { api } from "../lib/api";
|
||
import type { PagedResult, ProjectItem } from "../types";
|
||
import { RecordAvatar, useFirstImages } from "../components/RecordImage";
|
||
import { ListToolbar, PaginationControls, useDebouncedValue } from "../components/ListControls";
|
||
import { ArchiveButton } from "../components/ArchiveButton";
|
||
import { BulkArchiveActions, SelectionCheckbox } from "../components/BulkArchive";
|
||
|
||
const statusLabel = ["پیشنویس", "منتشرشده", "بایگانی"];
|
||
const innovationLabel = ["", "نوآوری دیجیتال", "نوآوری در فرآیند", "نوآوری در محصول", "نوآوری سبز", "نوآوری ساخت داخل", "نوآوری در کسبوکار", "نوآوری نرم", "نوآوری اجتماعی"];
|
||
|
||
export function ProjectsPage() {
|
||
const [data, setData] = useState<PagedResult<ProjectItem> | null>(null);
|
||
const [error, setError] = useState("");
|
||
const [page, setPage] = useState(1);
|
||
const [pageSize, setPageSize] = useState(25);
|
||
const [innovationType, setInnovationType] = useState("");
|
||
const [search, setSearch] = useState("");
|
||
const debouncedSearch = useDebouncedValue(search);
|
||
const [loading, setLoading] = useState(true);
|
||
const [selected,setSelected]=useState<Set<string>>(new Set());
|
||
useEffect(() => {
|
||
let active = true;
|
||
setLoading(true); setError("");
|
||
const query = new URLSearchParams({ page: String(page), pageSize: String(pageSize) });
|
||
if (innovationType) query.set("innovationType", innovationType);
|
||
if (debouncedSearch) query.set("search", debouncedSearch);
|
||
api<PagedResult<ProjectItem>>(`/api/v1/projects?${query}`)
|
||
.then(result => { if (active) setData(result); })
|
||
.catch(e => { if (active) setError(e.message); })
|
||
.finally(() => { if (active) setLoading(false); });
|
||
return () => { active = false; };
|
||
}, [page, pageSize, innovationType, debouncedSearch]);
|
||
const images = useFirstImages("Project", data?.items.map(x => x.id));
|
||
const remove=(id:string)=>setData(old=>old?{...old,items:old.items.filter(x=>x.id!==id),totalCount:old.totalCount-1}:old);
|
||
return <>
|
||
<PageTitle title="پروژهها" description="ثبت و مدیریت اطلاعات پایه و جزئیات نوآوری پروژه"
|
||
action={<Link className="button link-button" to="/admin/projects/new"><Plus size={18}/>پروژه جدید</Link>}/>
|
||
{error && <ErrorBanner message={error}/>}
|
||
<Card>
|
||
<ListToolbar search={search} onSearch={value => { setSearch(value); setPage(1); }} total={data?.totalCount ?? 0} placeholder="عنوان یا شماره پروژه">
|
||
<label className="list-filter">نوع نوآوری
|
||
<Select value={innovationType} onChange={e => { setInnovationType(e.target.value); setPage(1); }}>
|
||
<option value="">همه انواع</option>
|
||
{innovationLabel.map((label, value) => value > 0 && <option key={value} value={value}>{label}</option>)}
|
||
</Select>
|
||
</label>
|
||
</ListToolbar>
|
||
{data?.items.length?<BulkArchiveActions entityType="Project" records={data.items} selected={selected} onSelectionChange={setSelected} onArchived={ids=>setData(old=>old?{...old,items:old.items.filter(x=>!ids.includes(x.id)),totalCount:old.totalCount-ids.length}:old)}/>:null}
|
||
{!data ? <div className="loading-line">{loading ? "در حال دریافت اطلاعات…" : "اطلاعات پروژهها دریافت نشد."}</div> : data.items.length === 0
|
||
? <EmptyState title="هنوز پروژهای ثبت نشده" description="اولین پروژه را با فرم مستقل سامانه ایجاد کنید."/>
|
||
: <><div className={`table-wrap${loading ? " is-loading" : ""}`}><table><thead><tr><th>انتخاب</th><th>تصویر</th><th>شماره</th><th>عنوان</th><th>نوع</th><th>تاریخ شروع</th><th>وضعیت</th><th>عملیات</th></tr></thead>
|
||
<tbody>{data.items.map(item => <tr key={item.id}><td><SelectionCheckbox checked={selected.has(item.id)} onChange={checked=>setSelected(old=>{const next=new Set(old);checked?next.add(item.id):next.delete(item.id);return next;})} label={`انتخاب ${item.title}`}/></td><td><RecordAvatar attachmentId={images[item.id]} label={item.title}/></td><td className="latin">{item.projectNumber}</td><td>{item.title}</td><td>{innovationLabel[item.innovationType] ?? "نامشخص"}</td><td className="latin">{item.startDate}</td><td><span className={`status status-${item.status}`}>{statusLabel[item.status]}</span></td><td><div className="row-actions"><Link className="row-action" to={`/admin/projects/${item.id}/edit`}><Pencil size={15}/> مشاهده و ویرایش</Link><ArchiveButton entityType="Project" id={item.id} rowVersion={item.rowVersion} onArchived={()=>remove(item.id)}/></div></td></tr>)}</tbody></table></div>
|
||
<PaginationControls page={page} pageSize={pageSize} total={data.totalCount} loading={loading}
|
||
onPage={setPage} onPageSize={value => { setPageSize(value); setPage(1); }}/>
|
||
</>}
|
||
</Card>
|
||
</>;
|
||
}
|
||
|
||
|