Skip to content

Commit

Permalink
feat(notifications): Add extra filter to normaliser to return only un…
Browse files Browse the repository at this point in the history
…read notifications

Discourse doesn't provide a way to ask only for unread notifications, instead, it provides a way to
know if there is unread notifications and then pull the recent ones which lead to the need to having
a way to further filter those notifications to show only unread ones after get all
  • Loading branch information
duranmla committed Apr 6, 2019
1 parent 1da6b6f commit 38ac3b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/notifications/__tests__/normaliser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ describe("normaliser", () => {
expect(result.alerts).toEqual([notifications[1], notifications[3]]);
expect(result.messages).toEqual([notifications[0], notifications[2]]);
});

it("returns only unread notifications", () => {
const notificationsUpdated = [...notifications];
notificationsUpdated[3].read = true;
notificationsUpdated[2].read = true;
const result = normaliseUserNotifications(notificationsUpdated);

expect(result.alerts).toEqual([notifications[1]]);
expect(result.messages).toEqual([notifications[0]]);
});
});
11 changes: 7 additions & 4 deletions src/notifications/normaliser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// @flow

import filter from "lodash/filter";
import includes from "lodash/includes";
import { notificationTypes } from "./helpers";

const getMessages = notifications => {
const messagesTypes = [notificationTypes[5], notificationTypes[6]];

return notifications.filter(
n => messagesTypes.indexOf(notificationTypes[n.notification_type]) > -1
return filter(
notifications,
(n: Notification) =>
messagesTypes.indexOf(notificationTypes[n.notification_type]) > -1
);
};

Expand All @@ -18,7 +21,7 @@ export const normaliseUserNotifications = (
const alerts = notifications.filter(n => !includes(messages, n));

return {
alerts,
messages,
alerts: filter(alerts, { read: false }),
messages: filter(messages, { read: false }),
};
};

0 comments on commit 38ac3b2

Please sign in to comment.