]>
Commit | Line | Data |
---|---|---|
ad453580 | 1 | import { Subject } from 'rxjs' |
67ed6552 C |
2 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
3 | import { ComponentPagination, hasMoreItems, Notifier } from '@app/core' | |
d573926e | 4 | import { UserNotificationType, AbuseState } from '@shared/models' |
67ed6552 C |
5 | import { UserNotification } from './user-notification.model' |
6 | import { UserNotificationService } from './user-notification.service' | |
2f1548fd C |
7 | |
8 | @Component({ | |
9 | selector: 'my-user-notifications', | |
10 | templateUrl: 'user-notifications.component.html', | |
11 | styleUrls: [ 'user-notifications.component.scss' ] | |
12 | }) | |
13 | export class UserNotificationsComponent implements OnInit { | |
14 | @Input() ignoreLoadingBar = false | |
15 | @Input() infiniteScroll = true | |
9a39392a | 16 | @Input() itemsPerPage = 20 |
bc6f8863 | 17 | @Input() markAllAsReadSubject: Subject<boolean> |
2f1548fd | 18 | |
b28e4e5e C |
19 | @Output() notificationsLoaded = new EventEmitter() |
20 | ||
2f1548fd | 21 | notifications: UserNotification[] = [] |
654a188f | 22 | sortField = 'createdAt' |
2f1548fd C |
23 | |
24 | // So we can access it in the template | |
25 | UserNotificationType = UserNotificationType | |
26 | ||
457bb213 | 27 | componentPagination: ComponentPagination |
2f1548fd | 28 | |
ad453580 C |
29 | onDataSubject = new Subject<any[]>() |
30 | ||
2f1548fd C |
31 | constructor ( |
32 | private userNotificationService: UserNotificationService, | |
33 | private notifier: Notifier | |
34 | ) { } | |
35 | ||
36 | ngOnInit () { | |
457bb213 C |
37 | this.componentPagination = { |
38 | currentPage: 1, | |
39 | itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable | |
40 | totalItems: null | |
41 | } | |
42 | ||
654a188f | 43 | this.loadNotifications() |
bc6f8863 C |
44 | |
45 | if (this.markAllAsReadSubject) { | |
46 | this.markAllAsReadSubject.subscribe(() => this.markAllAsRead()) | |
47 | } | |
2f1548fd C |
48 | } |
49 | ||
654a188f RK |
50 | loadNotifications (reset?: boolean) { |
51 | this.userNotificationService.listMyNotifications({ | |
52 | pagination: this.componentPagination, | |
53 | ignoreLoadingBar: this.ignoreLoadingBar, | |
54 | sort: { | |
55 | field: this.sortField, | |
7b390964 | 56 | // if we order by creation date, we want DESC. all other fields are ASC (like unread). |
654a188f RK |
57 | order: this.sortField === 'createdAt' ? -1 : 1 |
58 | } | |
59 | }) | |
2f1548fd C |
60 | .subscribe( |
61 | result => { | |
654a188f | 62 | this.notifications = reset ? result.data : this.notifications.concat(result.data) |
2f1548fd | 63 | this.componentPagination.totalItems = result.total |
b28e4e5e C |
64 | |
65 | this.notificationsLoaded.emit() | |
ad453580 C |
66 | |
67 | this.onDataSubject.next(result.data) | |
2f1548fd C |
68 | }, |
69 | ||
70 | err => this.notifier.error(err.message) | |
71 | ) | |
72 | } | |
73 | ||
74 | onNearOfBottom () { | |
75 | if (this.infiniteScroll === false) return | |
76 | ||
77 | this.componentPagination.currentPage++ | |
78 | ||
79 | if (hasMoreItems(this.componentPagination)) { | |
654a188f | 80 | this.loadNotifications() |
2f1548fd C |
81 | } |
82 | } | |
83 | ||
84 | markAsRead (notification: UserNotification) { | |
457bb213 C |
85 | if (notification.read) return |
86 | ||
2f1548fd C |
87 | this.userNotificationService.markAsRead(notification) |
88 | .subscribe( | |
89 | () => { | |
90 | notification.read = true | |
91 | }, | |
92 | ||
93 | err => this.notifier.error(err.message) | |
94 | ) | |
95 | } | |
96 | ||
97 | markAllAsRead () { | |
98 | this.userNotificationService.markAllAsRead() | |
99 | .subscribe( | |
100 | () => { | |
101 | for (const notification of this.notifications) { | |
102 | notification.read = true | |
103 | } | |
104 | }, | |
105 | ||
106 | err => this.notifier.error(err.message) | |
107 | ) | |
108 | } | |
654a188f RK |
109 | |
110 | changeSortColumn (column: string) { | |
111 | this.componentPagination = { | |
112 | currentPage: 1, | |
113 | itemsPerPage: this.itemsPerPage, | |
114 | totalItems: null | |
115 | } | |
116 | this.sortField = column | |
117 | this.loadNotifications(true) | |
118 | } | |
d573926e C |
119 | |
120 | isAccepted (notification: UserNotification) { | |
121 | return notification.abuse.state === AbuseState.ACCEPTED | |
122 | } | |
2f1548fd | 123 | } |