]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/users/user-notifications.component.ts
add channel avatar to watch view
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user-notifications.component.ts
CommitLineData
b28e4e5e 1import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
2f1548fd
C
2import { UserNotificationService } from '@app/shared/users/user-notification.service'
3import { UserNotificationType } from '../../../../../shared'
4import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
5import { Notifier } from '@app/core'
6import { UserNotification } from '@app/shared/users/user-notification.model'
ad453580 7import { Subject } from 'rxjs'
2f1548fd
C
8
9@Component({
10 selector: 'my-user-notifications',
11 templateUrl: 'user-notifications.component.html',
12 styleUrls: [ 'user-notifications.component.scss' ]
13})
14export class UserNotificationsComponent implements OnInit {
15 @Input() ignoreLoadingBar = false
16 @Input() infiniteScroll = true
9a39392a 17 @Input() itemsPerPage = 20
2f1548fd 18
b28e4e5e
C
19 @Output() notificationsLoaded = new EventEmitter()
20
2f1548fd
C
21 notifications: UserNotification[] = []
22
23 // So we can access it in the template
24 UserNotificationType = UserNotificationType
25
457bb213 26 componentPagination: ComponentPagination
2f1548fd 27
ad453580
C
28 onDataSubject = new Subject<any[]>()
29
2f1548fd
C
30 constructor (
31 private userNotificationService: UserNotificationService,
32 private notifier: Notifier
33 ) { }
34
35 ngOnInit () {
457bb213
C
36 this.componentPagination = {
37 currentPage: 1,
38 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
39 totalItems: null
40 }
41
2f1548fd
C
42 this.loadMoreNotifications()
43 }
44
45 loadMoreNotifications () {
46 this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar)
47 .subscribe(
48 result => {
49 this.notifications = this.notifications.concat(result.data)
50 this.componentPagination.totalItems = result.total
b28e4e5e
C
51
52 this.notificationsLoaded.emit()
ad453580
C
53
54 this.onDataSubject.next(result.data)
2f1548fd
C
55 },
56
57 err => this.notifier.error(err.message)
58 )
59 }
60
61 onNearOfBottom () {
62 if (this.infiniteScroll === false) return
63
64 this.componentPagination.currentPage++
65
66 if (hasMoreItems(this.componentPagination)) {
67 this.loadMoreNotifications()
68 }
69 }
70
71 markAsRead (notification: UserNotification) {
457bb213
C
72 if (notification.read) return
73
2f1548fd
C
74 this.userNotificationService.markAsRead(notification)
75 .subscribe(
76 () => {
77 notification.read = true
78 },
79
80 err => this.notifier.error(err.message)
81 )
82 }
83
84 markAllAsRead () {
85 this.userNotificationService.markAllAsRead()
86 .subscribe(
87 () => {
88 for (const notification of this.notifications) {
89 notification.read = true
90 }
91 },
92
93 err => this.notifier.error(err.message)
94 )
95 }
96}