aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/menu/avatar-notification.component.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-01-08 11:26:41 +0100
committerChocobozzz <chocobozzz@cpy.re>2019-01-09 11:15:15 +0100
commit2f1548fda32c3ba9e53913270394eedfacd55986 (patch)
treeafee28df36a9e00f921603d9091e5d08d5818159 /client/src/app/menu/avatar-notification.component.ts
parentf7cc67b455a12ccae9b0ea16876d166720364357 (diff)
downloadPeerTube-2f1548fda32c3ba9e53913270394eedfacd55986.tar.gz
PeerTube-2f1548fda32c3ba9e53913270394eedfacd55986.tar.zst
PeerTube-2f1548fda32c3ba9e53913270394eedfacd55986.zip
Add notifications in the client
Diffstat (limited to 'client/src/app/menu/avatar-notification.component.ts')
-rw-r--r--client/src/app/menu/avatar-notification.component.ts64
1 files changed, 64 insertions, 0 deletions
diff --git a/client/src/app/menu/avatar-notification.component.ts b/client/src/app/menu/avatar-notification.component.ts
new file mode 100644
index 000000000..60e090726
--- /dev/null
+++ b/client/src/app/menu/avatar-notification.component.ts
@@ -0,0 +1,64 @@
1import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'
2import { User } from '../shared/users/user.model'
3import { UserNotificationService } from '@app/shared/users/user-notification.service'
4import { Subscription } from 'rxjs'
5import { Notifier } from '@app/core'
6import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
7import { NavigationEnd, Router } from '@angular/router'
8import { 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})
15export class AvatarNotificationComponent implements OnInit, OnDestroy {
16 @ViewChild('popover') popover: NgbPopover
17 @Input() user: User
18
19 unreadNotifications = 0
20
21 private notificationSub: Subscription
22 private routeSub: Subscription
23
24 constructor (
25 private userNotificationService: UserNotificationService,
26 private notifier: Notifier,
27 private router: Router
28 ) {}
29
30 ngOnInit () {
31 this.userNotificationService.countUnreadNotifications()
32 .subscribe(
33 result => {
34 this.unreadNotifications = Math.min(result, 99) // Limit number to 99
35 this.subscribeToNotifications()
36 },
37
38 err => this.notifier.error(err.message)
39 )
40
41 this.routeSub = this.router.events
42 .pipe(filter(event => event instanceof NavigationEnd))
43 .subscribe(() => this.closePopover())
44 }
45
46 ngOnDestroy () {
47 if (this.notificationSub) this.notificationSub.unsubscribe()
48 if (this.routeSub) this.routeSub.unsubscribe()
49 }
50
51 closePopover () {
52 this.popover.close()
53 }
54
55 private subscribeToNotifications () {
56 this.notificationSub = this.userNotificationService.getMyNotificationsSocket()
57 .subscribe(data => {
58 if (data.type === 'new') return this.unreadNotifications++
59 if (data.type === 'read') return this.unreadNotifications--
60 if (data.type === 'read-all') return this.unreadNotifications = 0
61 })
62 }
63
64}