aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-main/users/user-notifications.component.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-06-23 14:10:17 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-06-23 16:00:49 +0200
commit67ed6552b831df66713bac9e672738796128d33f (patch)
tree59c97d41e0b49d75a90aa3de987968ab9b1ff447 /client/src/app/shared/shared-main/users/user-notifications.component.ts
parent0c4bacbff53bc732f5a2677d62a6ead7752e2405 (diff)
downloadPeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.gz
PeerTube-67ed6552b831df66713bac9e672738796128d33f.tar.zst
PeerTube-67ed6552b831df66713bac9e672738796128d33f.zip
Reorganize client shared modules
Diffstat (limited to 'client/src/app/shared/shared-main/users/user-notifications.component.ts')
-rw-r--r--client/src/app/shared/shared-main/users/user-notifications.component.ts100
1 files changed, 100 insertions, 0 deletions
diff --git a/client/src/app/shared/shared-main/users/user-notifications.component.ts b/client/src/app/shared/shared-main/users/user-notifications.component.ts
new file mode 100644
index 000000000..6abd8b7d8
--- /dev/null
+++ b/client/src/app/shared/shared-main/users/user-notifications.component.ts
@@ -0,0 +1,100 @@
1import { Subject } from 'rxjs'
2import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
3import { ComponentPagination, hasMoreItems, Notifier } from '@app/core'
4import { UserNotificationType } from '@shared/models'
5import { UserNotification } from './user-notification.model'
6import { UserNotificationService } from './user-notification.service'
7
8@Component({
9 selector: 'my-user-notifications',
10 templateUrl: 'user-notifications.component.html',
11 styleUrls: [ 'user-notifications.component.scss' ]
12})
13export class UserNotificationsComponent implements OnInit {
14 @Input() ignoreLoadingBar = false
15 @Input() infiniteScroll = true
16 @Input() itemsPerPage = 20
17 @Input() markAllAsReadSubject: Subject<boolean>
18
19 @Output() notificationsLoaded = new EventEmitter()
20
21 notifications: UserNotification[] = []
22
23 // So we can access it in the template
24 UserNotificationType = UserNotificationType
25
26 componentPagination: ComponentPagination
27
28 onDataSubject = new Subject<any[]>()
29
30 constructor (
31 private userNotificationService: UserNotificationService,
32 private notifier: Notifier
33 ) { }
34
35 ngOnInit () {
36 this.componentPagination = {
37 currentPage: 1,
38 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
39 totalItems: null
40 }
41
42 this.loadMoreNotifications()
43
44 if (this.markAllAsReadSubject) {
45 this.markAllAsReadSubject.subscribe(() => this.markAllAsRead())
46 }
47 }
48
49 loadMoreNotifications () {
50 this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar)
51 .subscribe(
52 result => {
53 this.notifications = this.notifications.concat(result.data)
54 this.componentPagination.totalItems = result.total
55
56 this.notificationsLoaded.emit()
57
58 this.onDataSubject.next(result.data)
59 },
60
61 err => this.notifier.error(err.message)
62 )
63 }
64
65 onNearOfBottom () {
66 if (this.infiniteScroll === false) return
67
68 this.componentPagination.currentPage++
69
70 if (hasMoreItems(this.componentPagination)) {
71 this.loadMoreNotifications()
72 }
73 }
74
75 markAsRead (notification: UserNotification) {
76 if (notification.read) return
77
78 this.userNotificationService.markAsRead(notification)
79 .subscribe(
80 () => {
81 notification.read = true
82 },
83
84 err => this.notifier.error(err.message)
85 )
86 }
87
88 markAllAsRead () {
89 this.userNotificationService.markAllAsRead()
90 .subscribe(
91 () => {
92 for (const notification of this.notifications) {
93 notification.read = true
94 }
95 },
96
97 err => this.notifier.error(err.message)
98 )
99 }
100}