Skip to content

Commit 6645913

Browse files
committed
Implemented the notification hooks
1 parent 16300cb commit 6645913

File tree

4 files changed

+11
-32
lines changed

4 files changed

+11
-32
lines changed

frontend/components/notifications/NotificationBell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useMarkAllRead } from "@/lib/react-query/hooks/notifications/useMarkAll
1010
export default function NotificationBell() {
1111
const [isOpen, setIsOpen] = useState(false);
1212
const dropdownRef = useRef<HTMLDivElement>(null);
13-
const { data, isLoading, isError } = useGetNotifications(10);
13+
const { data, isLoading, isError } = useGetNotifications(1, 10);
1414
const markAsRead = useMarkNotificationRead();
1515
const markAllAsRead = useMarkAllRead();
1616

frontend/lib/react-query/hooks/notifications/useGetNotifications.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ interface NotificationsResponse {
2828
unreadCount: number;
2929
}
3030

31-
export const useGetNotifications = (limit = 10) => {
31+
export const useGetNotifications = (page = 1, limit = 10) => {
3232
return useQuery({
33-
queryKey: queryKeys.notifications.list({ limit }),
33+
queryKey: queryKeys.notifications.list({ page, limit }),
3434
queryFn: () =>
3535
apiClient.get<NotificationsResponse>(
36-
`/notifications?limit=${limit}`
36+
`/notifications?page=${page}&limit=${limit}`
3737
),
38+
staleTime: 30000, // 30 seconds
3839
});
3940
};

frontend/lib/react-query/hooks/notifications/useMarkAllRead.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,11 @@ export const useMarkAllRead = () => {
99
const queryClient = useQueryClient();
1010

1111
return useMutation({
12-
mutationFn: async () => {
13-
// Fetch all unread notifications
14-
const response = await apiClient.get<{
15-
message: string;
16-
data: Array<{ id: string; isRead: boolean }>;
17-
meta: { total: number; page: number; limit: number; totalPages: number };
18-
unreadCount: number;
19-
}>("/notifications?limit=100");
20-
21-
// Mark each unread notification as read
22-
const unreadNotifications = response.data.filter(
23-
(notification) => !notification.isRead
24-
);
25-
26-
await Promise.all(
27-
unreadNotifications.map((notification) =>
28-
apiClient.patch(`/notifications/${notification.id}/read`)
29-
)
30-
);
31-
32-
return { markedCount: unreadNotifications.length };
33-
},
34-
onSuccess: (data) => {
12+
mutationFn: () =>
13+
apiClient.patch<{ message: string }>("/notifications/read-all"),
14+
onSuccess: () => {
3515
queryClient.invalidateQueries({ queryKey: queryKeys.notifications.all });
36-
if (data.markedCount > 0) {
37-
toast.success(`Marked ${data.markedCount} notification(s) as read`);
38-
}
16+
toast.success("All notifications marked as read");
3917
},
4018
onError: (error: Error) => {
4119
toast.error(error.message || "Failed to mark all notifications as read");

frontend/lib/react-query/keys/queryKeys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ export const queryKeys = {
3131

3232
/**
3333
* Paginated list key.
34-
* @example queryKeys.notifications.list({ limit: 10 })
34+
* @example queryKeys.notifications.list({ page: 1, limit: 10 })
3535
*/
36-
list: (params: { limit?: number }) =>
36+
list: (params: { page?: number; limit?: number }) =>
3737
["notifications", "list", params] as const,
3838
},
3939
};

0 commit comments

Comments
 (0)