270 lines
9.5 KiB
TypeScript
270 lines
9.5 KiB
TypeScript
import { ArrowRight, Save } from "lucide-react";
|
||
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, 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<number, Definition[]> = {
|
||
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<EcosystemActorItem | null>(null);
|
||
const [busy, setBusy] = useState(false);
|
||
const [loading, setLoading] = useState(editing);
|
||
const [error, setError] = useState("");
|
||
const [activeTab, setActiveTab] = useState("common");
|
||
useEffect(() => {
|
||
if (!editing) return;
|
||
setLoading(true);
|
||
void api<EcosystemActorItem>(`/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<HTMLFormElement>) {
|
||
event.preventDefault();
|
||
setBusy(true);
|
||
setError("");
|
||
const f = new FormData(event.currentTarget);
|
||
const payload: Record<string, unknown> = {
|
||
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) {
|
||
setError(
|
||
reason instanceof ApiError
|
||
? reason.message
|
||
: "ذخیره بازیگر ناموفق بود.",
|
||
);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
if (loading)
|
||
return (
|
||
<Card>
|
||
<p>در حال دریافت اطلاعات…</p>
|
||
</Card>
|
||
);
|
||
return (
|
||
<>
|
||
<PageTitle
|
||
title={editing ? "ویرایش بازیگر زیستبوم" : "بازیگر جدید زیستبوم"}
|
||
description="فرم وابسته به نوع؛ دادهها در مدل typed و با کنترل همزمانی ذخیره میشوند"
|
||
action={
|
||
<Link className="text-link" to="/admin/ecosystem">
|
||
<ArrowRight size={17} />
|
||
بازگشت
|
||
</Link>
|
||
}
|
||
/>
|
||
{error && <ErrorBanner message={error} />}
|
||
{editing && <FormTabs active={activeTab} onChange={setActiveTab} tabs={[
|
||
{id:"common",label:"مشخصات مشترک"},{id:"specific",label:"اطلاعات اختصاصی"},{id:"image",label:"تصویر یا لوگو"},
|
||
]}/>}
|
||
<form className="form-stack" onSubmit={submit} key={id ?? "new"}>
|
||
<Card className={editing && activeTab !== "common" ? "tab-hidden" : ""}>
|
||
<div className="section-heading">
|
||
<span>۰۱</span>
|
||
<div>
|
||
<h3>مشخصات مشترک</h3>
|
||
<p>فیلدهای مشترک هشت فرایند 1217 تا 1224</p>
|
||
</div>
|
||
</div>
|
||
<div className="form-grid cols-3">
|
||
<Field
|
||
label="نوع بازیگر"
|
||
hint={editing ? "نوع پس از ایجاد تغییر نمیکند" : undefined}
|
||
>
|
||
<Select
|
||
value={type}
|
||
disabled={editing}
|
||
onChange={(e) => setType(e.target.value)}
|
||
>
|
||
{actorLabels.slice(1).map((x, i) => (
|
||
<option key={x} value={i + 1}>
|
||
{x}
|
||
</option>
|
||
))}
|
||
</Select>
|
||
</Field>
|
||
{common.map((def) => (
|
||
<DynamicField
|
||
key={def[0]}
|
||
definition={def}
|
||
defaultValue={value(def[0])}
|
||
/>
|
||
))}
|
||
<Field label="وضعیت فنی">
|
||
<Select name="status" defaultValue={value("status") || "0"}>
|
||
<option value="0">پیشنویس</option>
|
||
<option value="1">انتشار</option>
|
||
</Select>
|
||
</Field>
|
||
</div>
|
||
</Card>
|
||
<Card className={editing && activeTab !== "specific" ? "tab-hidden" : ""}>
|
||
<div className="section-heading green-heading">
|
||
<span>۰۲</span>
|
||
<div>
|
||
<h3>اطلاعات وابسته به نوع</h3>
|
||
<p>نوع بازیگر مبنای نمایش فیلدهای اختصاصی است.</p>
|
||
</div>
|
||
</div>
|
||
<div className="form-grid cols-3">
|
||
{(specific[Number(type)] ?? []).map((def) => (
|
||
<DynamicField
|
||
key={def[0]}
|
||
definition={def}
|
||
defaultValue={value(def[0])}
|
||
/>
|
||
))}
|
||
</div>
|
||
</Card>
|
||
{editing && (
|
||
<Card className={activeTab !== "image" ? "tab-hidden" : ""}>
|
||
<Link className="text-link" to={`/admin/files/EcosystemActor/${id}`}>
|
||
مدیریت تصویر یا لوگوی بازیگر
|
||
</Link>
|
||
</Card>
|
||
)}
|
||
<div className={`form-actions${editing && activeTab === "image" ? " tab-hidden" : ""}`}>
|
||
<span>نوع انتخابی: {actorLabels[Number(type)]}</span>
|
||
<Button type="submit" disabled={busy}>
|
||
<Save size={18} />
|
||
{busy ? "در حال ذخیره…" : editing ? "ذخیره تغییرات" : "ثبت بازیگر"}
|
||
</Button>
|
||
</div>
|
||
</form>
|
||
</>
|
||
);
|
||
}
|
||
|
||
function DynamicField({
|
||
definition,
|
||
defaultValue,
|
||
}: {
|
||
definition: Definition;
|
||
defaultValue: string;
|
||
}) {
|
||
const [key, label, kind = "text"] = definition;
|
||
const required = key === "fullName" || key === "collaborationStartDate";
|
||
return (
|
||
<Field label={label} hint={unitHint("ecosystem", key)}>
|
||
<Input
|
||
name={key}
|
||
groupDigits={isMoneyField("ecosystem", key)}
|
||
required={required}
|
||
defaultValue={defaultValue}
|
||
type={
|
||
kind === "date"
|
||
? "date"
|
||
: kind === "integer"
|
||
? "number"
|
||
: kind === "url"
|
||
? "url"
|
||
: kind === "email"
|
||
? "email"
|
||
: "text"
|
||
}
|
||
min={kind === "integer" ? 0 : undefined}
|
||
inputMode={kind === "decimal" ? "decimal" : undefined}
|
||
dir={["decimal", "url", "email"].includes(kind) ? "ltr" : undefined}
|
||
/>
|
||
</Field>
|
||
);
|
||
}
|
||
|
||
|