280 lines
7.3 KiB
TypeScript
280 lines
7.3 KiB
TypeScript
/*
|
|
* File: dashboardSlice.ts
|
|
* Description: Dashboard state management slice
|
|
* Design & Developed by Tech4Biz Solutions
|
|
* Copyright (c) Spurrin Innovations. All rights reserved.
|
|
*/
|
|
|
|
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
|
|
import { ERDashboard, DashboardState } from '../../../shared/types';
|
|
|
|
// ============================================================================
|
|
// ASYNC THUNKS
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Fetch Dashboard Data Async Thunk
|
|
*
|
|
* Purpose: Fetch dashboard data from API
|
|
*
|
|
* @returns Promise with dashboard data or error
|
|
*/
|
|
export const fetchDashboardData = createAsyncThunk(
|
|
'dashboard/fetchDashboardData',
|
|
async (_, { rejectWithValue }) => {
|
|
try {
|
|
// TODO: Replace with actual API call
|
|
// Simulate API call with timeout
|
|
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
|
|
// Mock dashboard data
|
|
const mockDashboard: ERDashboard = {
|
|
totalPatients: 24,
|
|
criticalPatients: 3,
|
|
pendingScans: 8,
|
|
recentReports: 12,
|
|
bedOccupancy: 85,
|
|
departmentStats: {
|
|
emergency: 8,
|
|
trauma: 4,
|
|
cardiac: 3,
|
|
neurology: 2,
|
|
pediatrics: 5,
|
|
icu: 2,
|
|
},
|
|
shiftInfo: {
|
|
currentShift: 'DAY',
|
|
startTime: new Date(),
|
|
endTime: new Date(),
|
|
attendingPhysician: 'Dr. Smith',
|
|
residents: ['Dr. Johnson', 'Dr. Williams'],
|
|
nurses: ['Nurse Brown', 'Nurse Davis'],
|
|
},
|
|
lastUpdated: new Date(),
|
|
};
|
|
|
|
return mockDashboard;
|
|
} catch (error) {
|
|
return rejectWithValue('Failed to fetch dashboard data.');
|
|
}
|
|
}
|
|
);
|
|
|
|
/**
|
|
* Refresh Dashboard Data Async Thunk
|
|
*
|
|
* Purpose: Refresh dashboard data
|
|
*
|
|
* @returns Promise with updated dashboard data or error
|
|
*/
|
|
export const refreshDashboardData = createAsyncThunk(
|
|
'dashboard/refreshDashboardData',
|
|
async (_, { rejectWithValue }) => {
|
|
try {
|
|
// TODO: Replace with actual API call
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
// Mock refreshed dashboard data
|
|
const mockDashboard: ERDashboard = {
|
|
totalPatients: 26,
|
|
criticalPatients: 2,
|
|
pendingScans: 6,
|
|
recentReports: 15,
|
|
bedOccupancy: 87,
|
|
departmentStats: {
|
|
emergency: 9,
|
|
trauma: 3,
|
|
cardiac: 4,
|
|
neurology: 2,
|
|
pediatrics: 6,
|
|
icu: 2,
|
|
},
|
|
shiftInfo: {
|
|
currentShift: 'DAY',
|
|
startTime: new Date(),
|
|
endTime: new Date(),
|
|
attendingPhysician: 'Dr. Smith',
|
|
residents: ['Dr. Johnson', 'Dr. Williams'],
|
|
nurses: ['Nurse Brown', 'Nurse Davis'],
|
|
},
|
|
lastUpdated: new Date(),
|
|
};
|
|
|
|
return mockDashboard;
|
|
} catch (error) {
|
|
return rejectWithValue('Failed to refresh dashboard data.');
|
|
}
|
|
}
|
|
);
|
|
|
|
// ============================================================================
|
|
// INITIAL STATE
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Initial Dashboard State
|
|
*
|
|
* Purpose: Define the initial state for dashboard
|
|
*
|
|
* Features:
|
|
* - Dashboard data and statistics
|
|
* - Loading states for async operations
|
|
* - Error handling and messages
|
|
* - Real-time updates tracking
|
|
*/
|
|
const initialState: DashboardState = {
|
|
// Dashboard data
|
|
dashboard: null,
|
|
|
|
// Loading states
|
|
isLoading: false,
|
|
isRefreshing: false,
|
|
|
|
// Error handling
|
|
error: null,
|
|
|
|
// Real-time updates
|
|
lastUpdated: null,
|
|
isConnected: true,
|
|
|
|
// Filters and preferences
|
|
selectedFilter: 'all',
|
|
sortBy: 'priority',
|
|
sortOrder: 'desc',
|
|
};
|
|
|
|
// ============================================================================
|
|
// DASHBOARD SLICE
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Dashboard Slice
|
|
*
|
|
* Purpose: Redux slice for dashboard state management
|
|
*
|
|
* Features:
|
|
* - Dashboard data management
|
|
* - Real-time updates
|
|
* - Filtering and sorting
|
|
* - Error handling
|
|
* - Loading states
|
|
*/
|
|
const dashboardSlice = createSlice({
|
|
name: 'dashboard',
|
|
initialState,
|
|
reducers: {
|
|
/**
|
|
* Clear Error Action
|
|
*
|
|
* Purpose: Clear dashboard errors
|
|
*/
|
|
clearError: (state) => {
|
|
state.error = null;
|
|
},
|
|
|
|
/**
|
|
* Set Filter Action
|
|
*
|
|
* Purpose: Set dashboard filter
|
|
*/
|
|
setFilter: (state, action: PayloadAction<'all' | 'critical' | 'active' | 'pending'>) => {
|
|
state.selectedFilter = action.payload;
|
|
},
|
|
|
|
/**
|
|
* Set Sort Action
|
|
*
|
|
* Purpose: Set dashboard sort options
|
|
*/
|
|
setSort: (state, action: PayloadAction<{ by: string; order: 'asc' | 'desc' }>) => {
|
|
state.sortBy = action.payload.by;
|
|
state.sortOrder = action.payload.order;
|
|
},
|
|
|
|
/**
|
|
* Update Connection Status Action
|
|
*
|
|
* Purpose: Update real-time connection status
|
|
*/
|
|
updateConnectionStatus: (state, action: PayloadAction<boolean>) => {
|
|
state.isConnected = action.payload;
|
|
},
|
|
|
|
/**
|
|
* Update Last Updated Action
|
|
*
|
|
* Purpose: Update last updated timestamp
|
|
*/
|
|
updateLastUpdated: (state, action: PayloadAction<Date>) => {
|
|
state.lastUpdated = action.payload;
|
|
},
|
|
|
|
/**
|
|
* Update Dashboard Data Action
|
|
*
|
|
* Purpose: Update dashboard data manually
|
|
*/
|
|
updateDashboardData: (state, action: PayloadAction<Partial<ERDashboard>>) => {
|
|
if (state.dashboard) {
|
|
state.dashboard = { ...state.dashboard, ...action.payload };
|
|
state.lastUpdated = new Date();
|
|
}
|
|
},
|
|
},
|
|
extraReducers: (builder) => {
|
|
// Fetch Dashboard Data
|
|
builder
|
|
.addCase(fetchDashboardData.pending, (state) => {
|
|
state.isLoading = true;
|
|
state.error = null;
|
|
})
|
|
.addCase(fetchDashboardData.fulfilled, (state, action) => {
|
|
state.isLoading = false;
|
|
state.dashboard = action.payload;
|
|
state.lastUpdated = action.payload.lastUpdated;
|
|
state.error = null;
|
|
})
|
|
.addCase(fetchDashboardData.rejected, (state, action) => {
|
|
state.isLoading = false;
|
|
state.error = action.payload as string;
|
|
});
|
|
|
|
// Refresh Dashboard Data
|
|
builder
|
|
.addCase(refreshDashboardData.pending, (state) => {
|
|
state.isRefreshing = true;
|
|
state.error = null;
|
|
})
|
|
.addCase(refreshDashboardData.fulfilled, (state, action) => {
|
|
state.isRefreshing = false;
|
|
state.dashboard = action.payload;
|
|
state.lastUpdated = action.payload.lastUpdated;
|
|
state.error = null;
|
|
})
|
|
.addCase(refreshDashboardData.rejected, (state, action) => {
|
|
state.isRefreshing = false;
|
|
state.error = action.payload as string;
|
|
});
|
|
},
|
|
});
|
|
|
|
// ============================================================================
|
|
// EXPORTS
|
|
// ============================================================================
|
|
|
|
export const {
|
|
clearError,
|
|
setFilter,
|
|
setSort,
|
|
updateConnectionStatus,
|
|
updateLastUpdated,
|
|
updateDashboardData,
|
|
} = dashboardSlice.actions;
|
|
|
|
export default dashboardSlice.reducer;
|
|
|
|
/*
|
|
* End of File: dashboardSlice.ts
|
|
* Design & Developed by Tech4Biz Solutions
|
|
* Copyright (c) Spurrin Innovations. All rights reserved.
|
|
*/
|