1 import { Component, Input, OnInit } 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 notifications: UserNotification[] = []
20 // So we can access it in the template
21 UserNotificationType = UserNotificationType
23 componentPagination: ComponentPagination
26 private userNotificationService: UserNotificationService,
27 private notifier: Notifier
31 this.componentPagination = {
33 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
37 this.loadMoreNotifications()
40 loadMoreNotifications () {
41 this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar)
44 this.notifications = this.notifications.concat(result.data)
45 this.componentPagination.totalItems = result.total
48 err => this.notifier.error(err.message)
53 if (this.infiniteScroll === false) return
55 this.componentPagination.currentPage++
57 if (hasMoreItems(this.componentPagination)) {
58 this.loadMoreNotifications()
62 markAsRead (notification: UserNotification) {
63 if (notification.read) return
65 this.userNotificationService.markAsRead(notification)
68 notification.read = true
71 err => this.notifier.error(err.message)
76 this.userNotificationService.markAllAsRead()
79 for (const notification of this.notifications) {
80 notification.read = true
84 err => this.notifier.error(err.message)