Refactor how we use icons
[oweals/peertube.git] / client / src / app / shared / users / user-notifications.component.ts
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'
7
8 @Component({
9   selector: 'my-user-notifications',
10   templateUrl: 'user-notifications.component.html',
11   styleUrls: [ 'user-notifications.component.scss' ]
12 })
13 export class UserNotificationsComponent implements OnInit {
14   @Input() ignoreLoadingBar = false
15   @Input() infiniteScroll = true
16   @Input() itemsPerPage = 20
17
18   notifications: UserNotification[] = []
19
20   // So we can access it in the template
21   UserNotificationType = UserNotificationType
22
23   componentPagination: ComponentPagination
24
25   constructor (
26     private userNotificationService: UserNotificationService,
27     private notifier: Notifier
28   ) { }
29
30   ngOnInit () {
31     this.componentPagination = {
32       currentPage: 1,
33       itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
34       totalItems: null
35     }
36
37     this.loadMoreNotifications()
38   }
39
40   loadMoreNotifications () {
41     this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar)
42         .subscribe(
43           result => {
44             this.notifications = this.notifications.concat(result.data)
45             this.componentPagination.totalItems = result.total
46           },
47
48           err => this.notifier.error(err.message)
49         )
50   }
51
52   onNearOfBottom () {
53     if (this.infiniteScroll === false) return
54
55     this.componentPagination.currentPage++
56
57     if (hasMoreItems(this.componentPagination)) {
58       this.loadMoreNotifications()
59     }
60   }
61
62   markAsRead (notification: UserNotification) {
63     if (notification.read) return
64
65     this.userNotificationService.markAsRead(notification)
66         .subscribe(
67           () => {
68             notification.read = true
69           },
70
71           err => this.notifier.error(err.message)
72         )
73   }
74
75   markAllAsRead () {
76     this.userNotificationService.markAllAsRead()
77         .subscribe(
78           () => {
79             for (const notification of this.notifications) {
80               notification.read = true
81             }
82           },
83
84           err => this.notifier.error(err.message)
85         )
86   }
87 }