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