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