Website : rimsha.abasa.com
backdoor
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
mudeer-web
/
src
/
hooks
/
Filename :
useNotifications.ts
back
Copy
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import axios from 'axios'; const MUDEER_API_URL = process.env.NEXT_PUBLIC_MUDEER_URL; const getAuthHeaders = () => { const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null; return { headers: { Authorization: `Bearer ${token}` } }; }; export const useNotifications = (page = 1, filters = {}) => { const isAuthenticated = typeof window !== 'undefined' ? !!localStorage.getItem('token') : false; return useQuery({ queryKey: ['notifications', page, filters], queryFn: async () => { const params = new URLSearchParams({ page: page.toString(), limit: '20', ...filters }); const response = await axios.get( `${MUDEER_API_URL}/notification?${params.toString()}`, getAuthHeaders() ); return response.data; }, refetchInterval: 30000, enabled: isAuthenticated, // Only run when authenticated }); }; /** Fetches 10 latest notifications for the header popover. */ export const useNotificationsGlimpse = (enabled: boolean) => { const isAuthenticated = typeof window !== 'undefined' ? !!localStorage.getItem('token') : false; return useQuery({ queryKey: ['notifications', 'glimpse', 10], queryFn: async () => { const params = new URLSearchParams({ page: '1', limit: '10' }); const response = await axios.get( `${MUDEER_API_URL}/notification?${params.toString()}`, getAuthHeaders() ); return response.data; }, enabled: isAuthenticated && enabled, }); }; export const useUnreadCount = () => { const isAuthenticated = typeof window !== 'undefined' ? !!localStorage.getItem('token') : false; return useQuery({ queryKey: ['notifications-unread-count'], queryFn: async () => { const response = await axios.get( `${MUDEER_API_URL}/notification/unread-count`, getAuthHeaders() ); return response.data; }, refetchInterval: 10000, enabled: isAuthenticated, // Only run when authenticated }); }; export const useNotificationMutations = () => { const queryClient = useQueryClient(); const markAsRead = useMutation({ mutationFn: async (notificationId: string) => { return axios.patch( `${MUDEER_API_URL}/notification/${notificationId}/read`, {}, getAuthHeaders() ); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['notifications'] }); queryClient.invalidateQueries({ queryKey: ['notifications-unread-count'] }); } }); const markMultipleAsRead = useMutation({ mutationFn: async (notificationIds: string[]) => { return axios.patch( `${MUDEER_API_URL}/notification/read-multiple`, { notificationIds }, getAuthHeaders() ); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['notifications'] }); queryClient.invalidateQueries({ queryKey: ['notifications-unread-count'] }); } }); const markAllAsRead = useMutation({ mutationFn: async () => { return axios.patch( `${MUDEER_API_URL}/notification/read-all`, {}, getAuthHeaders() ); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['notifications'] }); queryClient.invalidateQueries({ queryKey: ['notifications-unread-count'] }); } }); const deleteNotification = useMutation({ mutationFn: async (notificationId: string) => { return axios.delete( `${MUDEER_API_URL}/notification/${notificationId}`, getAuthHeaders() ); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['notifications'] }); queryClient.invalidateQueries({ queryKey: ['notifications-unread-count'] }); } }); return { markAsRead, markMultipleAsRead, markAllAsRead, deleteNotification }; };