]>
Commit | Line | Data |
---|---|---|
ad453580 | 1 | import { Subject } from 'rxjs' |
67ed6552 C |
2 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
3 | import { ComponentPagination, hasMoreItems, Notifier } from '@app/core' | |
29837f88 | 4 | import { AbuseState } from '@shared/models' |
67ed6552 C |
5 | import { UserNotification } from './user-notification.model' |
6 | import { 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 | }) | |
13 | export 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 | 47 | loadNotifications (reset?: boolean) { |
32a18cbf | 48 | const options = { |
654a188f RK |
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 | } | |
32a18cbf C |
56 | } |
57 | ||
58 | this.userNotificationService.listMyNotifications(options) | |
1378c0d3 C |
59 | .subscribe({ |
60 | next: 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 | ||
1378c0d3 C |
69 | error: err => this.notifier.error(err.message) |
70 | }) | |
2f1548fd C |
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 | 86 | this.userNotificationService.markAsRead(notification) |
1378c0d3 C |
87 | .subscribe({ |
88 | next: () => { | |
2f1548fd C |
89 | notification.read = true |
90 | }, | |
91 | ||
1378c0d3 C |
92 | error: err => this.notifier.error(err.message) |
93 | }) | |
2f1548fd C |
94 | } |
95 | ||
96 | markAllAsRead () { | |
97 | this.userNotificationService.markAllAsRead() | |
1378c0d3 C |
98 | .subscribe({ |
99 | next: () => { | |
2f1548fd C |
100 | for (const notification of this.notifications) { |
101 | notification.read = true | |
102 | } | |
103 | }, | |
104 | ||
1378c0d3 C |
105 | error: err => this.notifier.error(err.message) |
106 | }) | |
2f1548fd | 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 | } | |
d573926e C |
118 | |
119 | isAccepted (notification: UserNotification) { | |
120 | return notification.abuse.state === AbuseState.ACCEPTED | |
121 | } | |
2f1548fd | 122 | } |