Add manage buttons for own channels and account, video counts (#2421)
[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, Notifier } 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', { static: false }) 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 notifier: Notifier,
29     private authService: AuthService,
30     private videoChannelService: VideoChannelService,
31     private restExtractor: RestExtractor,
32     private hotkeysService: HotkeysService
33   ) { }
34
35   ngOnInit () {
36     this.routeSub = this.route.params
37                         .pipe(
38                           map(params => params[ 'videoChannelName' ]),
39                           distinctUntilChanged(),
40                           switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)),
41                           catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
42                         )
43                         .subscribe(videoChannel => this.videoChannel = videoChannel)
44
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
51       }, undefined, this.i18n('Subscribe to the account'))
52     ]
53     if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys)
54   }
55
56   ngOnDestroy () {
57     if (this.routeSub) this.routeSub.unsubscribe()
58
59     // Unbind hotkeys
60     if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys)
61   }
62
63   isUserLoggedIn () {
64     return this.authService.isLoggedIn()
65   }
66
67   get isManageable () {
68     if (!this.isUserLoggedIn()) return false
69     return this.videoChannel.ownerAccount.userId === this.authService.getUser().id
70   }
71
72   activateCopiedMessage () {
73     this.notifier.success(this.i18n('Username copied'))
74   }
75 }