2018-09-02 13:54:23 -05:00
|
|
|
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
|
2018-04-25 09:56:13 -05:00
|
|
|
import { ActivatedRoute } from '@angular/router'
|
|
|
|
import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
|
|
|
|
import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
|
2018-05-31 04:35:01 -05:00
|
|
|
import { RestExtractor } from '@app/shared'
|
2018-06-18 04:02:24 -05:00
|
|
|
import { catchError, distinctUntilChanged, map, switchMap } from 'rxjs/operators'
|
|
|
|
import { Subscription } from 'rxjs'
|
2018-08-30 07:58:00 -05:00
|
|
|
import { AuthService } from '@app/core'
|
2018-09-02 13:54:23 -05:00
|
|
|
import { Hotkey, HotkeysService } from 'angular2-hotkeys'
|
|
|
|
import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component'
|
2018-10-03 03:11:26 -05:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
2018-04-25 09:56:13 -05:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: './video-channels.component.html',
|
|
|
|
styleUrls: [ './video-channels.component.scss' ]
|
|
|
|
})
|
2018-06-07 04:19:26 -05:00
|
|
|
export class VideoChannelsComponent implements OnInit, OnDestroy {
|
2019-07-24 09:05:59 -05:00
|
|
|
@ViewChild('subscribeButton', { static: false }) subscribeButton: SubscribeButtonComponent
|
2018-09-02 13:54:23 -05:00
|
|
|
|
2018-04-25 09:56:13 -05:00
|
|
|
videoChannel: VideoChannel
|
2018-09-02 13:54:23 -05:00
|
|
|
hotkeys: Hotkey[]
|
2018-04-25 09:56:13 -05:00
|
|
|
|
2018-06-07 04:19:26 -05:00
|
|
|
private routeSub: Subscription
|
|
|
|
|
2018-04-25 09:56:13 -05:00
|
|
|
constructor (
|
2018-10-03 03:11:26 -05:00
|
|
|
private i18n: I18n,
|
2018-04-25 09:56:13 -05:00
|
|
|
private route: ActivatedRoute,
|
2018-08-30 07:58:00 -05:00
|
|
|
private authService: AuthService,
|
2018-05-31 04:35:01 -05:00
|
|
|
private videoChannelService: VideoChannelService,
|
2018-09-02 13:54:23 -05:00
|
|
|
private restExtractor: RestExtractor,
|
|
|
|
private hotkeysService: HotkeysService
|
2018-06-07 04:19:26 -05:00
|
|
|
) { }
|
2018-04-25 09:56:13 -05:00
|
|
|
|
|
|
|
ngOnInit () {
|
2018-06-07 04:19:26 -05:00
|
|
|
this.routeSub = this.route.params
|
|
|
|
.pipe(
|
2019-01-08 04:26:41 -06:00
|
|
|
map(params => params[ 'videoChannelName' ]),
|
2018-06-07 04:19:26 -05:00
|
|
|
distinctUntilChanged(),
|
2019-01-08 04:26:41 -06:00
|
|
|
switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)),
|
2018-06-07 04:19:26 -05:00
|
|
|
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
|
|
|
|
)
|
|
|
|
.subscribe(videoChannel => this.videoChannel = videoChannel)
|
|
|
|
|
2018-09-02 13:54:23 -05:00
|
|
|
this.hotkeys = [
|
|
|
|
new Hotkey('S', (event: KeyboardEvent): boolean => {
|
|
|
|
this.subscribeButton.subscribed ?
|
|
|
|
this.subscribeButton.unsubscribe() :
|
|
|
|
this.subscribeButton.subscribe()
|
|
|
|
return false
|
2018-10-03 03:11:26 -05:00
|
|
|
}, undefined, this.i18n('Subscribe to the account'))
|
2018-09-02 13:54:23 -05:00
|
|
|
]
|
|
|
|
if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys)
|
2018-06-07 04:19:26 -05:00
|
|
|
}
|
2018-04-25 09:56:13 -05:00
|
|
|
|
2018-06-07 04:19:26 -05:00
|
|
|
ngOnDestroy () {
|
|
|
|
if (this.routeSub) this.routeSub.unsubscribe()
|
2018-09-02 13:54:23 -05:00
|
|
|
|
|
|
|
// Unbind hotkeys
|
|
|
|
if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys)
|
2018-04-25 09:56:13 -05:00
|
|
|
}
|
2018-08-30 07:58:00 -05:00
|
|
|
|
|
|
|
isUserLoggedIn () {
|
|
|
|
return this.authService.isLoggedIn()
|
|
|
|
}
|
2018-04-25 09:56:13 -05:00
|
|
|
}
|