|
@@ -0,0 +1,66 @@
|
|
|
+import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
|
|
|
+import {
|
|
|
+ fetchPatientTypes,
|
|
|
+ PatientType,
|
|
|
+ PatientTypeParams,
|
|
|
+} from '../API/patientType';
|
|
|
+import { AxiosError } from 'axios';
|
|
|
+
|
|
|
+interface PatientTypeState {
|
|
|
+ items: PatientType[];
|
|
|
+ loading: boolean;
|
|
|
+ error: string | null;
|
|
|
+}
|
|
|
+
|
|
|
+const initialState: PatientTypeState = {
|
|
|
+ items: [],
|
|
|
+ loading: false,
|
|
|
+ error: null,
|
|
|
+};
|
|
|
+
|
|
|
+export const getPatientTypes = createAsyncThunk(
|
|
|
+ 'patientType/getPatientTypes',
|
|
|
+ async (
|
|
|
+ { params, token }: { params: PatientTypeParams; token: string },
|
|
|
+ { rejectWithValue }
|
|
|
+ ) => {
|
|
|
+ try {
|
|
|
+ const data = await fetchPatientTypes(params, token);
|
|
|
+ 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: {},
|
|
|
+ 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;
|