NeoScan_Radiologist/app/modules/Auth/redux/hospitalSlice.ts

169 lines
4.4 KiB
TypeScript

/*
* File: hospitalSlice.ts
* Description: Redux slice for Hospital state management (non-persisted)
* Design & Developed by Tech4Biz Solutions
* Copyright (c) Spurrin Innovations. All rights reserved.
*/
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
import { authAPI } from '../services/authAPI';
import { showError } from '../../../shared/utils/toast';
// ============================================================================
// INTERFACES
// ============================================================================
/**
* Hospital Interface
*
* Purpose: Defines the structure for hospital data
*/
interface Hospital {
hospital_id: string | null;
hospital_name: string | null;
}
/**
* Hospital State Interface
*
* Purpose: Defines the structure for hospital state
*/
interface HospitalState {
hospitals: Hospital[] | null;
loading: boolean;
error: string | null;
}
// ============================================================================
// ASYNC THUNKS
// ============================================================================
/**
* Fetch Hospitals Async Thunk
*
* Purpose: Fetch hospital list from API
*
* Features:
* - API integration with error handling
* - Loading state management
* - Toast notifications for errors
* - Automatic state updates
*/
export const fetchHospitals = createAsyncThunk(
'hospital/fetchHospitals',
async (_, { rejectWithValue }) => {
try {
const response: any = await authAPI.gethospitals();
if (response.ok && response.data && response.data.data) {
return response.data.data;
} else {
showError('Error while fetching hospital list');
return rejectWithValue('Failed to fetch hospitals');
}
} catch (error: any) {
console.log('Hospital fetch error:', error);
showError('Error while fetching hospital list');
return rejectWithValue(error.message || 'Network error');
}
}
);
// ============================================================================
// INITIAL STATE
// ============================================================================
const initialState: HospitalState = {
hospitals: [],
loading: false,
error: null,
};
// ============================================================================
// HOSPITAL SLICE
// ============================================================================
/**
* Hospital Slice
*
* Purpose: Manages hospital-related state
*
* Features:
* - Hospital list management
* - Loading states for API calls
* - Error handling and display
* - Non-persisted state (not stored in AsyncStorage)
*/
const hospitalSlice = createSlice({
name: 'hospital',
initialState,
reducers: {
/**
* Set Hospitals Action
*
* Purpose: Manually set hospital list
*/
setHospitals(state, action: PayloadAction<Hospital[]>) {
state.hospitals = action.payload;
state.loading = false;
state.error = null;
},
/**
* Clear Hospitals Action
*
* Purpose: Clear hospital list and reset state
*/
clearHospitals(state) {
state.hospitals = [];
state.loading = false;
state.error = null;
},
/**
* Clear Error Action
*
* Purpose: Clear error state
*/
clearError(state) {
state.error = null;
},
},
extraReducers: (builder) => {
builder
// Fetch Hospitals - Pending
.addCase(fetchHospitals.pending, (state) => {
state.loading = true;
state.error = null;
})
// Fetch Hospitals - Fulfilled
.addCase(fetchHospitals.fulfilled, (state, action) => {
state.loading = false;
state.hospitals = action.payload;
state.error = null;
})
// Fetch Hospitals - Rejected
.addCase(fetchHospitals.rejected, (state, action) => {
state.loading = false;
state.error = action.payload as string || 'Failed to fetch hospitals';
});
},
});
// ============================================================================
// EXPORTS
// ============================================================================
export const {
setHospitals,
clearHospitals,
clearError,
} = hospitalSlice.actions;
export default hospitalSlice.reducer;
/*
* End of File: hospitalSlice.ts
* Design & Developed by Tech4Biz Solutions
* Copyright (c) Spurrin Innovations. All rights reserved.
*/