]>
Commit | Line | Data |
---|---|---|
2f1548fd C |
1 | import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core' |
2 | import { User } from '../shared/users/user.model' | |
3 | import { UserNotificationService } from '@app/shared/users/user-notification.service' | |
4 | import { Subscription } from 'rxjs' | |
9a39392a | 5 | import { Notifier, UserNotificationSocket } from '@app/core' |
2f1548fd C |
6 | import { NgbPopover } from '@ng-bootstrap/ng-bootstrap' |
7 | import { NavigationEnd, Router } from '@angular/router' | |
8 | import { filter } from 'rxjs/operators' | |
9 | ||
10 | @Component({ | |
11 | selector: 'my-avatar-notification', | |
12 | templateUrl: './avatar-notification.component.html', | |
13 | styleUrls: [ './avatar-notification.component.scss' ] | |
14 | }) | |
15 | export class AvatarNotificationComponent implements OnInit, OnDestroy { | |
f36da21e | 16 | @ViewChild('popover', { static: true }) popover: NgbPopover |
2f1548fd C |
17 | @Input() user: User |
18 | ||
19 | unreadNotifications = 0 | |
b28e4e5e | 20 | loaded = false |
2f1548fd C |
21 | |
22 | private notificationSub: Subscription | |
23 | private routeSub: Subscription | |
24 | ||
25 | constructor ( | |
26 | private userNotificationService: UserNotificationService, | |
9a39392a | 27 | private userNotificationSocket: UserNotificationSocket, |
2f1548fd C |
28 | private notifier: Notifier, |
29 | private router: Router | |
41d71344 C |
30 | ) { |
31 | } | |
2f1548fd C |
32 | |
33 | ngOnInit () { | |
34 | this.userNotificationService.countUnreadNotifications() | |
41d71344 C |
35 | .subscribe( |
36 | result => { | |
37 | this.unreadNotifications = Math.min(result, 99) // Limit number to 99 | |
38 | this.subscribeToNotifications() | |
39 | }, | |
2f1548fd | 40 | |
41d71344 C |
41 | err => this.notifier.error(err.message) |
42 | ) | |
2f1548fd C |
43 | |
44 | this.routeSub = this.router.events | |
45 | .pipe(filter(event => event instanceof NavigationEnd)) | |
46 | .subscribe(() => this.closePopover()) | |
47 | } | |
48 | ||
49 | ngOnDestroy () { | |
50 | if (this.notificationSub) this.notificationSub.unsubscribe() | |
51 | if (this.routeSub) this.routeSub.unsubscribe() | |
52 | } | |
53 | ||
54 | closePopover () { | |
55 | this.popover.close() | |
56 | } | |
57 | ||
b28e4e5e C |
58 | onPopoverHidden () { |
59 | this.loaded = false | |
60 | } | |
61 | ||
62 | onNotificationLoaded () { | |
63 | this.loaded = true | |
64 | } | |
65 | ||
10475dea RK |
66 | markAllAsRead () { |
67 | this.userNotificationService.markAllAsRead() | |
c5c09c1e RK |
68 | .subscribe( |
69 | () => { | |
70 | this.unreadNotifications = 0 | |
71 | }, | |
72 | ||
73 | err => this.notifier.error(err.message) | |
74 | ) | |
10475dea RK |
75 | } |
76 | ||
41d71344 C |
77 | private async subscribeToNotifications () { |
78 | const obs = await this.userNotificationSocket.getMyNotificationsSocket() | |
79 | ||
80 | this.notificationSub = obs.subscribe(data => { | |
81 | if (data.type === 'new') return this.unreadNotifications++ | |
82 | if (data.type === 'read') return this.unreadNotifications-- | |
83 | if (data.type === 'read-all') return this.unreadNotifications = 0 | |
84 | }) | |
2f1548fd C |
85 | } |
86 | ||
87 | } |