]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
ad453580 1import { Subject } from 'rxjs'
67ed6552
C
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'
2f1548fd
C
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
9a39392a 16 @Input() itemsPerPage = 20
bc6f8863 17 @Input() markAllAsReadSubject: Subject<boolean>
2f1548fd 18
b28e4e5e
C
19 @Output() notificationsLoaded = new EventEmitter()
20
2f1548fd 21 notifications: UserNotification[] = []
654a188f 22 sortField = 'createdAt'
2f1548fd
C
23
24 // So we can access it in the template
25 UserNotificationType = UserNotificationType
26
457bb213 27 componentPagination: ComponentPagination
2f1548fd 28
ad453580
C
29 onDataSubject = new Subject<any[]>()
30
2f1548fd
C
31 constructor (
32 private userNotificationService: UserNotificationService,
33 private notifier: Notifier
34 ) { }
35
36 ngOnInit () {
457bb213
C
37 this.componentPagination = {
38 currentPage: 1,
39 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
40 totalItems: null
41 }
42
654a188f 43 this.loadNotifications()
bc6f8863
C
44
45 if (this.markAllAsReadSubject) {
46 this.markAllAsReadSubject.subscribe(() => this.markAllAsRead())
47 }
2f1548fd
C
48 }
49
654a188f
RK
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 })
2f1548fd
C
59 .subscribe(
60 result => {
654a188f 61 this.notifications = reset ? result.data : this.notifications.concat(result.data)
2f1548fd 62 this.componentPagination.totalItems = result.total
b28e4e5e
C
63
64 this.notificationsLoaded.emit()
ad453580
C
65
66 this.onDataSubject.next(result.data)
2f1548fd
C
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)) {
654a188f 79 this.loadNotifications()
2f1548fd
C
80 }
81 }
82
83 markAsRead (notification: UserNotification) {
457bb213
C
84 if (notification.read) return
85
2f1548fd
C
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 }
654a188f
RK
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 }
2f1548fd 118}