2019-02-20 10:16:04 +01:00
|
|
|
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
|
2019-01-08 11:26:41 +01:00
|
|
|
import { UserNotificationService } from '@app/shared/users/user-notification.service'
|
|
|
|
import { UserNotificationType } from '../../../../../shared'
|
|
|
|
import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
|
|
|
|
import { Notifier } from '@app/core'
|
|
|
|
import { UserNotification } from '@app/shared/users/user-notification.model'
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-user-notifications',
|
|
|
|
templateUrl: 'user-notifications.component.html',
|
|
|
|
styleUrls: [ 'user-notifications.component.scss' ]
|
|
|
|
})
|
|
|
|
export class UserNotificationsComponent implements OnInit {
|
|
|
|
@Input() ignoreLoadingBar = false
|
|
|
|
@Input() infiniteScroll = true
|
2019-01-14 15:32:09 +01:00
|
|
|
@Input() itemsPerPage = 20
|
2019-01-08 11:26:41 +01:00
|
|
|
|
2019-02-20 10:16:04 +01:00
|
|
|
@Output() notificationsLoaded = new EventEmitter()
|
|
|
|
|
2019-01-08 11:26:41 +01:00
|
|
|
notifications: UserNotification[] = []
|
|
|
|
|
|
|
|
// So we can access it in the template
|
|
|
|
UserNotificationType = UserNotificationType
|
|
|
|
|
2019-01-16 16:05:40 +01:00
|
|
|
componentPagination: ComponentPagination
|
2019-01-08 11:26:41 +01:00
|
|
|
|
|
|
|
constructor (
|
|
|
|
private userNotificationService: UserNotificationService,
|
|
|
|
private notifier: Notifier
|
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit () {
|
2019-01-16 16:05:40 +01:00
|
|
|
this.componentPagination = {
|
|
|
|
currentPage: 1,
|
|
|
|
itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
|
|
|
|
totalItems: null
|
|
|
|
}
|
|
|
|
|
2019-01-08 11:26:41 +01:00
|
|
|
this.loadMoreNotifications()
|
|
|
|
}
|
|
|
|
|
|
|
|
loadMoreNotifications () {
|
|
|
|
this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar)
|
|
|
|
.subscribe(
|
|
|
|
result => {
|
|
|
|
this.notifications = this.notifications.concat(result.data)
|
|
|
|
this.componentPagination.totalItems = result.total
|
2019-02-20 10:16:04 +01:00
|
|
|
|
|
|
|
this.notificationsLoaded.emit()
|
2019-01-08 11:26:41 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
onNearOfBottom () {
|
|
|
|
if (this.infiniteScroll === false) return
|
|
|
|
|
|
|
|
this.componentPagination.currentPage++
|
|
|
|
|
|
|
|
if (hasMoreItems(this.componentPagination)) {
|
|
|
|
this.loadMoreNotifications()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
markAsRead (notification: UserNotification) {
|
2019-01-16 16:05:40 +01:00
|
|
|
if (notification.read) return
|
|
|
|
|
2019-01-08 11:26:41 +01:00
|
|
|
this.userNotificationService.markAsRead(notification)
|
|
|
|
.subscribe(
|
|
|
|
() => {
|
|
|
|
notification.read = true
|
|
|
|
},
|
|
|
|
|
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
markAllAsRead () {
|
|
|
|
this.userNotificationService.markAllAsRead()
|
|
|
|
.subscribe(
|
|
|
|
() => {
|
|
|
|
for (const notification of this.notifications) {
|
|
|
|
notification.read = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|