]>
Commit | Line | Data |
---|---|---|
b28e4e5e | 1 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
2f1548fd C |
2 | import { UserNotificationService } from '@app/shared/users/user-notification.service' |
3 | import { UserNotificationType } from '../../../../../shared' | |
4 | import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model' | |
5 | import { Notifier } from '@app/core' | |
6 | import { UserNotification } from '@app/shared/users/user-notification.model' | |
ad453580 | 7 | import { Subject } from 'rxjs' |
2f1548fd C |
8 | |
9 | @Component({ | |
10 | selector: 'my-user-notifications', | |
11 | templateUrl: 'user-notifications.component.html', | |
12 | styleUrls: [ 'user-notifications.component.scss' ] | |
13 | }) | |
14 | export class UserNotificationsComponent implements OnInit { | |
15 | @Input() ignoreLoadingBar = false | |
16 | @Input() infiniteScroll = true | |
9a39392a | 17 | @Input() itemsPerPage = 20 |
bc6f8863 | 18 | @Input() markAllAsReadSubject: Subject<boolean> |
2f1548fd | 19 | |
b28e4e5e C |
20 | @Output() notificationsLoaded = new EventEmitter() |
21 | ||
2f1548fd C |
22 | notifications: UserNotification[] = [] |
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 | ||
2f1548fd | 43 | this.loadMoreNotifications() |
bc6f8863 C |
44 | |
45 | if (this.markAllAsReadSubject) { | |
46 | this.markAllAsReadSubject.subscribe(() => this.markAllAsRead()) | |
47 | } | |
2f1548fd C |
48 | } |
49 | ||
50 | loadMoreNotifications () { | |
51 | this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar) | |
52 | .subscribe( | |
53 | result => { | |
54 | this.notifications = this.notifications.concat(result.data) | |
55 | this.componentPagination.totalItems = result.total | |
b28e4e5e C |
56 | |
57 | this.notificationsLoaded.emit() | |
ad453580 C |
58 | |
59 | this.onDataSubject.next(result.data) | |
2f1548fd C |
60 | }, |
61 | ||
62 | err => this.notifier.error(err.message) | |
63 | ) | |
64 | } | |
65 | ||
66 | onNearOfBottom () { | |
67 | if (this.infiniteScroll === false) return | |
68 | ||
69 | this.componentPagination.currentPage++ | |
70 | ||
71 | if (hasMoreItems(this.componentPagination)) { | |
72 | this.loadMoreNotifications() | |
73 | } | |
74 | } | |
75 | ||
76 | markAsRead (notification: UserNotification) { | |
457bb213 C |
77 | if (notification.read) return |
78 | ||
2f1548fd C |
79 | this.userNotificationService.markAsRead(notification) |
80 | .subscribe( | |
81 | () => { | |
82 | notification.read = true | |
83 | }, | |
84 | ||
85 | err => this.notifier.error(err.message) | |
86 | ) | |
87 | } | |
88 | ||
89 | markAllAsRead () { | |
90 | this.userNotificationService.markAllAsRead() | |
91 | .subscribe( | |
92 | () => { | |
93 | for (const notification of this.notifications) { | |
94 | notification.read = true | |
95 | } | |
96 | }, | |
97 | ||
98 | err => this.notifier.error(err.message) | |
99 | ) | |
100 | } | |
101 | } |