251208:0010 Backend & Frontend Debug
This commit is contained in:
31
frontend/hooks/use-notification.ts
Normal file
31
frontend/hooks/use-notification.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { notificationService } from '@/lib/services/notification.service';
|
||||
import { toast } from 'sonner';
|
||||
|
||||
export const notificationKeys = {
|
||||
all: ['notifications'] as const,
|
||||
unread: () => [...notificationKeys.all, 'unread'] as const,
|
||||
};
|
||||
|
||||
export function useNotifications() {
|
||||
return useQuery({
|
||||
queryKey: notificationKeys.unread(),
|
||||
queryFn: notificationService.getUnread,
|
||||
refetchInterval: 60 * 1000, // Poll every 1 minute
|
||||
staleTime: 30 * 1000,
|
||||
});
|
||||
}
|
||||
|
||||
export function useMarkNotificationRead() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: notificationService.markAsRead,
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: notificationKeys.unread() });
|
||||
},
|
||||
onError: () => {
|
||||
toast.error("Failed to mark notification as read");
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user