Skip to content

Commit

Permalink
Merge pull request #468 from bento-platform/fix/notification-mark-as-…
Browse files Browse the repository at this point in the history
…read

fix: proper notification mark-as-read behaviour
  • Loading branch information
davidlougheed authored Nov 15, 2024
2 parents cd8ff0e + 25e09c5 commit e5019cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/modules/notifications/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const MARK_NOTIFICATION_AS_READ = createNetworkActionTypes("MARK_NOTIFICA
export const markNotificationAsRead = networkAction((notificationID) => (_dispatch, getState) => ({
types: MARK_NOTIFICATION_AS_READ,
params: { notificationID },
check: (state) =>
state.services.notificationService && !state.notifications.itemsByID[notificationID]?.isMarkingAsRead,
url: `${getState().services.notificationService.url}/notifications/${notificationID}/read`,
req: { method: "PUT" },
err: "Error marking notification as read",
Expand Down
26 changes: 14 additions & 12 deletions src/modules/notifications/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
} from "./actions";

const unreadNotifications = (items) => items.filter((n) => !n.read);
const setMarkingAsRead = (i) => ({ ...i, isMarkingAsRead: true });
const setRead = (i) => ({ ...i, read: true });
const setNotMarkingAsRead = (i) => ({ ...i, isMarkingAsRead: false });

export const notifications = (
state = {
Expand Down Expand Up @@ -57,33 +60,32 @@ export const notifications = (
return { ...state, isFetching: false };

case MARK_NOTIFICATION_AS_READ.REQUEST: {
const rp = (i) => ({ ...i, isMarkingAsRead: true });
const items = replaceNotificationInArray(setMarkingAsRead);
return {
...state,
isMarkingAsRead: true,
items: replaceNotificationInArray(rp),
unreadItems: replaceNotificationInArray(rp),
itemsByID: replaceNotificationInObject(rp),
items,
unreadItems: unreadNotifications(items),
itemsByID: replaceNotificationInObject(setMarkingAsRead),
};
}
case MARK_NOTIFICATION_AS_READ.RECEIVE: {
const rp = (i) => ({ ...i, read: true });
const items = replaceNotificationInArray(rp);
const items = replaceNotificationInArray(setRead);
return {
...state,
items: items,
items,
unreadItems: unreadNotifications(items),
itemsByID: replaceNotificationInObject(rp),
itemsByID: replaceNotificationInObject(setRead),
};
}
case MARK_NOTIFICATION_AS_READ.FINISH: {
const rp = (i) => ({ ...i, isMarkingAsRead: false });
const items = replaceNotificationInArray(setNotMarkingAsRead);
return {
...state,
isMarkingAsRead: false,
items: replaceNotificationInArray(rp),
unreadItems: replaceNotificationInArray(rp), // should do nothing unless there's an error
itemsByID: replaceNotificationInObject(rp),
items,
unreadItems: unreadNotifications(items), // should do nothing unless there's an error
itemsByID: replaceNotificationInObject(setNotMarkingAsRead),
};
}

Expand Down

0 comments on commit e5019cd

Please sign in to comment.