12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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 | null>(
- 'patientType/setCurrentPatientType'
- );
- export const patientTypeChanged = createAction<PatientType | null>(
- '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<PatientType | null>
- ) => {
- 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;
|