refactor(web): 迁移 3 个健康页面错误处理到 useApiRequest — 消除内联 catch/message.error
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled

- PatientList: handleCreateOrEdit/handleDelete/openEditModal 使用 execute
- AppointmentList: handleStatusChange(2处)/handleSubmit 使用 execute
- FollowUpTaskList: handleCreate/handleRecordSubmit/handleAssign/handleDelete 使用 execute
- 移除不再需要的 message 导入(PatientList/FollowUpTaskList)
This commit is contained in:
iven
2026-04-28 19:24:07 +08:00
parent 40a71e5a1c
commit 679d83d3b6
3 changed files with 112 additions and 105 deletions

View File

@@ -9,7 +9,6 @@ import {
DatePicker,
Space,
Popconfirm,
message,
} from 'antd';
import { PlusOutlined, EditOutlined, SwapOutlined, DeleteOutlined } from '@ant-design/icons';
import type { ColumnsType, TablePaginationConfig } from 'antd/es/table';
@@ -24,6 +23,7 @@ import { EntityName } from '../../components/EntityName';
import { DrawerForm } from '../../components/DrawerForm';
import { formatDate, formatDateTime } from '../../utils/format';
import { usePaginatedData } from '../../hooks/usePaginatedData';
import { useApiRequest } from '../../hooks/useApiRequest';
const STATUS_OPTIONS = [
{ value: 'pending', label: '待处理' },
@@ -90,6 +90,8 @@ export default function FollowUpTaskList() {
defaultFilters: {},
});
const { execute } = useApiRequest();
// Create task modal
const [createOpen, setCreateOpen] = useState(false);
const [createLoading, setCreateLoading] = useState(false);
@@ -113,26 +115,30 @@ export default function FollowUpTaskList() {
// Create task
const handleCreate = async () => {
let values: CreateFollowUpTaskReq;
try {
const values = await createForm.validateFields();
setCreateLoading(true);
const plannedDate = values.planned_date;
await followUpApi.createTask({
values = await createForm.validateFields();
} catch (err: unknown) {
if (err && typeof err === 'object' && 'errorFields' in err) return;
return;
}
setCreateLoading(true);
const plannedDate = values.planned_date;
const result = await execute(
() => followUpApi.createTask({
patient_id: values.patient_id,
follow_up_type: values.follow_up_type,
planned_date: dayjs.isDayjs(plannedDate) ? plannedDate.format('YYYY-MM-DD') : plannedDate,
assigned_to: values.assigned_to,
content_template: values.content_template,
});
message.success('随访任务创建成功');
}),
'随访任务创建成功',
);
setCreateLoading(false);
if (result !== null) {
setCreateOpen(false);
createForm.resetFields();
refresh(page);
} catch (err: unknown) {
if (err && typeof err === 'object' && 'errorFields' in err) return; // form validation
message.error('创建随访任务失败');
} finally {
setCreateLoading(false);
}
};
@@ -144,9 +150,9 @@ export default function FollowUpTaskList() {
const handleRecordSubmit = async (values: Record<string, unknown>) => {
if (!activeTask) return;
try {
setRecordLoading(true);
await followUpApi.createRecord(activeTask.id, {
setRecordLoading(true);
const result = await execute(
() => followUpApi.createRecord(activeTask.id, {
executed_date: (values.executed_date as dayjs.Dayjs).format('YYYY-MM-DD'),
result: values.result as string,
patient_condition: values.patient_condition as string,
@@ -154,15 +160,14 @@ export default function FollowUpTaskList() {
next_follow_up_date: values.next_follow_up_date
? (values.next_follow_up_date as dayjs.Dayjs).format('YYYY-MM-DD')
: undefined,
});
message.success('随访记录填写成功');
}),
'随访记录填写成功',
);
setRecordLoading(false);
if (result !== null) {
setRecordOpen(false);
setActiveTask(null);
refresh(page);
} catch {
message.error('填写随访记录失败');
} finally {
setRecordLoading(false);
}
};
@@ -178,35 +183,37 @@ export default function FollowUpTaskList() {
const handleAssign = async () => {
if (!assignTask) return;
let values: { assigned_to: string };
try {
const values = await assignForm.validateFields();
setAssignLoading(true);
const req: UpdateFollowUpTaskReq & { version: number } = {
assigned_to: values.assigned_to,
version: assignTask.version,
};
await followUpApi.updateTask(assignTask.id, req);
message.success('分配成功');
values = await assignForm.validateFields();
} catch (err: unknown) {
if (err && typeof err === 'object' && 'errorFields' in err) return;
return;
}
setAssignLoading(true);
const req: UpdateFollowUpTaskReq & { version: number } = {
assigned_to: values.assigned_to,
version: assignTask.version,
};
const result = await execute(
() => followUpApi.updateTask(assignTask.id, req),
'分配成功',
);
setAssignLoading(false);
if (result !== null) {
setAssignOpen(false);
setAssignTask(null);
refresh(page);
} catch (err: unknown) {
if (err && typeof err === 'object' && 'errorFields' in err) return;
message.error('分配失败');
} finally {
setAssignLoading(false);
}
};
// Delete
const handleDelete = async (record: FollowUpTask) => {
try {
await followUpApi.deleteTask(record.id, record.version);
message.success('删除成功');
refresh(page);
} catch {
message.error('删除失败');
}
const result = await execute(
() => followUpApi.deleteTask(record.id, record.version),
'删除成功',
);
if (result !== null) refresh(page);
};
// --- Columns ---