1 import { Subject } from 'rxjs'
2 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
3 import { ComponentPagination, hasMoreItems, Notifier } from '@app/core'
4 import { UserNotificationType, AbuseState } from '@shared/models'
5 import { UserNotification } from './user-notification.model'
6 import { UserNotificationService } from './user-notification.service'
9 selector: 'my-user-notifications',
10 templateUrl: 'user-notifications.component.html',
11 styleUrls: [ 'user-notifications.component.scss' ]
13 export class UserNotificationsComponent implements OnInit {
14 @Input() ignoreLoadingBar = false
15 @Input() infiniteScroll = true
16 @Input() itemsPerPage = 20
17 @Input() markAllAsReadSubject: Subject<boolean>
19 @Output() notificationsLoaded = new EventEmitter()
21 notifications: UserNotification[] = []
22 sortField = 'createdAt'
24 componentPagination: ComponentPagination
26 onDataSubject = new Subject<any[]>()
29 private userNotificationService: UserNotificationService,
30 private notifier: Notifier
34 this.componentPagination = {
36 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
40 this.loadNotifications()
42 if (this.markAllAsReadSubject) {
43 this.markAllAsReadSubject.subscribe(() => this.markAllAsRead())
47 loadNotifications (reset?: boolean) {
49 pagination: this.componentPagination,
50 ignoreLoadingBar: this.ignoreLoadingBar,
52 field: this.sortField,
53 // if we order by creation date, we want DESC. all other fields are ASC (like unread).
54 order: this.sortField === 'createdAt' ? -1 : 1
58 this.userNotificationService.listMyNotifications(options)
61 this.notifications = reset ? result.data : this.notifications.concat(result.data)
62 this.componentPagination.totalItems = result.total
64 this.notificationsLoaded.emit()
66 this.onDataSubject.next(result.data)
69 err => this.notifier.error(err.message)
74 if (this.infiniteScroll === false) return
76 this.componentPagination.currentPage++
78 if (hasMoreItems(this.componentPagination)) {
79 this.loadNotifications()
83 markAsRead (notification: UserNotification) {
84 if (notification.read) return
86 this.userNotificationService.markAsRead(notification)
89 notification.read = true
92 err => this.notifier.error(err.message)
97 this.userNotificationService.markAllAsRead()
100 for (const notification of this.notifications) {
101 notification.read = true
105 err => this.notifier.error(err.message)
109 changeSortColumn (column: string) {
110 this.componentPagination = {
112 itemsPerPage: this.itemsPerPage,
115 this.sortField = column
116 this.loadNotifications(true)
119 isAccepted (notification: UserNotification) {
120 return notification.abuse.state === AbuseState.ACCEPTED