F2-7: 审批办理 UI 前端——ApprovalBar + trace 时间线 + e2e

- src/api/approval.ts:completeTask/delegateTask/fetchTrace API
- src/components/ApprovalBar.tsx:办理动作条(同意/驳回/转办+意见输入;
  流程轨迹抽屉 TraceTimeline 时间线;转办 Drawer 弹窗)
- src/pages/TodoPage.tsx:行内展开 ApprovalBar,办理后刷新列表+回调
- e2e/approval.spec.ts:4 个场景(UI展示/complete链路/trace/delegate),
  自起 E2E- 前缀流程实例,测后 psql 清理,4/4 通过
- docs/P1/F2-7审批UI报告.md:端点增补清单、e2e 证据、环境限制说明

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 23:09:24 +08:00
parent 7b5d9d2640
commit ab14598524
5 changed files with 823 additions and 50 deletions

View File

@@ -0,0 +1,309 @@
// ApprovalBar —— 办理动作条(同意/驳回/转办 + 审批意见 + 流程轨迹抽屉)
// 布局参照原型 ApprovalDetail 操作条:底部固定条,意见输入区+三按钮+轨迹入口
import { useState } from 'react';
import { Drawer } from './ui/Dialog';
import { toast } from './ui/Toast';
import { completeTask, delegateTask, fetchTrace } from '../api/approval';
import type { TraceNode } from '../api/approval';
export interface ApprovalBarProps {
taskId: string;
processInstanceId?: string;
businessKey?: string;
tableName?: string;
/** 办理成功后回调(刷新待办列表/铃铛) */
onCompleted?: () => void;
}
// ---------------------------------------------------------------------------
// 流程轨迹时间线
// ---------------------------------------------------------------------------
function TraceTimeline({ nodes }: { nodes: TraceNode[] }) {
if (nodes.length === 0) {
return (
<div style={{ color: 'var(--text-3)', padding: '24px 0', textAlign: 'center' }}>
暂无轨迹记录
</div>
);
}
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: 0 }}>
{nodes.map((node, idx) => {
const isLast = idx === nodes.length - 1;
const resultMap: Record<string, { text: string; color: string }> = {
agree: { text: '同意', color: 'var(--ok)' },
approved: { text: '同意', color: 'var(--ok)' },
reject: { text: '驳回', color: 'var(--err)' },
rejected: { text: '驳回', color: 'var(--err)' },
transfer: { text: '转办', color: 'var(--warn)' },
complete: { text: '完成', color: 'var(--ok)' },
};
const res = node.result ? (resultMap[node.result.toLowerCase()] ?? { text: node.result, color: 'var(--text-2)' }) : null;
const dotColor = node.current ? 'var(--brand)' : node.endTime ? 'var(--ok)' : 'var(--text-3)';
return (
<div key={node.taskId ?? idx} style={{ display: 'flex', gap: 12 }}>
{/* 时间线竖线 + 圆点 */}
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', width: 20, flexShrink: 0 }}>
<div
style={{
width: 12,
height: 12,
borderRadius: '50%',
background: dotColor,
border: node.current ? '2px solid var(--brand)' : '2px solid transparent',
boxSizing: 'border-box',
marginTop: 4,
flexShrink: 0,
}}
/>
{!isLast && (
<div style={{ width: 2, flex: 1, background: 'var(--line)', minHeight: 24, margin: '2px 0' }} />
)}
</div>
{/* 节点内容 */}
<div style={{ paddingBottom: isLast ? 0 : 20, flex: 1, minWidth: 0 }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' }}>
<span style={{ fontWeight: 600, fontSize: 'var(--fs)' }}>
{node.taskName || '(未命名节点)'}
</span>
{node.current && (
<span className="tag tag-info" style={{ fontSize: 11 }}>当前</span>
)}
{res && (
<span style={{ fontSize: 12, color: res.color, fontWeight: 500 }}>{res.text}</span>
)}
</div>
<div style={{ fontSize: 'var(--fs-sm)', color: 'var(--text-2)', marginTop: 2 }}>
{node.assigneeName || node.assignee || '—'}
{node.startTime && (
<span style={{ marginLeft: 8, color: 'var(--text-3)' }}>
{new Date(node.startTime).toLocaleString('zh-CN', { hour12: false }).slice(0, 16)}
</span>
)}
</div>
{node.comment && (
<div
style={{
marginTop: 4,
padding: '4px 8px',
background: 'var(--bg-2)',
borderRadius: 'var(--radius)',
fontSize: 'var(--fs-sm)',
color: 'var(--text-1)',
wordBreak: 'break-all',
}}
>
{node.comment}
</div>
)}
</div>
</div>
);
})}
</div>
);
}
// ---------------------------------------------------------------------------
// 转办弹窗内容
// ---------------------------------------------------------------------------
function DelegateForm({
taskId,
onSuccess,
onCancel,
}: {
taskId: string;
onSuccess: () => void;
onCancel: () => void;
}) {
const [userId, setUserId] = useState('');
const [submitting, setSubmitting] = useState(false);
const submit = async () => {
if (!userId.trim()) {
toast('请输入转办人账号', 'warn');
return;
}
setSubmitting(true);
try {
await delegateTask(taskId, { delegateUserId: userId.trim() });
toast('转办成功');
onSuccess();
} catch {
toast('转办失败,请重试', 'err');
} finally {
setSubmitting(false);
}
};
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
<label style={{ fontSize: 'var(--fs-sm)', color: 'var(--text-2)' }}>
转办人账号(userId)
</label>
<input
className="input"
placeholder="请输入被转办人账号"
value={userId}
onChange={(e) => setUserId(e.target.value)}
autoFocus
/>
<div style={{ display: 'flex', gap: 8, justifyContent: 'flex-end', marginTop: 4 }}>
<button className="btn btn-sm" onClick={onCancel} disabled={submitting}>
取消
</button>
<button className="btn btn-sm btn-primary" onClick={submit} disabled={submitting}>
{submitting ? '提交中…' : '确认转办'}
</button>
</div>
</div>
);
}
// ---------------------------------------------------------------------------
// 主组件:ApprovalBar
// ---------------------------------------------------------------------------
export function ApprovalBar({
taskId,
processInstanceId,
businessKey,
tableName,
onCompleted,
}: ApprovalBarProps) {
const [comment, setComment] = useState('');
const [submitting, setSubmitting] = useState(false);
const [showTrace, setShowTrace] = useState(false);
const [showDelegate, setShowDelegate] = useState(false);
const [traceNodes, setTraceNodes] = useState<TraceNode[]>([]);
const [traceLoading, setTraceLoading] = useState(false);
const openTrace = async () => {
setShowTrace(true);
if (processInstanceId) {
setTraceLoading(true);
try {
const nodes = await fetchTrace(processInstanceId);
setTraceNodes(nodes);
} catch {
toast('轨迹加载失败', 'err');
} finally {
setTraceLoading(false);
}
}
};
const handleComplete = async (action: 'agree' | 'reject') => {
setSubmitting(true);
try {
await completeTask(taskId, {
variables: {
approveResult: action,
comment: comment.trim() || undefined,
},
processInstanceId,
businessKey,
tableName,
});
toast(action === 'agree' ? '已同意' : '已驳回');
if (onCompleted) onCompleted();
} catch {
toast('办理失败,请重试', 'err');
} finally {
setSubmitting(false);
}
};
return (
<>
{/* 办理条 */}
<div
data-testid="approval-bar"
style={{
borderTop: '1px solid var(--line)',
padding: '12px 16px',
background: 'var(--bg)',
display: 'flex',
flexDirection: 'column',
gap: 10,
}}
>
{/* 审批意见输入 */}
<textarea
data-testid="approval-comment"
className="input"
style={{ width: '100%', minHeight: 64, resize: 'vertical', boxSizing: 'border-box' }}
placeholder="审批意见(选填)"
value={comment}
onChange={(e) => setComment(e.target.value)}
disabled={submitting}
/>
{/* 操作按钮行 */}
<div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
<button
data-testid="btn-agree"
className="btn btn-primary"
onClick={() => handleComplete('agree')}
disabled={submitting}
>
{submitting ? '处理中…' : '同意'}
</button>
<button
data-testid="btn-reject"
className="btn"
style={{ background: 'var(--err-soft, #fff5f5)', color: 'var(--err)', borderColor: 'var(--err)' }}
onClick={() => handleComplete('reject')}
disabled={submitting}
>
驳回
</button>
<button
data-testid="btn-transfer"
className="btn"
onClick={() => setShowDelegate(true)}
disabled={submitting}
>
转办
</button>
{processInstanceId && (
<button
data-testid="btn-trace"
className="btn btn-text"
style={{ marginLeft: 'auto' }}
onClick={openTrace}
>
流程轨迹
</button>
)}
</div>
</div>
{/* 流程轨迹抽屉 */}
{showTrace && (
<Drawer title="流程轨迹" width={400} onClose={() => setShowTrace(false)}>
{traceLoading ? (
<div style={{ color: 'var(--text-3)', padding: 16 }}>加载中…</div>
) : (
<TraceTimeline nodes={traceNodes} />
)}
</Drawer>
)}
{/* 转办弹窗——用 Drawer 轻量实现 */}
{showDelegate && (
<Drawer title="转办" width={360} onClose={() => setShowDelegate(false)}>
<DelegateForm
taskId={taskId}
onSuccess={() => {
setShowDelegate(false);
if (onCompleted) onCompleted();
}}
onCancel={() => setShowDelegate(false)}
/>
</Drawer>
)}
</>
);
}