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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
import { Subject } from 'rxjs'
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
import { ComponentPagination, hasMoreItems, Notifier } from '@app/core'
import { AbuseState } from '@shared/models'
import { UserNotification } from './user-notification.model'
import { UserNotificationService } from './user-notification.service'
@Component({
selector: 'my-user-notifications',
templateUrl: 'user-notifications.component.html',
styleUrls: [ 'user-notifications.component.scss' ]
})
export class UserNotificationsComponent implements OnInit {
@Input() ignoreLoadingBar = false
@Input() infiniteScroll = true
@Input() itemsPerPage = 20
@Input() markAllAsReadSubject: Subject<boolean>
@Output() notificationsLoaded = new EventEmitter()
notifications: UserNotification[] = []
sortField = 'createdAt'
componentPagination: ComponentPagination
onDataSubject = new Subject<any[]>()
constructor (
private userNotificationService: UserNotificationService,
private notifier: Notifier
) { }
ngOnInit () {
this.componentPagination = {
currentPage: 1,
itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
totalItems: null
}
this.loadNotifications()
if (this.markAllAsReadSubject) {
this.markAllAsReadSubject.subscribe(() => this.markAllAsRead())
}
}
loadNotifications (reset?: boolean) {
const options = {
pagination: this.componentPagination,
ignoreLoadingBar: this.ignoreLoadingBar,
sort: {
field: this.sortField,
// if we order by creation date, we want DESC. all other fields are ASC (like unread).
order: this.sortField === 'createdAt' ? -1 : 1
}
}
this.userNotificationService.listMyNotifications(options)
.subscribe({
next: result => {
this.notifications = reset ? result.data : this.notifications.concat(result.data)
this.componentPagination.totalItems = result.total
this.notificationsLoaded.emit()
this.onDataSubject.next(result.data)
},
error: err => this.notifier.error(err.message)
})
}
onNearOfBottom () {
if (this.infiniteScroll === false) return
this.componentPagination.currentPage++
if (hasMoreItems(this.componentPagination)) {
this.loadNotifications()
}
}
markAsRead (notification: UserNotification) {
if (notification.read) return
this.userNotificationService.markAsRead(notification)
.subscribe({
next: () => {
notification.read = true
},
error: err => this.notifier.error(err.message)
})
}
markAllAsRead () {
this.userNotificationService.markAllAsRead()
.subscribe({
next: () => {
for (const notification of this.notifications) {
notification.read = true
}
},
error: err => this.notifier.error(err.message)
})
}
changeSortColumn (column: string) {
this.componentPagination = {
currentPage: 1,
itemsPerPage: this.itemsPerPage,
totalItems: null
}
this.sortField = column
this.loadNotifications(true)
}
isAccepted (notification: UserNotification) {
return notification.abuse.state === AbuseState.ACCEPTED
}
}
|