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([]); const [values, setValues] = useState([]); 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("/api/v1/strategic-themes"); setThemes(items); if (selectedTheme && !items.some(x => x.id === selectedTheme)) setSelectedTheme(""); } async function refreshValues(id: string) { setValues(id ? await api(`/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) { 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) { 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 <> {error && }

مضامین راهبردی

{themes.length} مورد فعال

void addTheme(e).catch(x => setError(x.message))}>
{filteredThemes.length===0?:<>
{visibleThemes.map(x =>
{ setThemes(old => old.filter(item => item.id !== x.id)); if (selectedTheme === x.id) { setSelectedTheme(""); setValues([]); } }}/>
)}
}

ارزش‌های فناوری و نوآوری

وابسته به مضمون انتخابی

{!selectedTheme ? : <>
void addValue(e).catch(x => setError(x.message))}>
{filteredValues.length===0?:<>
{visibleValues.map(x =>
{x.title} setValues(old => old.filter(item => item.id !== x.id))}/>
)}
} }
; }