]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/menu/avatar-notification.component.ts
Add ability to change email in client
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / avatar-notification.component.ts
CommitLineData
2f1548fd
C
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'
9a39392a 5import { Notifier, UserNotificationSocket } from '@app/core'
2f1548fd
C
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
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
41d71344
C
66 private async subscribeToNotifications () {
67 const obs = await this.userNotificationSocket.getMyNotificationsSocket()
68
69 this.notificationSub = obs.subscribe(data => {
70 if (data.type === 'new') return this.unreadNotifications++
71 if (data.type === 'read') return this.unreadNotifications--
72 if (data.type === 'read-all') return this.unreadNotifications = 0
73 })
2f1548fd
C
74 }
75
76}