]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/users/user-notifications.component.ts
modify user-quota to match account settings style
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / users / user-notifications.component.ts
1 import { Subject } from 'rxjs'
2 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
3 import { ComponentPagination, hasMoreItems, Notifier } from '@app/core'
4 import { UserNotificationType } from '@shared/models'
5 import { UserNotification } from './user-notification.model'
6 import { 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 })
13 export 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 sortField = 'createdAt'
23
24 // So we can access it in the template
25 UserNotificationType = UserNotificationType
26
27 componentPagination: ComponentPagination
28
29 onDataSubject = new Subject<any[]>()
30
31 constructor (
32 private userNotificationService: UserNotificationService,
33 private notifier: Notifier
34 ) { }
35
36 ngOnInit () {
37 this.componentPagination = {
38 currentPage: 1,
39 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
40 totalItems: null
41 }
42
43 this.loadNotifications()
44
45 if (this.markAllAsReadSubject) {
46 this.markAllAsReadSubject.subscribe(() => this.markAllAsRead())
47 }
48 }
49
50 loadNotifications (reset?: boolean) {
51 this.userNotificationService.listMyNotifications({
52 pagination: this.componentPagination,
53 ignoreLoadingBar: this.ignoreLoadingBar,
54 sort: {
55 field: this.sortField,
56 order: this.sortField === 'createdAt' ? -1 : 1
57 }
58 })
59 .subscribe(
60 result => {
61 this.notifications = reset ? result.data : this.notifications.concat(result.data)
62 this.componentPagination.totalItems = result.total
63
64 this.notificationsLoaded.emit()
65
66 this.onDataSubject.next(result.data)
67 },
68
69 err => this.notifier.error(err.message)
70 )
71 }
72
73 onNearOfBottom () {
74 if (this.infiniteScroll === false) return
75
76 this.componentPagination.currentPage++
77
78 if (hasMoreItems(this.componentPagination)) {
79 this.loadNotifications()
80 }
81 }
82
83 markAsRead (notification: UserNotification) {
84 if (notification.read) return
85
86 this.userNotificationService.markAsRead(notification)
87 .subscribe(
88 () => {
89 notification.read = true
90 },
91
92 err => this.notifier.error(err.message)
93 )
94 }
95
96 markAllAsRead () {
97 this.userNotificationService.markAllAsRead()
98 .subscribe(
99 () => {
100 for (const notification of this.notifications) {
101 notification.read = true
102 }
103 },
104
105 err => this.notifier.error(err.message)
106 )
107 }
108
109 changeSortColumn (column: string) {
110 this.componentPagination = {
111 currentPage: 1,
112 itemsPerPage: this.itemsPerPage,
113 totalItems: null
114 }
115 this.sortField = column
116 this.loadNotifications(true)
117 }
118 }