Skip to content

Commit

Permalink
Add getUnreadCount
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Apr 6, 2022
1 parent dc1d9c9 commit 41a8799
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
20 changes: 15 additions & 5 deletions src/notification/NotificationControllerV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
GetNotificationCount,
GetNotifications,
GetNotificationState,
MarkNotificationViewed,
GetUnreadNotificationCount,
MarkNotificationRead,
NotificationController,
NotificationMessenger,
NotificationStateChange,
Expand All @@ -26,8 +27,9 @@ function getUnrestrictedMessenger() {
| GetNotificationState
| ShowNotification
| DismissNotification
| MarkNotificationViewed
| MarkNotificationRead
| GetNotificationCount
| GetUnreadNotificationCount
| GetNotifications
| GetSubjectMetadataState,
NotificationStateChange
Expand Down Expand Up @@ -147,7 +149,7 @@ describe('NotificationControllerV2', () => {
id: expect.any(String),
date: expect.any(Number),
origin,
viewed: false,
read: false,
message,
title: SNAP_NAME,
type: NotificationType.InApp,
Expand Down Expand Up @@ -185,13 +187,21 @@ describe('NotificationControllerV2', () => {
'NotificationControllerV2:getNotifications',
);
await unrestricted.call(
'NotificationControllerV2:markViewed',
'NotificationControllerV2:markRead',
notifications[0].id,
);

expect(
await unrestricted.call('NotificationControllerV2:getNotifications'),
).toContainEqual({ ...notifications[0], viewed: true });
).toContainEqual({ ...notifications[0], read: true });

expect(await unrestricted.call('NotificationControllerV2:getCount')).toBe(
1,
);

expect(
await unrestricted.call('NotificationControllerV2:getUnreadCount'),
).toBe(0);
});

it('action: NotificationControllerV2:dismiss', async () => {
Expand Down
45 changes: 33 additions & 12 deletions src/notification/NotificationControllerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type Notification = {
type: NotificationType;
origin: string;
date: number;
viewed: boolean;
read: boolean;
title: string;
message: string;
};
Expand Down Expand Up @@ -64,9 +64,9 @@ export type DismissNotification = {
handler: NotificationController['dismiss'];
};

export type MarkNotificationViewed = {
type: `${typeof name}:markViewed`;
handler: NotificationController['markViewed'];
export type MarkNotificationRead = {
type: `${typeof name}:markRead`;
handler: NotificationController['markRead'];
};

export type GetNotifications = {
Expand All @@ -79,13 +79,19 @@ export type GetNotificationCount = {
handler: NotificationController['getCount'];
};

export type GetUnreadNotificationCount = {
type: `${typeof name}:getUnreadCount`;
handler: NotificationController['getUnreadCount'];
};

export type ControllerActions =
| GetNotificationState
| ShowNotification
| DismissNotification
| MarkNotificationViewed
| MarkNotificationRead
| GetNotifications
| GetNotificationCount;
| GetNotificationCount
| GetUnreadNotificationCount;

type AllowedActions = GetSubjectMetadataState;

Expand Down Expand Up @@ -155,8 +161,8 @@ export class NotificationController extends BaseController<
);

this.messagingSystem.registerActionHandler(
`${name}:markViewed` as const,
(id: string) => this.markViewed(id),
`${name}:markRead` as const,
(id: string) => this.markRead(id),
);

this.messagingSystem.registerActionHandler(
Expand All @@ -168,6 +174,11 @@ export class NotificationController extends BaseController<
`${name}:getCount` as const,
() => this.getCount(),
);

this.messagingSystem.registerActionHandler(
`${name}:getUnreadCount` as const,
() => this.getUnreadCount(),
);
}

/**
Expand Down Expand Up @@ -203,7 +214,7 @@ export class NotificationController extends BaseController<
type: NotificationType.InApp,
origin,
date: Date.now(),
viewed: false,
read: false,
title,
message,
};
Expand All @@ -226,14 +237,14 @@ export class NotificationController extends BaseController<
}

/**
* Marks a notification as viewed.
* Marks a notification as read.
*
* @param id - The notification's ID
*/
markViewed(id: string) {
markRead(id: string) {
this.update((state) => {
if (hasProperty(state.notifications, id)) {
state.notifications[id].viewed = true;
state.notifications[id].read = true;
}
});
}
Expand All @@ -255,4 +266,14 @@ export class NotificationController extends BaseController<
getCount() {
return Object.values(this.state.notifications).length;
}

/**
* Gets the current number of unread notifications.
*
* @returns The number of current notifications
*/
getUnreadCount() {
return Object.values(this.state.notifications).filter((n) => !n.read)
.length;
}
}

0 comments on commit 41a8799

Please sign in to comment.