import { createSlice, createAsyncThunk, createAction, PayloadAction, } from '@reduxjs/toolkit'; import { fetchPatientTypes, PatientType } from '../API/patientType'; import { AxiosError } from 'axios'; interface PatientTypeState { items: PatientType[]; loading: boolean; error: string | null; current: PatientType | null; } const initialState: PatientTypeState = { items: [], loading: false, error: null, current: null, }; export const setCurrentPatientType = createAction( 'patientType/setCurrentPatientType' ); export const patientTypeChanged = createAction( 'patientType/patientTypeChanged' ); export const getPatientTypes = createAsyncThunk( 'patientType/getPatientTypes', async (_, { rejectWithValue }) => { try { console.log('Fetching patient types with params:'); const data = await fetchPatientTypes(); return data; } catch (err: unknown) { let errorMessage = 'Failed to fetch patient types'; if ( err && typeof err === 'object' && 'message' in err && typeof (err as AxiosError).message === 'string' ) { errorMessage = (err as AxiosError).message; } return rejectWithValue(errorMessage); } } ); const patientTypeSlice = createSlice({ name: 'patientType', initialState, reducers: { setCurrentPatientType: ( state, action: PayloadAction ) => { console.log('当前 patient type 变化:', action.payload); state.current = action.payload; if (action.payload) { // const patientTypeId = action.payload.patient_type_id; // setBodyPartsByPatientType(patientTypeId); } // Dispatch the action using useDispatch in a component }, }, extraReducers: (builder) => { builder .addCase(getPatientTypes.pending, (state) => { state.loading = true; state.error = null; }) .addCase(getPatientTypes.fulfilled, (state, action) => { state.loading = false; state.items = action.payload; }) .addCase(getPatientTypes.rejected, (state, action) => { state.loading = false; state.error = action.payload as string; }); }, }); export default patientTypeSlice.reducer;