]>
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' |
fef213ca | 6 | import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators' |
4d089429 | 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 { |
2f5d2ec5 | 18 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent |
20d21199 | 19 | |
170726f5 | 20 | videoChannel: VideoChannel |
20d21199 | 21 | hotkeys: Hotkey[] |
fef213ca | 22 | isChannelManageable = false |
170726f5 | 23 | |
734a5ceb C |
24 | private routeSub: Subscription |
25 | ||
170726f5 | 26 | constructor ( |
e33f888b | 27 | private i18n: I18n, |
170726f5 | 28 | private route: ActivatedRoute, |
a0dedc02 | 29 | private notifier: Notifier, |
2d3741d6 | 30 | private authService: AuthService, |
a51bad1a | 31 | private videoChannelService: VideoChannelService, |
20d21199 RK |
32 | private restExtractor: RestExtractor, |
33 | private hotkeysService: HotkeysService | |
734a5ceb | 34 | ) { } |
170726f5 C |
35 | |
36 | ngOnInit () { | |
734a5ceb C |
37 | this.routeSub = this.route.params |
38 | .pipe( | |
2f1548fd | 39 | map(params => params[ 'videoChannelName' ]), |
734a5ceb | 40 | distinctUntilChanged(), |
2f1548fd | 41 | switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)), |
734a5ceb C |
42 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) |
43 | ) | |
fef213ca C |
44 | .subscribe(videoChannel => { |
45 | this.videoChannel = videoChannel | |
46 | ||
47 | if (this.authService.isLoggedIn()) { | |
48 | this.authService.userInformationLoaded | |
49 | .subscribe(() => { | |
50 | const channelUserId = this.videoChannel.ownerAccount.userId | |
51 | this.isChannelManageable = channelUserId && channelUserId === this.authService.getUser().id | |
52 | }) | |
53 | } | |
54 | }) | |
734a5ceb | 55 | |
20d21199 RK |
56 | this.hotkeys = [ |
57 | new Hotkey('S', (event: KeyboardEvent): boolean => { | |
58 | this.subscribeButton.subscribed ? | |
59 | this.subscribeButton.unsubscribe() : | |
60 | this.subscribeButton.subscribe() | |
61 | return false | |
e33f888b | 62 | }, undefined, this.i18n('Subscribe to the account')) |
20d21199 RK |
63 | ] |
64 | if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys) | |
734a5ceb | 65 | } |
170726f5 | 66 | |
734a5ceb C |
67 | ngOnDestroy () { |
68 | if (this.routeSub) this.routeSub.unsubscribe() | |
20d21199 RK |
69 | |
70 | // Unbind hotkeys | |
71 | if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys) | |
170726f5 | 72 | } |
2d3741d6 C |
73 | |
74 | isUserLoggedIn () { | |
75 | return this.authService.isLoggedIn() | |
76 | } | |
a0dedc02 | 77 | |
aa0f1963 RK |
78 | get isManageable () { |
79 | if (!this.isUserLoggedIn()) return false | |
80 | return this.videoChannel.ownerAccount.userId === this.authService.getUser().id | |
81 | } | |
82 | ||
a0dedc02 RK |
83 | activateCopiedMessage () { |
84 | this.notifier.success(this.i18n('Username copied')) | |
85 | } | |
170726f5 | 86 | } |