aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/users/user-notifications.component.ts
blob: ce43b604a33a929abd25447310a5e2fb01f7efd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
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
  @Input() itemsPerPage = 20

  @Output() notificationsLoaded = new EventEmitter()

  notifications: UserNotification[] = []

  // So we can access it in the template
  UserNotificationType = UserNotificationType

  componentPagination: ComponentPagination

  constructor (
    private userNotificationService: UserNotificationService,
    private notifier: Notifier
  ) { }

  ngOnInit () {
    this.componentPagination = {
      currentPage: 1,
      itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
      totalItems: null
    }

    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

            this.notificationsLoaded.emit()
          },

          err => this.notifier.error(err.message)
        )
  }

  onNearOfBottom () {
    if (this.infiniteScroll === false) return

    this.componentPagination.currentPage++

    if (hasMoreItems(this.componentPagination)) {
      this.loadMoreNotifications()
    }
  }

  markAsRead (notification: UserNotification) {
    if (notification.read) return

    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)
        )
  }
}