]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/users/user-notifications.component.ts
e3913ba566c73372291e78acdb9f383217305ad6
[github/Chocobozzz/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 currentPage: 1,
25 itemsPerPage: this.itemsPerPage,
26 totalItems: null
27 }
28
29 constructor (
30 private userNotificationService: UserNotificationService,
31 private notifier: Notifier
32 ) { }
33
34 ngOnInit () {
35 this.loadMoreNotifications()
36 }
37
38 loadMoreNotifications () {
39 this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar)
40 .subscribe(
41 result => {
42 this.notifications = this.notifications.concat(result.data)
43 this.componentPagination.totalItems = result.total
44 },
45
46 err => this.notifier.error(err.message)
47 )
48 }
49
50 onNearOfBottom () {
51 if (this.infiniteScroll === false) return
52
53 this.componentPagination.currentPage++
54
55 if (hasMoreItems(this.componentPagination)) {
56 this.loadMoreNotifications()
57 }
58 }
59
60 markAsRead (notification: UserNotification) {
61 this.userNotificationService.markAsRead(notification)
62 .subscribe(
63 () => {
64 notification.read = true
65 },
66
67 err => this.notifier.error(err.message)
68 )
69 }
70
71 markAllAsRead () {
72 this.userNotificationService.markAllAsRead()
73 .subscribe(
74 () => {
75 for (const notification of this.notifications) {
76 notification.read = true
77 }
78 },
79
80 err => this.notifier.error(err.message)
81 )
82 }
83 }