import { ArrowRight, Save } from "lucide-react"; import { focusAdminServerError, validateAdminForm } from "../lib/validateForm"; import { FormErrorProvider } from "../components/Ui"; import { useEffect, useState, type FormEvent } from "react"; import { Link, useNavigate, useParams } from "react-router"; import { Button, Card, ErrorBanner, Field, Input, PageTitle, Select, } from "../components/Ui"; import { ApiError, adminFieldErrors, api } from "../lib/api"; import type { EcosystemActorItem } from "../types"; import { actorLabels } from "./EcosystemPage"; import { isMoneyField, unitHint } from "../lib/fieldUnits"; import { FormTabs } from "../components/FormTabs"; type Kind = "text" | "integer" | "decimal" | "date" | "url" | "email"; type Definition = [string, string, Kind?]; const common: Definition[] = [ ["fullName", "نام کامل"], ["collaborationStartDate", "تاریخ شروع همکاری", "date"], ["establishmentYear", "سال تأسیس", "integer"], ["managerName", "مدیر/مدیرعامل"], ["organizationType", "نوع سازمان/مرکز"], ["activityDomain", "حوزه فعالیت"], ["specialtyField", "حوزه تخصصی"], ["collaborationField", "حوزه همکاری"], ["aboutCollaboration", "درباره همکاری"], ["mouCount", "تعداد تفاهم‌نامه", "integer"], ["collaborationHistoryCount", "تعداد سابقه همکاری", "integer"], ["staffCount", "تعداد نیروی انسانی", "integer"], ["location", "محل استقرار"], ["ownershipType", "نوع مالکیت"], ["website", "وب‌سایت", "url"], ["email", "رایانامه", "email"], ["phone", "تلفن"], ["mobile", "همراه"], ["transactionVolume", "حجم تراکنش", "decimal"], ]; const specific: Record = { 1: [ ["relatedFaculty", "دانشکده مرتبط"], ["jointProjectsCount", "پروژه‌های مشترک صنعتی", "integer"], ], 2: [ ["experienceYears", "سابقه (سال)", "integer"], ["bachelorField", "رشته کارشناسی"], ["masterField", "رشته کارشناسی ارشد"], ["phdSpecialization", "تخصص دکتری"], ["postdocSpecialization", "تخصص پسادکتری"], ["socialPage", "صفحه اجتماعی", "url"], ], 3: [ ["experienceYears", "میزان تجربه", "integer"], ["collaborationHistory", "سابقه همکاری"], ], 4: [["investmentStage", "مرحله سرمایه‌گذاری"]], 5: [ ["experienceYears", "میزان تجربه", "integer"], ["collaborationHistory", "سابقه همکاری"], ], 6: [ ["fundCapitalVolume", "حجم سرمایه صندوق", "decimal"], ["capitalRaisedFromFund", "سرمایه جذب‌شده", "decimal"], ], 7: [["supportedStartupsCount", "استارتاپ‌های تحت حمایت", "integer"]], 8: [["residentCompaniesLevel", "سطح شرکت‌های مستقر"]], }; export function EcosystemFormPage() { const { id } = useParams(); const editing = Boolean(id); const navigate = useNavigate(); const [type, setType] = useState("1"); const [details, setDetails] = useState(null); const [busy, setBusy] = useState(false); const [loading, setLoading] = useState(editing); const [error, setError] = useState(""); const [fieldErrors, setFieldErrors] = useState>({}); const [activeTab, setActiveTab] = useState("common"); useEffect(() => { if (!editing) return; setLoading(true); void api(`/api/v1/ecosystem-actors/${id}`) .then((x) => { setDetails(x); setType(String(x.actorType)); }) .catch((e) => setError(e.message)) .finally(() => setLoading(false)); }, [editing, id]); const value = (key: string) => details?.[key] == null ? "" : String(details[key]); async function submit(event: FormEvent) { event.preventDefault(); if (!validateAdminForm(event.currentTarget, setActiveTab)) return; setBusy(true); setError(""); setFieldErrors({}); const f = new FormData(event.currentTarget); const payload: Record = { actorType: Number(type), status: Number(f.get("status")), }; for (const [key, , kind = "text"] of [ ...common, ...Object.values(specific).flat(), ]) { if (key in payload) continue; const raw = String(f.get(key) ?? ""); payload[key] = kind === "integer" ? (raw ? Number(raw) : null) : raw || null; } payload.fullName = String(f.get("fullName") ?? ""); payload.collaborationStartDate = String( f.get("collaborationStartDate") ?? "", ); if (editing) payload.rowVersion = details?.rowVersion; try { await api(`/api/v1/ecosystem-actors${editing ? `/${id}` : ""}`, { method: editing ? "PUT" : "POST", body: JSON.stringify(payload), }); navigate("/admin/ecosystem", { replace: true }); } catch (reason) { if (reason instanceof ApiError) { const errors = adminFieldErrors(reason.problem); setFieldErrors(errors); focusAdminServerError(errors, setActiveTab); } setError( reason instanceof ApiError ? reason.message : "ذخیره بازیگر ناموفق بود.", ); } finally { setBusy(false); } } if (loading) return (

در حال دریافت اطلاعات…

); return ( <> بازگشت } /> {error && } {editing && }
۰۱

مشخصات مشترک

فیلدهای مشترک هشت فرایند 1217 تا 1224

{common.map((def) => ( ))}
۰۲

اطلاعات وابسته به نوع

نوع بازیگر مبنای نمایش فیلدهای اختصاصی است.

{(specific[Number(type)] ?? []).map((def) => ( ))}
{editing && ( مدیریت تصویر یا لوگوی بازیگر )}
نوع انتخابی: {actorLabels[Number(type)]}
); } function DynamicField({ definition, defaultValue, }: { definition: Definition; defaultValue: string; }) { const [key, label, kind = "text"] = definition; const required = key === "fullName" || key === "collaborationStartDate"; return ( ); }