Browse Source

添加身体部位的获取与存储相关逻辑

dengdx 2 months ago
parent
commit
3a7b4b0103
2 changed files with 102 additions and 0 deletions
  1. 40 0
      src/API/bodyPart.ts
  2. 62 0
      src/states/bodyPartSlice.ts

+ 40 - 0
src/API/bodyPart.ts

@@ -0,0 +1,40 @@
+import axios from 'axios';
+
+export interface BodyPartParams {
+  patient_type?: string;
+  modality?: string;
+  is_enabled?: string;
+}
+
+export interface BodyPart {
+  id: string;
+  body_part_id: string;
+  body_part_name: string;
+  body_part_local: string;
+  body_part_description: string;
+  patient_type: string;
+  category: string;
+  sort: number;
+  is_enabled: boolean;
+  product: string;
+  is_pre_install: boolean;
+}
+
+export async function fetchBodyParts(
+  params: BodyPartParams,
+  token: string,
+  language = 'en',
+  product = 'DROS',
+  source = 'Electron'
+): Promise<BodyPart[]> {
+  const response = await axios.get('/api/body-parts', {
+    params,
+    headers: {
+      Authorization: `Bearer ${token}`,
+      Language: language,
+      Product: product,
+      Source: source,
+    },
+  });
+  return response.data;
+}

+ 62 - 0
src/states/bodyPartSlice.ts

@@ -0,0 +1,62 @@
+import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
+import { fetchBodyParts, BodyPart, BodyPartParams } from '../API/bodyPart';
+import { AxiosError } from 'axios';
+
+interface BodyPartState {
+  items: BodyPart[];
+  loading: boolean;
+  error: string | null;
+}
+
+const initialState: BodyPartState = {
+  items: [],
+  loading: false,
+  error: null,
+};
+
+export const getBodyParts = createAsyncThunk(
+  'bodyPart/getBodyParts',
+  async (
+    { params, token }: { params: BodyPartParams; token: string },
+    { rejectWithValue }
+  ) => {
+    try {
+      const data = await fetchBodyParts(params, token);
+      return data;
+    } catch (err: unknown) {
+      let errorMessage = 'Failed to fetch body parts';
+      if (
+        err &&
+        typeof err === 'object' &&
+        'message' in err &&
+        typeof (err as AxiosError).message === 'string'
+      ) {
+        errorMessage = (err as AxiosError).message;
+      }
+      return rejectWithValue(errorMessage);
+    }
+  }
+);
+
+const bodyPartSlice = createSlice({
+  name: 'bodyPart',
+  initialState,
+  reducers: {},
+  extraReducers: (builder) => {
+    builder
+      .addCase(getBodyParts.pending, (state) => {
+        state.loading = true;
+        state.error = null;
+      })
+      .addCase(getBodyParts.fulfilled, (state, action) => {
+        state.loading = false;
+        state.items = action.payload;
+      })
+      .addCase(getBodyParts.rejected, (state, action) => {
+        state.loading = false;
+        state.error = action.payload as string;
+      });
+  },
+});
+
+export default bodyPartSlice.reducer;