]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/avatar-notification.component.ts
Support i18n build
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / avatar-notification.component.ts
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'
4 import { Subject, Subscription } from 'rxjs'
5 import { Notifier, UserNotificationSocket } from '@app/core'
6 import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
7 import { NavigationEnd, Router } from '@angular/router'
8 import { filter } from 'rxjs/operators'
9 import { UserNotificationsComponent } from '@app/shared'
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 {
17 @ViewChild('popover', { static: true }) popover: NgbPopover
18
19 @Input() user: User
20
21 unreadNotifications = 0
22 loaded = false
23
24 markAllAsReadSubject = new Subject<boolean>()
25
26 private notificationSub: Subscription
27 private routeSub: Subscription
28
29 constructor (
30 private userNotificationService: UserNotificationService,
31 private userNotificationSocket: UserNotificationSocket,
32 private notifier: Notifier,
33 private router: Router
34 ) {
35 }
36
37 ngOnInit () {
38 this.userNotificationService.countUnreadNotifications()
39 .subscribe(
40 result => {
41 this.unreadNotifications = Math.min(result, 99) // Limit number to 99
42 this.subscribeToNotifications()
43 },
44
45 err => this.notifier.error(err.message)
46 )
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
62 onPopoverHidden () {
63 this.loaded = false
64 }
65
66 onNotificationLoaded () {
67 this.loaded = true
68 }
69
70 markAllAsRead () {
71 this.markAllAsReadSubject.next(true)
72 }
73
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 })
82 }
83
84 }