dashboard api optimisation for role specific
This commit is contained in:
parent
ea1cc43cb6
commit
02182daa65
@ -196,26 +196,30 @@ export function Dashboard({ onNavigate, onNewRequest }: DashboardProps) {
|
|||||||
params.endDate = customEnd.toISOString();
|
params.endDate = customEnd.toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch common data for all users
|
||||||
|
const commonPromises = [
|
||||||
|
dashboardService.getKPIs(selectedDateRange, customStart, customEnd),
|
||||||
|
dashboardService.getRecentActivity(1, 10),
|
||||||
|
dashboardService.getCriticalRequests(1, 10),
|
||||||
|
dashboardService.getUpcomingDeadlines(1, 10)
|
||||||
|
];
|
||||||
|
|
||||||
|
// Fetch admin-only data if user is admin
|
||||||
|
const adminPromises = isAdmin ? [
|
||||||
|
dashboardService.getDepartmentStats(selectedDateRange, customStart, customEnd),
|
||||||
|
dashboardService.getPriorityDistribution(selectedDateRange, customStart, customEnd),
|
||||||
|
dashboardService.getAIRemarkUtilization(selectedDateRange, customStart, customEnd),
|
||||||
|
dashboardService.getApproverPerformance(selectedDateRange, 1, 10, customStart, customEnd)
|
||||||
|
] : [];
|
||||||
|
|
||||||
// Fetch all data in parallel
|
// Fetch all data in parallel
|
||||||
const [
|
const [
|
||||||
kpisData,
|
kpisData,
|
||||||
activityResult,
|
activityResult,
|
||||||
criticalResult,
|
criticalResult,
|
||||||
deptStats,
|
|
||||||
priorityDist,
|
|
||||||
deadlinesResult,
|
deadlinesResult,
|
||||||
aiUtilization,
|
...adminResults
|
||||||
approverResult
|
] = await Promise.all([...commonPromises, ...adminPromises]);
|
||||||
] = await Promise.all([
|
|
||||||
dashboardService.getKPIs(selectedDateRange, customStart, customEnd),
|
|
||||||
dashboardService.getRecentActivity(1, 10),
|
|
||||||
dashboardService.getCriticalRequests(1, 10),
|
|
||||||
dashboardService.getDepartmentStats(selectedDateRange, customStart, customEnd),
|
|
||||||
dashboardService.getPriorityDistribution(selectedDateRange, customStart, customEnd),
|
|
||||||
dashboardService.getUpcomingDeadlines(1, 10),
|
|
||||||
dashboardService.getAIRemarkUtilization(selectedDateRange, customStart, customEnd),
|
|
||||||
dashboardService.getApproverPerformance(selectedDateRange, 1, 10, customStart, customEnd)
|
|
||||||
]);
|
|
||||||
|
|
||||||
setKpis(kpisData);
|
setKpis(kpisData);
|
||||||
setRecentActivity(activityResult.activities);
|
setRecentActivity(activityResult.activities);
|
||||||
@ -228,27 +232,35 @@ export function Dashboard({ onNavigate, onNewRequest }: DashboardProps) {
|
|||||||
setCriticalTotalPages(criticalResult.pagination.totalPages);
|
setCriticalTotalPages(criticalResult.pagination.totalPages);
|
||||||
setCriticalTotalRecords(criticalResult.pagination.totalRecords);
|
setCriticalTotalRecords(criticalResult.pagination.totalRecords);
|
||||||
|
|
||||||
setDepartmentStats(deptStats);
|
|
||||||
setPriorityDistribution(priorityDist);
|
|
||||||
|
|
||||||
setUpcomingDeadlines(deadlinesResult.deadlines);
|
setUpcomingDeadlines(deadlinesResult.deadlines);
|
||||||
setDeadlinesPage(deadlinesResult.pagination.currentPage);
|
setDeadlinesPage(deadlinesResult.pagination.currentPage);
|
||||||
setDeadlinesTotalPages(deadlinesResult.pagination.totalPages);
|
setDeadlinesTotalPages(deadlinesResult.pagination.totalPages);
|
||||||
setDeadlinesTotalRecords(deadlinesResult.pagination.totalRecords);
|
setDeadlinesTotalRecords(deadlinesResult.pagination.totalRecords);
|
||||||
|
|
||||||
|
// Only set admin-specific data if user is admin
|
||||||
|
if (isAdmin && adminResults.length === 4) {
|
||||||
|
const [deptStats, priorityDist, aiUtilization, approverResult] = adminResults;
|
||||||
|
setDepartmentStats(deptStats);
|
||||||
|
setPriorityDistribution(priorityDist);
|
||||||
setAiRemarkUtilization(aiUtilization);
|
setAiRemarkUtilization(aiUtilization);
|
||||||
|
|
||||||
setApproverPerformance(approverResult.performance);
|
setApproverPerformance(approverResult.performance);
|
||||||
setApproverPage(approverResult.pagination.currentPage);
|
setApproverPage(approverResult.pagination.currentPage);
|
||||||
setApproverTotalPages(approverResult.pagination.totalPages);
|
setApproverTotalPages(approverResult.pagination.totalPages);
|
||||||
setApproverTotalRecords(approverResult.pagination.totalRecords);
|
setApproverTotalRecords(approverResult.pagination.totalRecords);
|
||||||
|
} else if (!isAdmin) {
|
||||||
|
// Reset admin-specific data for normal users
|
||||||
|
setDepartmentStats([]);
|
||||||
|
setPriorityDistribution([]);
|
||||||
|
setAiRemarkUtilization(null);
|
||||||
|
setApproverPerformance([]);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch dashboard data:', error);
|
console.error('Failed to fetch dashboard data:', error);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setRefreshing(false);
|
setRefreshing(false);
|
||||||
}
|
}
|
||||||
}, []);
|
}, [isAdmin]);
|
||||||
|
|
||||||
const handleRefresh = () => {
|
const handleRefresh = () => {
|
||||||
if (dateRange === 'custom' && customStartDate && customEndDate) {
|
if (dateRange === 'custom' && customStartDate && customEndDate) {
|
||||||
@ -455,12 +467,20 @@ export function Dashboard({ onNavigate, onNewRequest }: DashboardProps) {
|
|||||||
}, [fetchDashboardData, dateRange, customStartDate, customEndDate]);
|
}, [fetchDashboardData, dateRange, customStartDate, customEndDate]);
|
||||||
|
|
||||||
// Memoize quick actions to prevent recreation on each render
|
// Memoize quick actions to prevent recreation on each render
|
||||||
const quickActions = useMemo(() => [
|
const quickActions = useMemo(() => {
|
||||||
|
const actions = [
|
||||||
{ label: 'New Request', icon: FileText, action: () => onNewRequest?.(), color: 'bg-emerald-600 hover:bg-emerald-700' },
|
{ label: 'New Request', icon: FileText, action: () => onNewRequest?.(), color: 'bg-emerald-600 hover:bg-emerald-700' },
|
||||||
{ label: 'View Pending', icon: Clock, action: () => onNavigate?.('open-requests'), color: 'bg-blue-600 hover:bg-blue-700' },
|
{ label: 'View Pending', icon: Clock, action: () => onNavigate?.('open-requests'), color: 'bg-blue-600 hover:bg-blue-700' },
|
||||||
{ label: 'Reports', icon: Activity, action: () => onNavigate?.('detailed-reports'), color: 'bg-purple-600 hover:bg-purple-700' },
|
|
||||||
{ label: 'Settings', icon: Settings, action: () => onNavigate?.('settings'), color: 'bg-slate-600 hover:bg-slate-700' }
|
{ label: 'Settings', icon: Settings, action: () => onNavigate?.('settings'), color: 'bg-slate-600 hover:bg-slate-700' }
|
||||||
], [onNavigate, onNewRequest]);
|
];
|
||||||
|
|
||||||
|
// Only show Reports button for admin users
|
||||||
|
if (isAdmin) {
|
||||||
|
actions.splice(2, 0, { label: 'Reports', icon: Activity, action: () => onNavigate?.('detailed-reports'), color: 'bg-purple-600 hover:bg-purple-700' });
|
||||||
|
}
|
||||||
|
|
||||||
|
return actions;
|
||||||
|
}, [onNavigate, onNewRequest, isAdmin]);
|
||||||
|
|
||||||
// Helper function to build filter URL params
|
// Helper function to build filter URL params
|
||||||
const buildFilterUrl = useCallback((filters: {
|
const buildFilterUrl = useCallback((filters: {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user