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