]>
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' |
24e7916c | 12 | import { ListOverflowItem } from '@app/shared/misc/list-overflow.component' |
937b7a6a | 13 | import { ScreenService } from '@app/shared/misc/screen.service' |
170726f5 C |
14 | |
15 | @Component({ | |
16 | templateUrl: './video-channels.component.html', | |
17 | styleUrls: [ './video-channels.component.scss' ] | |
18 | }) | |
734a5ceb | 19 | export class VideoChannelsComponent implements OnInit, OnDestroy { |
2f5d2ec5 | 20 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent |
20d21199 | 21 | |
170726f5 | 22 | videoChannel: VideoChannel |
20d21199 | 23 | hotkeys: Hotkey[] |
24e7916c | 24 | links: ListOverflowItem[] = [] |
fef213ca | 25 | isChannelManageable = false |
170726f5 | 26 | |
734a5ceb C |
27 | private routeSub: Subscription |
28 | ||
170726f5 | 29 | constructor ( |
e33f888b | 30 | private i18n: I18n, |
170726f5 | 31 | private route: ActivatedRoute, |
a0dedc02 | 32 | private notifier: Notifier, |
2d3741d6 | 33 | private authService: AuthService, |
a51bad1a | 34 | private videoChannelService: VideoChannelService, |
20d21199 | 35 | private restExtractor: RestExtractor, |
937b7a6a | 36 | private hotkeysService: HotkeysService, |
edb86865 | 37 | private screenService: ScreenService |
734a5ceb | 38 | ) { } |
170726f5 C |
39 | |
40 | ngOnInit () { | |
734a5ceb C |
41 | this.routeSub = this.route.params |
42 | .pipe( | |
2f1548fd | 43 | map(params => params[ 'videoChannelName' ]), |
734a5ceb | 44 | distinctUntilChanged(), |
2f1548fd | 45 | switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)), |
734a5ceb C |
46 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) |
47 | ) | |
fef213ca C |
48 | .subscribe(videoChannel => { |
49 | this.videoChannel = videoChannel | |
50 | ||
51 | if (this.authService.isLoggedIn()) { | |
52 | this.authService.userInformationLoaded | |
53 | .subscribe(() => { | |
54 | const channelUserId = this.videoChannel.ownerAccount.userId | |
55 | this.isChannelManageable = channelUserId && channelUserId === this.authService.getUser().id | |
56 | }) | |
57 | } | |
58 | }) | |
734a5ceb | 59 | |
20d21199 RK |
60 | this.hotkeys = [ |
61 | new Hotkey('S', (event: KeyboardEvent): boolean => { | |
62 | this.subscribeButton.subscribed ? | |
63 | this.subscribeButton.unsubscribe() : | |
64 | this.subscribeButton.subscribe() | |
65 | return false | |
e33f888b | 66 | }, undefined, this.i18n('Subscribe to the account')) |
20d21199 RK |
67 | ] |
68 | if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys) | |
24e7916c RK |
69 | |
70 | this.links = [ | |
14571f19 RK |
71 | { label: this.i18n('VIDEOS'), routerLink: 'videos' }, |
72 | { label: this.i18n('VIDEO PLAYLISTS'), routerLink: 'video-playlists' }, | |
73 | { label: this.i18n('ABOUT'), routerLink: 'about' } | |
24e7916c | 74 | ] |
734a5ceb | 75 | } |
170726f5 | 76 | |
734a5ceb C |
77 | ngOnDestroy () { |
78 | if (this.routeSub) this.routeSub.unsubscribe() | |
20d21199 RK |
79 | |
80 | // Unbind hotkeys | |
81 | if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys) | |
170726f5 | 82 | } |
2d3741d6 | 83 | |
937b7a6a RK |
84 | get isInSmallView () { |
85 | return this.screenService.isInSmallView() | |
86 | } | |
87 | ||
2d3741d6 C |
88 | isUserLoggedIn () { |
89 | return this.authService.isLoggedIn() | |
90 | } | |
a0dedc02 | 91 | |
aa0f1963 RK |
92 | get isManageable () { |
93 | if (!this.isUserLoggedIn()) return false | |
94 | return this.videoChannel.ownerAccount.userId === this.authService.getUser().id | |
95 | } | |
96 | ||
a0dedc02 RK |
97 | activateCopiedMessage () { |
98 | this.notifier.success(this.i18n('Username copied')) | |
99 | } | |
170726f5 | 100 | } |