Spaces:
Sleeping
Sleeping
File size: 1,753 Bytes
d97b8f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import { useQuery } from '@tanstack/react-query';
import { useAuth } from '@/lib/auth-context';
import { assetRequestApi } from '@/services/assetRequestApi';
/**
* Hook to get unread asset request notifications count
* For employees: count of requests with status changes (approved, rejected, assigned)
* For admins: count of pending requests
*/
export const useAssetRequestNotifications = () => {
const { userData } = useAuth();
const isAdmin = userData?.roleId === 1 || userData?.roleName?.toLowerCase() === 'admin';
const employeeId = userData?.employeeId || 0;
// For admins: get count of pending requests
const { data: pendingRequests = [] } = useQuery({
queryKey: ['assetRequests', 'pending'],
queryFn: () => assetRequestApi.getPending(),
enabled: isAdmin,
staleTime: 30000,
refetchInterval: 60000, // Refresh every minute
});
// For employees: get their requests and count status changes
// In a real implementation, this would track which notifications have been "read"
// For now, we'll just count non-pending requests as potential notifications
const { data: employeeRequests = [] } = useQuery({
queryKey: ['assetRequests', 'employee', employeeId],
queryFn: () => assetRequestApi.getByEmployeeId(employeeId),
enabled: !isAdmin && employeeId > 0,
staleTime: 30000,
refetchInterval: 60000,
});
// Calculate notification count
const notificationCount = isAdmin
? pendingRequests.length
: employeeRequests.filter(
(req) => req.status === 'approved' || req.status === 'rejected' || req.status === 'assigned'
).length;
return {
notificationCount,
hasNotifications: notificationCount > 0,
};
};
|