const [domain_id,setDomainId]=useState<number>(doms[0]?.id||0);
const [name,setName]=useState(""); const [description,setDesc]=useState("");
const [status,setStatus]=useState("available"); const [version,setVersion]=useState("1.0"); const [creator,setCreator]=useState(CURRENT_USER_EMAIL);
- const save=async()=>{ try{ const r:any=await fetchDb("/domain-templates",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({domain_id,name,description,status,version,creator})}); notify("已创建"); onCreated(r.id); }catch(e:any){ notify("创建失败:"+e.message); } };
+ useEffect(()=>{ if(doms.length && !domain_id) setDomainId(doms[0].id); },[doms]);
+ const save=async()=>{ if(!domain_id){ notify("请选择所属领域"); return; } try{ const r:any=await fetchDb("/domain-templates",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({domain_id,name,description,status,version,creator})}); notify("已创建"); onCreated(r.id); }catch(e:any){ notify("创建失败:"+e.message); } };
return <Panel title="新增领域方案"><div className="form-grid">
<label>所属领域<select value={domain_id} onChange={e=>setDomainId(Number(e.target.value))}>{doms.map(d=><option key={d.id} value={d.id}>{d.name}</option>)}</select></label>
<label>方案名称<input value={name} onChange={e=>setName(e.target.value)} placeholder="如:自来水厂标准方案"/></label>
const [loading,setLoading]=useState(false);
const [m,setM]=useState<any>(null);
const loadDoms=async()=>{ try{ setDoms(await fetchDb("/domains") as any); }catch{} };
- const load=async()=>{ if(id==null){ setT(null); return; } setLoading(true); try{ setT(await fetchDb(`/domain-templates/${id}`) as any); }catch(e:any){ notify("加载失败:"+e.message); } finally{ setLoading(false); } };
+ const load=async(silent=false)=>{ if(id==null){ setT(null); return; } if(!silent) setLoading(true); try{ setT(await fetchDb(`/domain-templates/${id}`) as any); }catch(e:any){ notify("加载失败:"+e.message); } finally{ if(!silent) setLoading(false); } };
useEffect(()=>{ loadDoms(); load(); },[id]);
useEffect(()=>{ if(t) setM({domain_id:t.domain_id,name:t.name,description:t.description||"",status:t.status,version:t.version,creator:t.creator||""}); },[t?.id]);
if(id==null) return <CreateTemplate doms={doms} notify={notify} onCreated={onCreated}/>;
<div style={{gridColumn:"1 / -1"}}><Button onClick={saveMeta}>保存模板信息</Button></div>
</div></Panel>
<Panel title={`选用设备模版(${t.devices.length})`}>
- <DeviceTemplatePicker tmplId={id} go={go} selectedIds={t.devices.map((d:any)=>d.id)} notify={notify} onChange={load}/>
+ <DeviceTemplatePicker tmplId={id} go={go} selectedIds={t.devices.map((d:any)=>d.id)} notify={notify} onChange={()=>load(true)}/>
</Panel>
<Panel title={`领域页面(${t.pages.length})`}><div className="page-list">
{t.pages.map((p:any)=><PageRow key={p.id} p={p} notify={notify} onChange={load}/>)}