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) {
48 this.userNotificationService.listMyNotifications({
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
59 this.notifications = reset ? result.data : this.notifications.concat(result.data)
60 this.componentPagination.totalItems = result.total
62 this.notificationsLoaded.emit()
64 this.onDataSubject.next(result.data)
67 err => this.notifier.error(err.message)
72 if (this.infiniteScroll === false) return
74 this.componentPagination.currentPage++
76 if (hasMoreItems(this.componentPagination)) {
77 this.loadNotifications()
81 markAsRead (notification: UserNotification) {
82 if (notification.read) return
84 this.userNotificationService.markAsRead(notification)
87 notification.read = true
90 err => this.notifier.error(err.message)
95 this.userNotificationService.markAllAsRead()
98 for (const notification of this.notifications) {
99 notification.read = true
103 err => this.notifier.error(err.message)
107 changeSortColumn (column: string) {
108 this.componentPagination = {
110 itemsPerPage: this.itemsPerPage,
113 this.sortField = column
114 this.loadNotifications(true)
117 isAccepted (notification: UserNotification) {
118 return notification.abuse.state === AbuseState.ACCEPTED