1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
2 import { UserNotificationService } from '@app/shared/users/user-notification.service'
3 import { UserNotificationType } from '../../../../../shared'
4 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
5 import { Notifier } from '@app/core'
6 import { UserNotification } from '@app/shared/users/user-notification.model'
7 import { Subject } from 'rxjs'
10 selector: 'my-user-notifications',
11 templateUrl: 'user-notifications.component.html',
12 styleUrls: [ 'user-notifications.component.scss' ]
14 export class UserNotificationsComponent implements OnInit {
15 @Input() ignoreLoadingBar = false
16 @Input() infiniteScroll = true
17 @Input() itemsPerPage = 20
18 @Input() markAllAsReadSubject: Subject<boolean>
20 @Output() notificationsLoaded = new EventEmitter()
22 notifications: UserNotification[] = []
24 // So we can access it in the template
25 UserNotificationType = UserNotificationType
27 componentPagination: ComponentPagination
29 onDataSubject = new Subject<any[]>()
32 private userNotificationService: UserNotificationService,
33 private notifier: Notifier
37 this.componentPagination = {
39 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
43 this.loadMoreNotifications()
45 if (this.markAllAsReadSubject) {
46 this.markAllAsReadSubject.subscribe(() => this.markAllAsRead())
50 loadMoreNotifications () {
51 this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar)
54 this.notifications = this.notifications.concat(result.data)
55 this.componentPagination.totalItems = result.total
57 this.notificationsLoaded.emit()
59 this.onDataSubject.next(result.data)
62 err => this.notifier.error(err.message)
67 if (this.infiniteScroll === false) return
69 this.componentPagination.currentPage++
71 if (hasMoreItems(this.componentPagination)) {
72 this.loadMoreNotifications()
76 markAsRead (notification: UserNotification) {
77 if (notification.read) return
79 this.userNotificationService.markAsRead(notification)
82 notification.read = true
85 err => this.notifier.error(err.message)
90 this.userNotificationService.markAllAsRead()
93 for (const notification of this.notifications) {
94 notification.read = true
98 err => this.notifier.error(err.message)