Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

feat: add mark all as read

bautistaaa5c40ccf18f98
diff --git a/apps/web/src/app/notifications/notification-page.tsx b/apps/web/src/app/notifications/notification-page.tsx
index 92fb44a3..63743ff2 100644
--- a/apps/web/src/app/notifications/notification-page.tsx
+++ b/apps/web/src/app/notifications/notification-page.tsx
@@ -5,13 +5,17 @@ import { Bell } from '@repo/ui/icons';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useRef } from 'react';
import { NOTIFICATION_QUERY_KEY } from '~/components/Navigation/notification-link';
-import { markNotificationsAsRead } from './notification.actions';
+import { markAllAsRead, markNotificationsAsRead } from './notification.actions';
import { NotificationsAll } from './notifications-all';
import { NotificationsMentions } from './notifications-mentions';
+import { Button } from '@repo/ui/components/button';
+import { useToast } from '@repo/ui/components/use-toast';
+import { ToastAction } from '@repo/ui/components/toast';
export default function NotificationPage() {
const seenNotifications = useRef(new Set<number>());
const timeoutRef = useRef<number>();
+ const { toast } = useToast();
const client = useQueryClient();
const mutation = useMutation({
mutationFn: markNotificationsAsRead,
@@ -37,13 +41,30 @@ export default function NotificationPage() {
}, 500);
};
+ const onMarkAllAsReadClick = async () => {
+ await markAllAsRead();
+ // partial matching is not working for some reason
+ await client.invalidateQueries({ queryKey: ['notifications-all'] });
+ await client.invalidateQueries({ queryKey: ['notifications-mentions'] });
+ await client.refetchQueries({ queryKey: ['notifications-all'] });
+ await client.refetchQueries({ queryKey: ['notifications-mentions'] });
+ toast({
+ variant: 'success',
+ title: 'All notifications marked as read.',
+ action: <ToastAction altText="Dismiss">Dismiss</ToastAction>,
+ });
+ };
+
return (
<div className="bg-background mx-auto min-w-[300px] max-w-3xl rounded-lg ">
- <div className="p-4">
+ <div className="flex items-center justify-between py-4">
<h2 className="text-foreground flex items-center text-xl font-bold">
<Bell className="mr-2 h-5 w-5" />
Notifications
</h2>
+ <Button size="sm" variant="outline" onClick={onMarkAllAsReadClick}>
+ Mark all as read
+ </Button>
</div>
<Tabs defaultValue="all" className="w-full">
<TabsList className="bg-muted grid w-full grid-cols-2">
diff --git a/apps/web/src/app/notifications/notification.actions.ts b/apps/web/src/app/notifications/notification.actions.ts
index 12f9403a..1335aec6 100644
--- a/apps/web/src/app/notifications/notification.actions.ts
+++ b/apps/web/src/app/notifications/notification.actions.ts
@@ -71,9 +71,6 @@ export async function getNotifications({
type: true,
},
orderBy: [
- {
- isRead: 'asc',
- },
{
createdAt: 'desc',
},
@@ -102,9 +99,6 @@ export async function getNotifications({
id: true,
},
orderBy: [
- {
- isRead: 'asc',
- },
{
createdAt: 'desc',
},
@@ -119,3 +113,19 @@ export async function getNotifications({
cursor: nextPage.length > 0 ? notifications.at(-1)?.id : undefined,
};
}
+
+export async function markAllAsRead() {
+ const session = await auth();
+ if (!session) {
+ throw new Error('not authenticated');
+ }
+
+ await prisma.notification.updateMany({
+ where: {
+ toUserId: session.user.id ?? '',
+ },
+ data: {
+ isRead: true,
+ },
+ });
+}