Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: notification read status being toggled when click on link #1769

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/app/components/notifications/notification-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { snoozeOptions } from "constants/notification";
type NotificationCardProps = {
notification: IUserNotification;
markNotificationReadStatus: (notificationId: string) => Promise<void>;
markNotificationReadStatusToggle: (notificationId: string) => Promise<void>;
markNotificationArchivedStatus: (notificationId: string) => Promise<void>;
setSelectedNotificationForSnooze: (notificationId: string) => void;
markSnoozeNotification: (notificationId: string, dateTime?: Date | undefined) => Promise<void>;
Expand All @@ -36,6 +37,7 @@ export const NotificationCard: React.FC<NotificationCardProps> = (props) => {
const {
notification,
markNotificationReadStatus,
markNotificationReadStatusToggle,
markNotificationArchivedStatus,
setSelectedNotificationForSnooze,
markSnoozeNotification,
Expand Down Expand Up @@ -159,7 +161,7 @@ export const NotificationCard: React.FC<NotificationCardProps> = (props) => {
name: notification.read_at ? "Mark as unread" : "Mark as read",
icon: "chat_bubble",
onClick: () => {
markNotificationReadStatus(notification.id).then(() => {
markNotificationReadStatusToggle(notification.id).then(() => {
setToastAlert({
title: notification.read_at
? "Notification marked as unread"
Expand Down
4 changes: 3 additions & 1 deletion apps/app/components/notifications/notification-popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const NotificationPopover = () => {
notificationMutate,
markNotificationArchivedStatus,
markNotificationReadStatus,
markNotificationAsRead,
markSnoozeNotification,
notificationCount,
totalNotificationCount,
Expand Down Expand Up @@ -128,7 +129,8 @@ export const NotificationPopover = () => {
key={notification.id}
notification={notification}
markNotificationArchivedStatus={markNotificationArchivedStatus}
markNotificationReadStatus={markNotificationReadStatus}
markNotificationReadStatus={markNotificationAsRead}
markNotificationReadStatusToggle={markNotificationReadStatus}
setSelectedNotificationForSnooze={setSelectedNotificationForSnooze}
markSnoozeNotification={markSnoozeNotification}
/>
Expand Down
21 changes: 21 additions & 0 deletions apps/app/hooks/use-user-notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ const useUserNotification = () => {
}
};

const markNotificationAsRead = async (notificationId: string) => {
if (!workspaceSlug) return;

const isRead =
notifications?.find((notification) => notification.id === notificationId)?.read_at !== null;

if (isRead) return;

mutateNotification(notificationId, { read_at: new Date() });
handleReadMutation("read");

await userNotificationServices
.markUserNotificationAsRead(workspaceSlug.toString(), notificationId)
.catch(() => {
throw new Error("Something went wrong");
});

mutateNotificationCount();
};

const markNotificationArchivedStatus = async (notificationId: string) => {
if (!workspaceSlug) return;
const isArchived =
Expand Down Expand Up @@ -283,6 +303,7 @@ const useUserNotification = () => {
hasMore,
isRefreshing,
setFetchNotifications,
markNotificationAsRead,
};
};

Expand Down