0c5c814c74d9dbd6065e93b924c1d056bca5ca2f
[oweals/peertube.git] / client / src / app / +video-channels / video-channels.component.ts
1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
4 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
5 import { RestExtractor } from '@app/shared'
6 import { catchError, distinctUntilChanged, map, switchMap } from 'rxjs/operators'
7 import { Subscription } from 'rxjs'
8 import { AuthService } from '@app/core'
9 import { Hotkey, HotkeysService } from 'angular2-hotkeys'
10 import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12
13 @Component({
14   templateUrl: './video-channels.component.html',
15   styleUrls: [ './video-channels.component.scss' ]
16 })
17 export class VideoChannelsComponent implements OnInit, OnDestroy {
18   @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent
19
20   videoChannel: VideoChannel
21   hotkeys: Hotkey[]
22
23   private routeSub: Subscription
24
25   constructor (
26     private i18n: I18n,
27     private route: ActivatedRoute,
28     private authService: AuthService,
29     private videoChannelService: VideoChannelService,
30     private restExtractor: RestExtractor,
31     private hotkeysService: HotkeysService
32   ) { }
33
34   ngOnInit () {
35     this.routeSub = this.route.params
36                         .pipe(
37                           map(params => params[ 'videoChannelId' ]),
38                           distinctUntilChanged(),
39                           switchMap(videoChannelId => this.videoChannelService.getVideoChannel(videoChannelId)),
40                           catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
41                         )
42                         .subscribe(videoChannel => this.videoChannel = videoChannel)
43
44     this.hotkeys = [
45       new Hotkey('S', (event: KeyboardEvent): boolean => {
46         this.subscribeButton.subscribed ?
47           this.subscribeButton.unsubscribe() :
48           this.subscribeButton.subscribe()
49         return false
50       }, undefined, this.i18n('Subscribe to the account'))
51     ]
52     if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys)
53   }
54
55   ngOnDestroy () {
56     if (this.routeSub) this.routeSub.unsubscribe()
57
58     // Unbind hotkeys
59     if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys)
60   }
61
62   isUserLoggedIn () {
63     return this.authService.isLoggedIn()
64   }
65 }