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
19 @Output() notificationsLoaded = new EventEmitter()
21 notifications: UserNotification[] = []
23 // So we can access it in the template
24 UserNotificationType = UserNotificationType
26 componentPagination: ComponentPagination
28 onDataSubject = new Subject<any[]>()
31 private userNotificationService: UserNotificationService,
32 private notifier: Notifier
36 this.componentPagination = {
38 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
42 this.loadMoreNotifications()
45 loadMoreNotifications () {
46 this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar)
49 this.notifications = this.notifications.concat(result.data)
50 this.componentPagination.totalItems = result.total
52 this.notificationsLoaded.emit()
54 this.onDataSubject.next(result.data)
57 err => this.notifier.error(err.message)
62 if (this.infiniteScroll === false) return
64 this.componentPagination.currentPage++
66 if (hasMoreItems(this.componentPagination)) {
67 this.loadMoreNotifications()
71 markAsRead (notification: UserNotification) {
72 if (notification.read) return
74 this.userNotificationService.markAsRead(notification)
77 notification.read = true
80 err => this.notifier.error(err.message)
85 this.userNotificationService.markAllAsRead()
88 for (const notification of this.notifications) {
89 notification.read = true
93 err => this.notifier.error(err.message)