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'
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
18 @Output() notificationsLoaded = new EventEmitter()
20 notifications: UserNotification[] = []
22 // So we can access it in the template
23 UserNotificationType = UserNotificationType
25 componentPagination: ComponentPagination
28 private userNotificationService: UserNotificationService,
29 private notifier: Notifier
33 this.componentPagination = {
35 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
39 this.loadMoreNotifications()
42 loadMoreNotifications () {
43 this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar)
46 this.notifications = this.notifications.concat(result.data)
47 this.componentPagination.totalItems = result.total
49 this.notificationsLoaded.emit()
52 err => this.notifier.error(err.message)
57 if (this.infiniteScroll === false) return
59 this.componentPagination.currentPage++
61 if (hasMoreItems(this.componentPagination)) {
62 this.loadMoreNotifications()
66 markAsRead (notification: UserNotification) {
67 if (notification.read) return
69 this.userNotificationService.markAsRead(notification)
72 notification.read = true
75 err => this.notifier.error(err.message)
80 this.userNotificationService.markAllAsRead()
83 for (const notification of this.notifications) {
84 notification.read = true
88 err => this.notifier.error(err.message)