]>
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 |
2f1548fd | 18 | |
b28e4e5e C |
19 | @Output() notificationsLoaded = new EventEmitter() |
20 | ||
2f1548fd C |
21 | notifications: UserNotification[] = [] |
22 | ||
23 | // So we can access it in the template | |
24 | UserNotificationType = UserNotificationType | |
25 | ||
457bb213 | 26 | componentPagination: ComponentPagination |
2f1548fd | 27 | |
ad453580 C |
28 | onDataSubject = new Subject<any[]>() |
29 | ||
2f1548fd C |
30 | constructor ( |
31 | private userNotificationService: UserNotificationService, | |
32 | private notifier: Notifier | |
33 | ) { } | |
34 | ||
35 | ngOnInit () { | |
457bb213 C |
36 | this.componentPagination = { |
37 | currentPage: 1, | |
38 | itemsPerPage: this.itemsPerPage, // Reset items per page, because of the @Input() variable | |
39 | totalItems: null | |
40 | } | |
41 | ||
2f1548fd C |
42 | this.loadMoreNotifications() |
43 | } | |
44 | ||
45 | loadMoreNotifications () { | |
46 | this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar) | |
47 | .subscribe( | |
48 | result => { | |
49 | this.notifications = this.notifications.concat(result.data) | |
50 | this.componentPagination.totalItems = result.total | |
b28e4e5e C |
51 | |
52 | this.notificationsLoaded.emit() | |
ad453580 C |
53 | |
54 | this.onDataSubject.next(result.data) | |
2f1548fd C |
55 | }, |
56 | ||
57 | err => this.notifier.error(err.message) | |
58 | ) | |
59 | } | |
60 | ||
61 | onNearOfBottom () { | |
62 | if (this.infiniteScroll === false) return | |
63 | ||
64 | this.componentPagination.currentPage++ | |
65 | ||
66 | if (hasMoreItems(this.componentPagination)) { | |
67 | this.loadMoreNotifications() | |
68 | } | |
69 | } | |
70 | ||
71 | markAsRead (notification: UserNotification) { | |
457bb213 C |
72 | if (notification.read) return |
73 | ||
2f1548fd C |
74 | this.userNotificationService.markAsRead(notification) |
75 | .subscribe( | |
76 | () => { | |
77 | notification.read = true | |
78 | }, | |
79 | ||
80 | err => this.notifier.error(err.message) | |
81 | ) | |
82 | } | |
83 | ||
84 | markAllAsRead () { | |
85 | this.userNotificationService.markAllAsRead() | |
86 | .subscribe( | |
87 | () => { | |
88 | for (const notification of this.notifications) { | |
89 | notification.read = true | |
90 | } | |
91 | }, | |
92 | ||
93 | err => this.notifier.error(err.message) | |
94 | ) | |
95 | } | |
96 | } |