Fix account/channel pages route subscription
authorChocobozzz <me@florianbigard.com>
Thu, 7 Jun 2018 09:19:26 +0000 (11:19 +0200)
committerChocobozzz <me@florianbigard.com>
Thu, 7 Jun 2018 09:19:26 +0000 (11:19 +0200)
client/src/app/+accounts/account-about/account-about.component.ts
client/src/app/+accounts/account-video-channels/account-video-channels.component.ts
client/src/app/+accounts/account-videos/account-videos.component.ts
client/src/app/+accounts/accounts.component.ts
client/src/app/+video-channels/video-channel-about/video-channel-about.component.ts
client/src/app/+video-channels/video-channel-videos/video-channel-videos.component.ts
client/src/app/+video-channels/video-channels.component.ts
client/src/app/videos/shared/markdown.service.ts

index 4086510ba51c523c05ff89f168a2f11b776ea413..c0e79375468b766c48ac8598226d9cd56b3d7743 100644 (file)
@@ -1,17 +1,20 @@
-import { Component, OnInit } from '@angular/core'
+import { Component, OnInit, OnDestroy } from '@angular/core'
 import { ActivatedRoute } from '@angular/router'
 import { Account } from '@app/shared/account/account.model'
 import { AccountService } from '@app/shared/account/account.service'
 import { I18n } from '@ngx-translate/i18n-polyfill'
+import { Subscription } from 'rxjs'
 
 @Component({
   selector: 'my-account-about',
   templateUrl: './account-about.component.html',
   styleUrls: [ './account-about.component.scss' ]
 })
