refactor(web): 迁移 3 个健康页面错误处理到 useApiRequest — 消除内联 catch/message.error
- PatientList: handleCreateOrEdit/handleDelete/openEditModal 使用 execute - AppointmentList: handleStatusChange(2处)/handleSubmit 使用 execute - FollowUpTaskList: handleCreate/handleRecordSubmit/handleAssign/handleDelete 使用 execute - 移除不再需要的 message 导入(PatientList/FollowUpTaskList)
This commit is contained in:
@@ -9,7 +9,6 @@ import {
|
||||
Select,
|
||||
Popconfirm,
|
||||
DatePicker,
|
||||
message,
|
||||
} from 'antd';
|
||||
import {
|
||||
PlusOutlined,
|
||||
@@ -29,6 +28,7 @@ import { AuthButton } from '../../components/AuthButton';
|
||||
import { PageContainer } from '../../components/PageContainer';
|
||||
import { DrawerForm } from '../../components/DrawerForm';
|
||||
import { usePaginatedData } from '../../hooks/usePaginatedData';
|
||||
import { useApiRequest } from '../../hooks/useApiRequest';
|
||||
import { calcAge, formatDateTime } from '../../utils/format';
|
||||
import { dayjs } from '../../utils/dayjs';
|
||||
|
||||
@@ -53,6 +53,9 @@ export default function PatientList() {
|
||||
const [editingPatient, setEditingPatient] = useState<PatientDetail | null>(null);
|
||||
const [selectedRowKeys, setSelectedRowKeys] = useState<string[]>([]);
|
||||
|
||||
// ---- API 请求 Hook ----
|
||||
const { execute } = useApiRequest();
|
||||
|
||||
// ---- 分页数据 Hook ----
|
||||
const {
|
||||
data: patients,
|
||||
@@ -125,39 +128,36 @@ export default function PatientList() {
|
||||
notes: values.notes as string | undefined,
|
||||
};
|
||||
|
||||
try {
|
||||
if (editingPatient) {
|
||||
const req: UpdatePatientReq & { version: number } = {
|
||||
...payload,
|
||||
version: editingPatient.version,
|
||||
};
|
||||
await patientApi.update(editingPatient.id, req);
|
||||
message.success('患者信息更新成功');
|
||||
} else {
|
||||
const successMsg = editingPatient ? '患者信息更新成功' : '患者创建成功';
|
||||
const result = await execute(
|
||||
async () => {
|
||||
if (editingPatient) {
|
||||
const req: UpdatePatientReq & { version: number } = {
|
||||
...payload,
|
||||
version: editingPatient.version,
|
||||
};
|
||||
return patientApi.update(editingPatient.id, req);
|
||||
}
|
||||
const req: CreatePatientReq = payload;
|
||||
await patientApi.create(req);
|
||||
message.success('患者创建成功');
|
||||
}
|
||||
return patientApi.create(req);
|
||||
},
|
||||
successMsg,
|
||||
'操作失败',
|
||||
);
|
||||
if (result !== null) {
|
||||
closeModal();
|
||||
refresh();
|
||||
} catch (err: unknown) {
|
||||
const errorMsg =
|
||||
(err as { response?: { data?: { message?: string } } })?.response?.data
|
||||
?.message || '操作失败';
|
||||
message.error(errorMsg);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (id: string) => {
|
||||
try {
|
||||
const patient = patients.find((p) => p.id === id);
|
||||
const version = patient?.version ?? 0;
|
||||
await patientApi.delete(id, version);
|
||||
message.success('患者已删除');
|
||||
refresh();
|
||||
} catch {
|
||||
message.error('删除失败');
|
||||
}
|
||||
const patient = patients.find((p) => p.id === id);
|
||||
const version = patient?.version ?? 0;
|
||||
const result = await execute(
|
||||
() => patientApi.delete(id, version),
|
||||
'患者已删除',
|
||||
);
|
||||
if (result !== null) refresh();
|
||||
};
|
||||
|
||||
const openCreateModal = () => {
|
||||
@@ -166,12 +166,14 @@ export default function PatientList() {
|
||||
};
|
||||
|
||||
const openEditModal = async (record: PatientListItem) => {
|
||||
try {
|
||||
const detail = await patientApi.get(record.id);
|
||||
const detail = await execute(
|
||||
() => patientApi.get(record.id),
|
||||
undefined,
|
||||
'获取患者详情失败',
|
||||
);
|
||||
if (detail) {
|
||||
setEditingPatient(detail);
|
||||
setModalOpen(true);
|
||||
} catch {
|
||||
message.error('获取患者详情失败');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user