]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/users/user-notifications.component.ts
Add next to stats github ci
[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'
d573926e 4import { UserNotificationType, AbuseState } from '@shared/models'
67ed6552
C
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 23
457bb213 24 componentPagination: ComponentPagination
2f1548fd 25
ad453580
C
26 onDataSubject = new Subject<any[]>()
27
2f1548fd
C
28 constructor (
29 private userNotificationService: UserNotificationService,
30 private notifier: Notifier
31 ) { }
32
33 ngOnInit () {
457bb213
C
34 this.componentPagination = {
35 currentPage: 1,
36 itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable
37 totalItems: null
38 }
39
654a188f 40 this.loadNotifications()
bc6f8863
C
41
42 if (this.markAllAsReadSubject) {
43 this.markAllAsReadSubject.subscribe(() => this.markAllAsRead())
44 }
2f1548fd
C
45 }
46
654a188f
RK
47 loadNotifications (reset?: boolean) {
48 this.userNotificationService.listMyNotifications({
49 pagination: this.componentPagination,
50 ignoreLoadingBar: this.ignoreLoadingBar,
51 sort: {
52 field: this.sortField,
7b390964 53 // if we order by creation date, we want DESC. all other fields are ASC (like unread).
654a188f
RK
54 order: this.sortField === 'createdAt' ? -1 : 1
55 }
56 })
2f1548fd
C
57 .subscribe(
58 result => {
654a188f 59 this.notifications = reset ? result.data : this.notifications.concat(result.data)
2f1548fd 60 this.componentPagination.totalItems = result.total
b28e4e5e
C
61
62 this.notificationsLoaded.emit()
ad453580
C
63
64 this.onDataSubject.next(result.data)
2f1548fd
C
65 },
66
67 err => this.notifier.error(err.message)
68 )
69 }
70
71 onNearOfBottom () {
72 if (this.infiniteScroll === false) return
73
74 this.componentPagination.currentPage++
75
76 if (hasMoreItems(this.componentPagination)) {
654a188f 77 this.loadNotifications()
2f1548fd
C
78 }
79 }
80
81 markAsRead (notification: UserNotification) {
457bb213
C
82 if (notification.read) return
83
2f1548fd
C
84 this.userNotificationService.markAsRead(notification)
85 .subscribe(
86 () => {
87 notification.read = true
88 },
89
90 err => this.notifier.error(err.message)
91 )
92 }
93
94 markAllAsRead () {
95 this.userNotificationService.markAllAsRead()
96 .subscribe(
97 () => {
98 for (const notification of this.notifications) {
99 notification.read = true
100 }
101 },
102
103 err => this.notifier.error(err.message)
104 )
105 }
654a188f
RK
106
107 changeSortColumn (column: string) {
108 this.componentPagination = {
109 currentPage: 1,
110 itemsPerPage: this.itemsPerPage,
111 totalItems: null
112 }
113 this.sortField = column
114 this.loadNotifications(true)
115 }
d573926e
C
116
117 isAccepted (notification: UserNotification) {
118 return notification.abuse.state === AbuseState.ACCEPTED
119 }
2f1548fd 120}