Skip to content

Commit 17d9c59

Browse files
authored
mark as read on click (#867)
1 parent a83ee6a commit 17d9c59

File tree

4 files changed

+48
-30
lines changed

4 files changed

+48
-30
lines changed

apps/web/actions/notifications/mark-all-as-read.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use server";
2+
3+
import { getCurrentUser } from "@cap/database/auth/session";
4+
import { notifications } from "@cap/database/schema";
5+
import { db } from "@cap/database";
6+
import { eq } from "drizzle-orm";
7+
import { revalidatePath } from "next/cache";
8+
9+
export const markAsRead = async (notificationId?: string) => {
10+
const currentUser = await getCurrentUser();
11+
if (!currentUser) {
12+
throw new Error("User not found");
13+
}
14+
15+
try {
16+
const now = new Date();
17+
if (notificationId) {
18+
await db()
19+
.update(notifications)
20+
.set({ readAt: now })
21+
.where(eq(notifications.id, notificationId));
22+
} else {
23+
await db()
24+
.update(notifications)
25+
.set({ readAt: now })
26+
.where(eq(notifications.recipientId, currentUser.id));
27+
}
28+
} catch (error) {
29+
console.log(error);
30+
throw new Error("Error marking notification(s) as read");
31+
}
32+
33+
revalidatePath("/dashboard");
34+
};

apps/web/app/(org)/dashboard/_components/Notifications/NotificationHeader.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { faCheckDouble } from "@fortawesome/free-solid-svg-icons";
22
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
33
import { toast } from "sonner";
44
import clsx from "clsx";
5-
import { markAllAsRead } from "@/actions/notifications/mark-all-as-read";
5+
import { markAsRead } from "@/actions/notifications/mark-as-read";
66
import { useQueryClient } from "@tanstack/react-query";
77
import { useState } from "react";
88

@@ -11,10 +11,10 @@ export const NotificationHeader = () => {
1111
const [loading, setLoading] = useState(false);
1212
const queryClient = useQueryClient();
1313

14-
const markAllAsReadHandler = async () => {
14+
const markAsReadHandler = async () => {
1515
try {
1616
setLoading(true);
17-
await markAllAsRead();
17+
await markAsRead();
1818
toast.success("Notifications marked as read");
1919
queryClient.invalidateQueries({
2020
queryKey: ["notifications"],
@@ -31,7 +31,7 @@ export const NotificationHeader = () => {
3131
<div className="flex justify-between items-center px-6 py-3 rounded-t-xl border bg-gray-3 border-gray-4">
3232
<p className="text-md text-gray-12">Notifications</p>
3333
<div
34-
onClick={markAllAsReadHandler}
34+
onClick={markAsReadHandler}
3535
className={clsx("flex gap-1 items-center transition-opacity duration-200 cursor-pointer hover:opacity-70", loading ? "opacity-50 cursor-not-allowed" : "")}
3636
>
3737
<FontAwesomeIcon

apps/web/app/(org)/dashboard/_components/Notifications/NotificationItem.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import clsx from "clsx";
44
import Link from "next/link";
55
import moment from "moment";
66
import { NotificationType } from "@/lib/Notification";
7+
import { markAsRead } from "@/actions/notifications/mark-as-read";
78
import { Notification as APINotification } from "@cap/web-api-contract";
89

910
type NotificationItemProps = {
@@ -25,9 +26,18 @@ export const NotificationItem = ({
2526
}: NotificationItemProps) => {
2627
const link = getLink(notification);
2728

29+
const markAsReadHandler = async () => {
30+
try {
31+
await markAsRead(notification.id);
32+
} catch (error) {
33+
console.error("Error marking notification as read:", error);
34+
}
35+
};
36+
2837
return (
2938
<Link
3039
href={link}
40+
onClick={markAsReadHandler}
3141
className={clsx(
3242
"flex gap-3 p-4 transition-colors cursor-pointer min-h-fit border-gray-3 hover:bg-gray-2",
3343
className

0 commit comments

Comments
 (0)