]>
Commit | Line | Data |
---|---|---|
20d21199 | 1 | import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core' |
170726f5 C |
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' | |
a51bad1a | 5 | import { RestExtractor } from '@app/shared' |
4d089429 C |
6 | import { catchError, distinctUntilChanged, map, switchMap } from 'rxjs/operators' |
7 | import { Subscription } from 'rxjs' | |
a0dedc02 | 8 | import { AuthService, Notifier } from '@app/core' |
20d21199 RK |
9 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
10 | import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component' | |
e33f888b | 11 | import { I18n } from '@ngx-translate/i18n-polyfill' |
170726f5 C |
12 | |
13 | @Component({ | |
14 | templateUrl: './video-channels.component.html', | |
15 | styleUrls: [ './video-channels.component.scss' ] | |
16 | }) | |
734a5ceb | 17 | export class VideoChannelsComponent implements OnInit, OnDestroy { |
f36da21e | 18 | @ViewChild('subscribeButton', { static: false }) subscribeButton: SubscribeButtonComponent |
20d21199 | 19 | |
170726f5 | 20 | videoChannel: VideoChannel |
20d21199 | 21 | hotkeys: Hotkey[] |
170726f5 | 22 | |
734a5ceb C |
23 | private routeSub: Subscription |
24 | ||
170726f5 | 25 | constructor ( |
e33f888b | 26 | private i18n: I18n, |
170726f5 | 27 | private route: ActivatedRoute, |
a0dedc02 | 28 | private notifier: Notifier, |
2d3741d6 | 29 | private authService: AuthService, |
a51bad1a | 30 | private videoChannelService: VideoChannelService, |
20d21199 RK |
31 | private restExtractor: RestExtractor, |
32 | private hotkeysService: HotkeysService | |
734a5ceb | 33 | ) { } |
170726f5 C |
34 | |
35 | ngOnInit () { | |
734a5ceb C |
36 | this.routeSub = this.route.params |
37 | .pipe( | |
2f1548fd | 38 | map(params => params[ 'videoChannelName' ]), |
734a5ceb | 39 | distinctUntilChanged(), |
2f1548fd | 40 | switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)), |
734a5ceb C |
41 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) |
42 | ) | |
43 | .subscribe(videoChannel => this.videoChannel = videoChannel) | |
44 | ||
20d21199 RK |
45 | this.hotkeys = [ |
46 | new Hotkey('S', (event: KeyboardEvent): boolean => { | |
47 | this.subscribeButton.subscribed ? | |
48 | this.subscribeButton.unsubscribe() : | |
49 | this.subscribeButton.subscribe() | |
50 | return false | |
e33f888b | 51 | }, undefined, this.i18n('Subscribe to the account')) |
20d21199 RK |
52 | ] |
53 | if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys) | |
734a5ceb | 54 | } |
170726f5 | 55 | |
734a5ceb C |
56 | ngOnDestroy () { |
57 | if (this.routeSub) this.routeSub.unsubscribe() | |
20d21199 RK |
58 | |
59 | // Unbind hotkeys | |
60 | if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys) | |
170726f5 | 61 | } |
2d3741d6 C |
62 | |
63 | isUserLoggedIn () { | |
64 | return this.authService.isLoggedIn() | |
65 | } | |
a0dedc02 RK |
66 | |
67 | activateCopiedMessage () { | |
68 | this.notifier.success(this.i18n('Username copied')) | |
69 | } | |
170726f5 | 70 | } |