-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
NotificationListState.ts
65 lines (51 loc) · 1.49 KB
/
NotificationListState.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import app from '../../forum/app';
import PaginatedListState from '../../common/states/PaginatedListState';
import Notification from '../../common/models/Notification';
export default class NotificationListState extends PaginatedListState<Notification> {
constructor() {
super({}, 1, null);
}
get type(): string {
return 'notifications';
}
/**
* Load the next page of notification results.
*/
load(): Promise<void> {
if (app.session.user?.newNotificationCount()) {
this.pages = [];
this.location = { page: 1 };
}
if (this.pages.length > 0) {
return Promise.resolve();
}
app.session.user?.pushAttributes({ newNotificationCount: 0 });
return super.loadNext();
}
/**
* Mark all of the notifications as read.
*/
markAllAsRead() {
if (this.pages.length === 0) return;
app.session.user?.pushAttributes({ unreadNotificationCount: 0 });
this.pages.forEach((page) => {
page.items.forEach((notification) => notification.pushAttributes({ isRead: true }));
});
return app.request({
url: app.forum.attribute('apiUrl') + '/notifications/read',
method: 'POST',
});
}
/**
* Delete all of the notifications for this user.
*/
deleteAll() {
if (this.pages.length === 0) return;
app.session.user?.pushAttributes({ unreadNotificationCount: 0 });
this.pages = [];
return app.request({
url: app.forum.attribute('apiUrl') + '/notifications',
method: 'DELETE',
});
}
}