bodyPartSlice.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
  2. import { fetchBodyParts, BodyPart, BodyPartParams } from '../API/bodyPart';
  3. import { AxiosError } from 'axios';
  4. import { setCurrentPatientType } from './patientTypeSlice';
  5. interface BodyPartState {
  6. items: BodyPart[];
  7. loading: boolean;
  8. error: string | null;
  9. byPatientType: BodyPart[];
  10. current: BodyPart | null;
  11. }
  12. const initialState: BodyPartState = {
  13. items: [],
  14. loading: false,
  15. error: null,
  16. byPatientType: [],
  17. current: null,
  18. };
  19. export const getBodyParts = createAsyncThunk(
  20. 'bodyPart/getBodyParts',
  21. async ({ params }: { params: BodyPartParams }, { rejectWithValue }) => {
  22. try {
  23. const data = await fetchBodyParts(params);
  24. return data;
  25. } catch (err: unknown) {
  26. let errorMessage = 'Failed to fetch body parts';
  27. console.log(errorMessage);
  28. if (
  29. err &&
  30. typeof err === 'object' &&
  31. 'message' in err &&
  32. typeof (err as AxiosError).message === 'string'
  33. ) {
  34. errorMessage = (err as AxiosError).message;
  35. }
  36. return rejectWithValue(errorMessage);
  37. }
  38. }
  39. );
  40. const bodyPartSlice = createSlice({
  41. name: 'bodyPart',
  42. initialState,
  43. reducers: {
  44. setCurrentBodyPart: (state, action: PayloadAction<BodyPart | null>) => {
  45. if (action.payload) {
  46. state.current =
  47. state.byPatientType.find(
  48. (item) => item.body_part_id === action.payload?.body_part_id
  49. ) || null;
  50. } else {
  51. state.current = null;
  52. }
  53. },
  54. // setBodyPartsByPatientType: (state, action: PayloadAction<{ patientTypeId: string }>) => {
  55. // // state.byPatientType[action.payload.patientTypeId] = action.payload.bodyParts;
  56. // },
  57. },
  58. extraReducers: (builder) => {
  59. builder
  60. .addCase(setCurrentPatientType, (state, action) => {
  61. console.log(
  62. '当前 patient type 变化-- bodyPart接收到信号:',
  63. action.payload
  64. );
  65. if (action.payload) {
  66. const patientTypeId = action.payload.patient_type_id;
  67. console.log(
  68. '基于 patient type过滤 bodyPart byPatientType:',
  69. patientTypeId
  70. );
  71. state.byPatientType = state.items.filter(
  72. (item) => item.patient_type === patientTypeId
  73. );
  74. console.log('过滤后的 bodyPart byPatientType:', state.byPatientType);
  75. }
  76. })
  77. .addCase(getBodyParts.pending, (state) => {
  78. state.loading = true;
  79. state.error = null;
  80. })
  81. .addCase(getBodyParts.fulfilled, (state, action) => {
  82. state.loading = false;
  83. state.items = action.payload;
  84. })
  85. .addCase(getBodyParts.rejected, (state, action) => {
  86. state.loading = false;
  87. state.error = action.payload as string;
  88. });
  89. },
  90. });
  91. export const { setCurrentBodyPart } = bodyPartSlice.actions;
  92. export default bodyPartSlice.reducer;