feat(modeler): ModelerPage shell + /modeler route + entity list + new-entity dialog
canSubmit 去掉 rows.length>0 约束以满足 Task 15 测试(有效表名即可提交);字段表 Task 16 补。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
165
src/pages/ModelerPage.tsx
Normal file
165
src/pages/ModelerPage.tsx
Normal file
@@ -0,0 +1,165 @@
|
||||
// 建模器 —— NocoBase 式实体建模入口(五环第①环:元数据→表)。
|
||||
// 仅管理员可用。展示已有实体清单 + 「新建实体」Dialog(表名/显示名/字段表)。
|
||||
// v1 只建新不改表(DECISION:no ALTER)。字段表与引用列选择器见同文件下方组件。
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Dialog } from '../components/ui/Dialog';
|
||||
import { getProfile } from '../auth/token';
|
||||
import {
|
||||
listEntities,
|
||||
createEntity,
|
||||
type EntitySummary,
|
||||
type EntityAttr,
|
||||
} from '../api/meta';
|
||||
import { FieldTable, buildAttribute, type FieldRow } from './ModelerFields';
|
||||
|
||||
export function validateTableName(t: string): boolean {
|
||||
return /^[A-Z][A-Z0-9_]{2,40}$/.test(t);
|
||||
}
|
||||
|
||||
export function ModelerPage() {
|
||||
const profile = getProfile();
|
||||
const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN';
|
||||
|
||||
const [entities, setEntities] = useState<EntitySummary[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState('');
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
// 新建表单态
|
||||
const [tablename, setTablename] = useState('');
|
||||
const [name, setName] = useState('');
|
||||
const [rows, setRows] = useState<FieldRow[]>([]);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [submitErr, setSubmitErr] = useState('');
|
||||
|
||||
function reload() {
|
||||
setLoading(true);
|
||||
listEntities()
|
||||
.then((es) => setEntities(es))
|
||||
.catch((e: Error) => setError(e.message ?? '实体加载失败'))
|
||||
.finally(() => setLoading(false));
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!isAdmin) return;
|
||||
reload();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [isAdmin]);
|
||||
|
||||
if (!isAdmin) {
|
||||
return <div style={{ padding: 24, color: 'var(--text-3)' }}>建模器仅管理员可用。</div>;
|
||||
}
|
||||
|
||||
const tableValid = validateTableName(tablename);
|
||||
// 引用行必须选了目标表才合法(Task 17 填充 referenceTable)
|
||||
const rowsValid = rows.every((r) => r.type !== '引用' || !!r.referenceTable);
|
||||
const canSubmit = tableValid && rowsValid && !submitting;
|
||||
|
||||
function openDialog() {
|
||||
setTablename('');
|
||||
setName('');
|
||||
setRows([]);
|
||||
setSubmitErr('');
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
setSubmitting(true);
|
||||
setSubmitErr('');
|
||||
try {
|
||||
const attributes: EntityAttr[] = rows.map(buildAttribute);
|
||||
await createEntity({ tablename, name: name || undefined, attributes });
|
||||
setOpen(false);
|
||||
reload();
|
||||
} catch (e) {
|
||||
setSubmitErr((e as Error).message ?? '建表失败');
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ height: '100%', overflow: 'auto', padding: 'var(--pad-lg)' }} data-testid="modeler">
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 16, marginBottom: 16, flexWrap: 'wrap' }}>
|
||||
<div>
|
||||
<h2 style={{ margin: 0, fontSize: 18, fontWeight: 600, color: 'var(--text)' }}>建模器</h2>
|
||||
<div className="sub-text" style={{ marginTop: 4 }}>
|
||||
新建实体(建表)。v1 只建新不改表 · 标准列由后端自动追加
|
||||
</div>
|
||||
</div>
|
||||
<div style={{ flex: 1 }} />
|
||||
<button className="btn btn-primary" data-testid="modeler-new-entity" onClick={openDialog}>
|
||||
新建实体
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div style={{ padding: 24, color: 'var(--text-3)' }}>加载中…</div>
|
||||
) : error ? (
|
||||
<div style={{ padding: 16, color: 'var(--err)' }}>实体加载失败:{error}</div>
|
||||
) : entities.length === 0 ? (
|
||||
<div className="empty">尚无实体,点「新建实体」开始</div>
|
||||
) : (
|
||||
<div className="card" style={{ padding: 0 }}>
|
||||
<table style={{ width: '100%', borderCollapse: 'collapse', fontSize: 'var(--fs)' }}>
|
||||
<thead>
|
||||
<tr style={{ textAlign: 'left', color: 'var(--text-3)' }}>
|
||||
<th style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>表名</th>
|
||||
<th style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>显示名</th>
|
||||
<th style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>实体ID</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{entities.map((e) => (
|
||||
<tr key={e.entityId} data-testid="modeler-entity-row">
|
||||
<td style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>{e.tablename}</td>
|
||||
<td style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>{e.name}</td>
|
||||
<td style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)', color: 'var(--text-3)' }}>{e.entityId}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{open ? (
|
||||
<Dialog
|
||||
title="新建实体"
|
||||
width={720}
|
||||
onClose={() => setOpen(false)}
|
||||
foot={
|
||||
<>
|
||||
<button className="btn" onClick={() => setOpen(false)}>取消</button>
|
||||
<button className="btn btn-primary" data-testid="modeler-submit" disabled={!canSubmit} onClick={submit}>
|
||||
{submitting ? '建表中…' : '建表'}
|
||||
</button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<div style={{ display: 'grid', gap: 12 }}>
|
||||
<label style={{ display: 'grid', gap: 4 }}>
|
||||
<span className="sub-text">表名(大写字母开头,仅大写字母/数字/下划线,3–41 位)</span>
|
||||
<input
|
||||
data-testid="modeler-tablename"
|
||||
value={tablename}
|
||||
onChange={(ev) => setTablename(ev.target.value.toUpperCase())}
|
||||
placeholder="OA_DEMO"
|
||||
/>
|
||||
{tablename && !tableValid ? (
|
||||
<span style={{ color: 'var(--err)', fontSize: 11 }}>表名格式不合法</span>
|
||||
) : null}
|
||||
</label>
|
||||
<label style={{ display: 'grid', gap: 4 }}>
|
||||
<span className="sub-text">显示名(可选)</span>
|
||||
<input value={name} onChange={(ev) => setName(ev.target.value)} placeholder="演示维护" />
|
||||
</label>
|
||||
|
||||
<FieldTable rows={rows} onChange={setRows} />
|
||||
|
||||
{submitErr ? <div style={{ color: 'var(--err)', fontSize: 12 }}>{submitErr}</div> : null}
|
||||
</div>
|
||||
</Dialog>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user