-export class AccountAboutComponent implements OnInit {
+export class AccountAboutComponent implements OnInit, OnDestroy {
   account: Account
 
+  private accountSub: Subscription
+
   constructor (
     private route: ActivatedRoute,
     private i18n: I18n,
@@ -20,10 +23,14 @@ export class AccountAboutComponent implements OnInit {
 
   ngOnInit () {
     // Parent get the account for us
-    this.accountService.accountLoaded
+    this.accountSub = this.accountService.accountLoaded
       .subscribe(account => this.account = account)
   }
 
+  ngOnDestroy () {
+    if (this.accountSub) this.accountSub.unsubscribe()
+  }
+
   getAccountDescription () {
     if (this.account.description) return this.account.description
 
index a6e6dd656bbf73ccbbce65efee233e67eb8d6fff..ebc6711136e2bd27248471c32099d04c712a118f 100644 (file)
@@ -1,20 +1,23 @@
-import { Component, OnInit } from '@angular/core'
+import { Component, OnDestroy, OnInit } from '@angular/core'
 import { ActivatedRoute } from '@angular/router'
 import { Account } from '@app/shared/account/account.model'
 import { AccountService } from '@app/shared/account/account.service'
 import { VideoChannel } from '../../../../../shared/models/videos'
 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
 import { flatMap, map, tap } from 'rxjs/operators'
+import { Subscription } from 'rxjs'
 
 @Component({
   selector: 'my-account-video-channels',
   templateUrl: './account-video-channels.component.html',
   styleUrls: [ './account-video-channels.component.scss' ]
 })
-export class AccountVideoChannelsComponent implements OnInit {
+export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
   account: Account
   videoChannels: VideoChannel[] = []
 
+  private accountSub: Subscription
+
   constructor (
     protected route: ActivatedRoute,
     private accountService: AccountService,
@@ -23,7 +26,7 @@ export class AccountVideoChannelsComponent implements OnInit {
 
   ngOnInit () {
     // Parent get the account for us
-    this.accountService.accountLoaded
+    this.accountSub = this.accountService.accountLoaded
         .pipe(
           tap(account => this.account = account),
           flatMap(account => this.videoChannelService.listAccountVideoChannels(account)),
@@ -31,4 +34,8 @@ export class AccountVideoChannelsComponent implements OnInit {
         )
         .subscribe(videoChannels => this.videoChannels = videoChannels)
   }
+
+  ngOnDestroy () {
+    if (this.accountSub) this.accountSub.unsubscribe()
+  }
 }
index 476f040247b43af58e5163da67a097c639bd96ce..5e3dbb6b386921d161aebe2a46e990347f443b53 100644 (file)
@@ -11,6 +11,7 @@ import { Account } from '@app/shared/account/account.model'
 import { AccountService } from '@app/shared/account/account.service'
 import { tap } from 'rxjs/operators'
 import { I18n } from '@ngx-translate/i18n-polyfill'
+import { Subscription } from 'rxjs'
 
 @Component({
   selector: 'my-account-videos',
@@ -27,6 +28,7 @@ export class AccountVideosComponent extends AbstractVideoList implements OnInit,
   loadOnInit = false
 
   private account: Account
+  private accountSub: Subscription
 
   constructor (
     protected router: Router,
@@ -48,17 +50,19 @@ export class AccountVideosComponent extends AbstractVideoList implements OnInit,
     super.ngOnInit()
 
     // Parent get the account for us
-    this.accountService.accountLoaded
+    this.accountSub = this.accountService.accountLoaded
       .subscribe(account => {
         this.account = account
-        this.currentRoute = '/account/' + this.account.id + '/videos'
+        this.currentRoute = '/account/' + this.account.nameWithHost + '/videos'
 
-        this.loadMoreVideos(this.pagination.currentPage)
+        this.reloadVideos()
         this.generateSyndicationList()
       })
   }
 
   ngOnDestroy () {
+    if (this.accountSub) this.accountSub.unsubscribe()
+
     super.ngOnDestroy()
   }
 
index 24bde61ce1ef53701c67e460db0bf5d590ce035f..2a6856c10fbad8dd03c4df5f4be6c2db188d31b5 100644 (file)
@@ -3,7 +3,8 @@ import { ActivatedRoute } from '@angular/router'
 import { AccountService } from '@app/shared/account/account.service'
 import { Account } from '@app/shared/account/account.model'
 import { RestExtractor } from '@app/shared'
-import { catchError } from 'rxjs/operators'
+import { catchError, switchMap, distinctUntilChanged, map } from 'rxjs/operators'
+import { Subscription } from 'rxjs'
 
 @Component({
   templateUrl: './accounts.component.html',
@@ -12,6 +13,8 @@ import { catchError } from 'rxjs/operators'
 export class AccountsComponent implements OnInit {
   account: Account
 
+  private routeSub: Subscription
+
   constructor (
     private route: ActivatedRoute,
     private accountService: AccountService,
@@ -19,10 +22,17 @@ export class AccountsComponent implements OnInit {
   ) {}
 
   ngOnInit () {
-    const accountId = this.route.snapshot.params['accountId']
+    this.routeSub = this.route.params
+      .pipe(
+        map(params => params[ 'accountId' ]),
+        distinctUntilChanged(),
+        switchMap(accountId => this.accountService.getAccount(accountId)),
+        catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
+      )
+      .subscribe(account => this.account = account)
+  }
 
-    this.accountService.getAccount(accountId)
-        .pipe(catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])))
-        .subscribe(account => this.account = account)
+  ngOnDestroy () {
+    if (this.routeSub) this.routeSub.unsubscribe()
   }
 }
index c5fd442c6c4663b0e7e07fcba3e5f05b2d09af56..dc0893962056fdd0023146ddce34a8e5e399a230 100644 (file)
@@ -1,17 +1,20 @@
-import { Component, OnInit } from '@angular/core'
+import { Component, OnDestroy, OnInit } from '@angular/core'
 import { ActivatedRoute } from '@angular/router'
 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
 import { I18n } from '@ngx-translate/i18n-polyfill'
+import { Subscription } from 'rxjs'
 
 @Component({
   selector: 'my-video-channel-about',
   templateUrl: './video-channel-about.component.html',
   styleUrls: [ './video-channel-about.component.scss' ]
 })
-export class VideoChannelAboutComponent implements OnInit {
+export class VideoChannelAboutComponent implements OnInit, OnDestroy {
   videoChannel: VideoChannel
 
+  private videoChannelSub: Subscription
+
   constructor (
     private route: ActivatedRoute,
     private i18n: I18n,
@@ -20,10 +23,14 @@ export class VideoChannelAboutComponent implements OnInit {
 
   ngOnInit () {
     // Parent get the video channel for us
-    this.videoChannelService.videoChannelLoaded
+    this.videoChannelSub = this.videoChannelService.videoChannelLoaded
       .subscribe(videoChannel => this.videoChannel = videoChannel)
   }
 
+  ngOnDestroy () {
+    if (this.videoChannelSub) this.videoChannelSub.unsubscribe()
+  }
+
   getVideoChannelDescription () {
     if (this.videoChannel.description) return this.videoChannel.description
 
index 28c591039fa59ef37dd4c76fed690fb9cc6780b0..2d3f669948fc60c8309da3d1f6066bd787af771b 100644 (file)
@@ -11,6 +11,7 @@ import { VideoChannelService } from '@app/shared/video-channel/video-channel.ser
 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
 import { tap } from 'rxjs/operators'
 import { I18n } from '@ngx-translate/i18n-polyfill'
+import { Subscription } from 'rxjs'
 
 @Component({
   selector: 'my-video-channel-videos',
@@ -27,6 +28,7 @@ export class VideoChannelVideosComponent extends AbstractVideoList implements On
   loadOnInit = false
 
   private videoChannel: VideoChannel
+  private videoChannelSub: Subscription
 
   constructor (
     protected router: Router,
@@ -48,17 +50,19 @@ export class VideoChannelVideosComponent extends AbstractVideoList implements On
     super.ngOnInit()
 
     // Parent get the video channel for us
-    this.videoChannelService.videoChannelLoaded
+    this.videoChannelSub = this.videoChannelService.videoChannelLoaded
       .subscribe(videoChannel => {
         this.videoChannel = videoChannel
         this.currentRoute = '/video-channel/' + this.videoChannel.uuid + '/videos'
 
-        this.loadMoreVideos(this.pagination.currentPage)
+        this.reloadVideos()
         this.generateSyndicationList()
       })
   }
 
   ngOnDestroy () {
+    if (this.videoChannelSub) this.videoChannelSub.unsubscribe()
+
     super.ngOnDestroy()
   }
 
index 09541b370e52febb413ae2853058a40dbb6f22e0..cd04638596b1d4d8dc56f4d8d1b3511853b2d993 100644 (file)
@@ -1,28 +1,39 @@
-import { Component, OnInit } from '@angular/core'
+import { Component, OnInit, OnDestroy } from '@angular/core'
 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'
 import { RestExtractor } from '@app/shared'
-import { catchError } from 'rxjs/operators'
+import { catchError, switchMap, map, distinctUntilChanged } from 'rxjs/operators'
+import { Subscription } from 'rxjs/Subscription'
 
 @Component({
   templateUrl: './video-channels.component.html',
   styleUrls: [ './video-channels.component.scss' ]
 })
-export class VideoChannelsComponent implements OnInit {
+export class VideoChannelsComponent implements OnInit, OnDestroy {
   videoChannel: VideoChannel
 
+  private routeSub: Subscription
+
   constructor (
     private route: ActivatedRoute,
     private videoChannelService: VideoChannelService,
     private restExtractor: RestExtractor
-  ) {}
+  ) { }
 
   ngOnInit () {
-    const videoChannelId = this.route.snapshot.params['videoChannelId']
+    this.routeSub = this.route.params
+                        .pipe(
+                          map(params => params[ 'videoChannelId' ]),
+                          distinctUntilChanged(),
+                          switchMap(videoChannelId => this.videoChannelService.getVideoChannel(videoChannelId)),
+                          catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
+                        )
+                        .subscribe(videoChannel => this.videoChannel = videoChannel)
+
+  }
 
-    this.videoChannelService.getVideoChannel(videoChannelId)
-        .pipe(catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])))
-        .subscribe(videoChannel => this.videoChannel = videoChannel)
+  ngOnDestroy () {
+    if (this.routeSub) this.routeSub.unsubscribe()
   }
 }
index 9a36786b5b9a232bd389c40b53af4d8227dc3d43..68114008766903f2b4f5845625f88ad4630d5963 100644 (file)
@@ -69,7 +69,6 @@ export class MarkdownService {
   }
 
   private avoidTruncatedLinks (html: string) {
-    console.log(html)
     return html.replace(/<a[^>]+>([^<]+)<\/a>\s*...((<\/p>)|(<\/li>)|(<\/strong>))?$/mi, '$1...')
   }
 }