Fix account/channel pages route subscription
[oweals/peertube.git] / client / src / app / +video-channels / video-channels.component.ts
1 import { Component, OnInit, OnDestroy } 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, switchMap, map, distinctUntilChanged } from 'rxjs/operators'
7 import { Subscription } from 'rxjs/Subscription'
8
9 @Component({
10   templateUrl: './video-channels.component.html',
11   styleUrls: [ './video-channels.component.scss' ]
12 })
13 export class VideoChannelsComponent implements OnInit, OnDestroy {
14   videoChannel: VideoChannel
15
16   private routeSub: Subscription
17
18   constructor (
19     private route: ActivatedRoute,
20     private videoChannelService: VideoChannelService,
21     private restExtractor: RestExtractor
22   ) { }
23
24   ngOnInit () {
25     this.routeSub = this.route.params
26                         .pipe(
27                           map(params => params[ 'videoChannelId' ]),
28                           distinctUntilChanged(),
29                           switchMap(videoChannelId => this.videoChannelService.getVideoChannel(videoChannelId)),
30                           catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
31                         )
32                         .subscribe(videoChannel => this.videoChannel = videoChannel)
33
34   }
35
36   ngOnDestroy () {
37     if (this.routeSub) this.routeSub.unsubscribe()
38   }
39 }