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

96 lines
7.2 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 { Pencil, Plus } from "lucide-react";
import { useEffect, useMemo, useState, type FormEvent } from "react";
import { Button, Card, EmptyState, ErrorBanner, Field, Input, PageTitle } from "../components/Ui";
import { api } from "../lib/api";
import type { ReferenceItem } from "../types";
import { ListToolbar, PaginationControls } from "../components/ListControls";
import { ArchiveButton } from "../components/ArchiveButton";
export function ReferenceDataPage() {
const [themes, setThemes] = useState<ReferenceItem[]>([]);
const [values, setValues] = useState<ReferenceItem[]>([]);
const [selectedTheme, setSelectedTheme] = useState("");
const [error, setError] = useState("");
const [themeSearch,setThemeSearch]=useState(""); const [themePage,setThemePage]=useState(1); const [themePageSize,setThemePageSize]=useState(25);
const [valueSearch,setValueSearch]=useState(""); const [valuePage,setValuePage]=useState(1); const [valuePageSize,setValuePageSize]=useState(25);
const filteredThemes=useMemo(()=>{const q=themeSearch.trim().toLocaleLowerCase("fa");return themes.filter(x=>!q||x.title.toLocaleLowerCase("fa").includes(q));},[themes,themeSearch]);
const visibleThemes=useMemo(()=>filteredThemes.slice((themePage-1)*themePageSize,themePage*themePageSize),[filteredThemes,themePage,themePageSize]);
const filteredValues=useMemo(()=>{const q=valueSearch.trim().toLocaleLowerCase("fa");return values.filter(x=>!q||x.title.toLocaleLowerCase("fa").includes(q));},[values,valueSearch]);
const visibleValues=useMemo(()=>filteredValues.slice((valuePage-1)*valuePageSize,valuePage*valuePageSize),[filteredValues,valuePage,valuePageSize]);
useEffect(()=>setThemePage(1),[themeSearch,themePageSize]); useEffect(()=>setValuePage(1),[valueSearch,valuePageSize,selectedTheme]);
async function refreshThemes() {
const items = await api<ReferenceItem[]>("/api/v1/strategic-themes");
setThemes(items);
if (selectedTheme && !items.some(x => x.id === selectedTheme)) setSelectedTheme("");
}
async function refreshValues(id: string) {
setValues(id ? await api<ReferenceItem[]>(`/api/v1/innovation-values?strategicThemeId=${id}`) : []);
}
useEffect(() => { void refreshThemes().catch(e => setError(e.message)); }, []);
useEffect(() => { void refreshValues(selectedTheme).catch(e => setError(e.message)); }, [selectedTheme]);
async function addTheme(event: FormEvent<HTMLFormElement>) {
event.preventDefault(); setError("");
const form = event.currentTarget;
const title = new FormData(form).get("title");
await api("/api/v1/strategic-themes", { method: "POST", body: JSON.stringify({ title }) });
form.reset(); await refreshThemes();
}
async function addValue(event: FormEvent<HTMLFormElement>) {
event.preventDefault(); setError("");
const form = event.currentTarget;
const title = new FormData(form).get("title");
await api("/api/v1/innovation-values", { method: "POST", body: JSON.stringify({ title, strategicThemeId: selectedTheme }) });
form.reset(); await refreshValues(selectedTheme);
}
async function edit(item: ReferenceItem, kind: "theme" | "value") {
const title = window.prompt("عنوان جدید را وارد کنید:", item.title)?.trim();
if (!title || title === item.title) return;
setError("");
const path = kind === "theme" ? "strategic-themes" : "innovation-values";
await api(`/api/v1/${path}/${item.id}`, {
method: "PUT", body: JSON.stringify({ title, rowVersion: item.rowVersion })
});
if (kind === "theme") await refreshThemes(); else await refreshValues(selectedTheme);
}
return <>
<PageTitle title="مضامین و ارزش‌ها" description="مدیریت داده مرجع و رابطه معتبر ارزش با مضمون راهبردی"/>
{error && <ErrorBanner message={error}/>}
<div className="two-column">
<Card><div className="card-heading"><div><h3>مضامین راهبردی</h3><p>{themes.length} مورد فعال</p></div></div>
<form className="inline-form" onSubmit={e => void addTheme(e).catch(x => setError(x.message))}>
<Input name="title" maxLength={500} required placeholder="عنوان مضمون"/><Button><Plus size={17}/>افزودن</Button>
</form>
<ListToolbar search={themeSearch} onSearch={setThemeSearch} total={filteredThemes.length} placeholder="جست‌وجوی مضمون…"/>
{filteredThemes.length===0?<EmptyState title="مضمونی یافت نشد" description="عبارت جست‌وجو را تغییر دهید."/>:<><div className="reference-list">{visibleThemes.map(x =>
<div className={`reference-item ${selectedTheme === x.id ? "selected" : ""}`} key={x.id}>
<button className="reference-select" type="button" onClick={() => setSelectedTheme(x.id)}>
<span>{x.title}</span><small>مشاهده ارزش‌ها</small>
</button>
<span className="row-actions">
<button type="button" className="row-action" onClick={() => void edit(x, "theme").catch(reason => setError(reason.message))}><Pencil size={15}/>ویرایش</button>
<ArchiveButton entityType="StrategicTheme" id={x.id} rowVersion={x.rowVersion} onArchived={() => { setThemes(old => old.filter(item => item.id !== x.id)); if (selectedTheme === x.id) { setSelectedTheme(""); setValues([]); } }}/>
</span>
</div>)}</div><PaginationControls page={themePage} pageSize={themePageSize} total={filteredThemes.length} loading={false} onPage={setThemePage} onPageSize={setThemePageSize}/></>}
</Card>
<Card><div className="card-heading"><div><h3>ارزش‌های فناوری و نوآوری</h3><p>وابسته به مضمون انتخابی</p></div></div>
{!selectedTheme ? <EmptyState title="یک مضمون را انتخاب کنید" description="ارزش‌های نوآوری در چارچوب مضمون انتخاب‌شده ثبت می‌شوند."/> : <>
<form className="inline-form" onSubmit={e => void addValue(e).catch(x => setError(x.message))}>
<Field label=""><Input name="title" maxLength={500} required placeholder="عنوان ارزش"/></Field><Button><Plus size={17}/>افزودن</Button>
</form>
<ListToolbar search={valueSearch} onSearch={setValueSearch} total={filteredValues.length} placeholder="جست‌وجوی ارزش…"/>
{filteredValues.length===0?<EmptyState title="ارزشی یافت نشد" description="برای این مضمون موردی ثبت نشده یا جست‌وجو نتیجه ندارد."/>:<><div className="reference-list simple">{visibleValues.map(x =>
<div className="reference-item" key={x.id}><span>{x.title}</span><span className="row-actions">
<button type="button" className="row-action" onClick={() => void edit(x, "value").catch(reason => setError(reason.message))}><Pencil size={15}/>ویرایش</button>
<ArchiveButton entityType="InnovationValue" id={x.id} rowVersion={x.rowVersion} onArchived={() => setValues(old => old.filter(item => item.id !== x.id))}/>
</span></div>)}</div><PaginationControls page={valuePage} pageSize={valuePageSize} total={filteredValues.length} loading={false} onPage={setValuePage} onPageSize={setValuePageSize}/></>}
</>}
</Card>
</div>
</>;
}