Browse Source

feat: 添加患者类型和身体部位的联动清理及请求验证

close #68

- src/states/bodyPartSlice.ts: 患者类型清空时清理身体部位状态
- src/states/patient/viewSelection/index.ts: 添加请求前置验证和协议/体位列表联动清空
- src/pages/patient/components/bodyPositionFilter.tsx: 添加前端双重验证
sw 1 week ago
parent
commit
75ec70c7f6

+ 13 - 7
src/pages/patient/components/bodyPositionFilter.tsx

@@ -69,13 +69,19 @@ const BodyPositionFilter: React.FC = () => {
                 ) || null;
               dispatch(setCurrentBodyPart(selectedBodyPart));
               // message.info(`得到对应的身体部位的ID:${selectedBodyPartId}`);
-              dispatch(
-                fetchViewsOrProtocols({
-                  selection: selectedViewOrProtocol,
-                  patientType: currentPatientType?.patient_type_id ?? null,
-                  bodyPart: selectedBodyPartId ?? null,
-                })
-              );
+
+              // 验证条件后再发起请求
+              if (currentPatientType?.patient_type_id && selectedBodyPartId) {
+                dispatch(
+                  fetchViewsOrProtocols({
+                    selection: selectedViewOrProtocol,
+                    patientType: currentPatientType.patient_type_id,
+                    bodyPart: selectedBodyPartId,
+                  })
+                );
+              } else {
+                message.warning('请先选择患者类型和身体部位');
+              }
             }}
             selectedId={null}
           />

+ 5 - 0
src/states/bodyPartSlice.ts

@@ -78,6 +78,11 @@ const bodyPartSlice = createSlice({
             (item) => item.patient_type === patientTypeId
           );
           console.log('过滤后的 bodyPart byPatientType:', state.byPatientType);
+        } else {
+          // 当患者类型被清空时,清空身体部位相关状态
+          state.byPatientType = [];
+          state.current = null;
+          console.log('患者类型已清空,身体部位信息已重置');
         }
       })
       .addCase(getBodyParts.pending, (state) => {

+ 18 - 0
src/states/patient/viewSelection/index.ts

@@ -53,6 +53,12 @@ export const fetchViewsOrProtocols = createAsyncThunk(
     try {
       console.log(`触发查询,查询条件是:${JSON.stringify(filter)}`);
 
+      // 验证必填条件
+      if (!filter.patientType || !filter.bodyPart) {
+        console.warn('缺少必填过滤条件:patientType 或 bodyPart');
+        return rejectWithValue('缺少必填过滤条件:患者类型和身体部位必须选择');
+      }
+
       if (filter.selection === 'protocol') {
         const response = await fetchProcedures(
           filter.patientType,
@@ -130,6 +136,12 @@ const viewSelectionSlice = createSlice({
           // console.log(`在view section中感知到 current patient type 变化: ${currentPatientType.patient_type_name}`);
           // 基于过滤条件查询协议或者体位
           state.currentPatientType = currentPatientType;
+        } else {
+          // 患者类型被清空,清空相关数据
+          state.currentPatientType = null;
+          state.protocols = [];
+          state.availableViews = [];
+          console.log('患者类型已清空,协议和体位列表已重置');
         }
       })
       .addCase(setCurrentBodyPart, (state, action) => {
@@ -137,6 +149,12 @@ const viewSelectionSlice = createSlice({
         if (currentBodyPart) {
           state.currentBodyPart = currentBodyPart;
           // console.log(`在view section中感知到 currentBodyPart 变化: ${JSON.stringify(currentBodyPart, null, 2)}`);
+        } else {
+          // 身体部位被清空,清空相关数据
+          state.currentBodyPart = null;
+          state.protocols = [];
+          state.availableViews = [];
+          console.log('身体部位已清空,协议和体位列表已重置');
         }
       })
       .addCase(setSelected, (state, action) => {