Switching to a named filters/single input on video-abuse
[oweals/peertube.git] / client / src / app / +my-account / my-account-video-playlists / my-account-video-playlist-update.component.ts
index 167d7dd094bae4ee1ea77f9e4bd5121f8e8dbb80..2f85cdd963f62f17fc832ee40d8e67fe4777d308 100644 (file)
@@ -1,7 +1,7 @@
 import { Component, OnDestroy, OnInit } from '@angular/core'
 import { ActivatedRoute, Router } from '@angular/router'
 import { AuthService, Notifier, ServerService } from '@app/core'
-import { Subscription } from 'rxjs'
+import { forkJoin, Subscription } from 'rxjs'
 import { I18n } from '@ngx-translate/i18n-polyfill'
 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
 import { MyAccountVideoPlaylistEdit } from '@app/+my-account/my-account-video-playlists/my-account-video-playlist-edit'
@@ -9,9 +9,8 @@ import { populateAsyncUserVideoChannels } from '@app/shared/misc/utils'
 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
 import { VideoPlaylistValidatorsService } from '@app/shared'
 import { VideoPlaylistUpdate } from '@shared/models/videos/playlist/video-playlist-update.model'
-import { VideoConstant } from '@shared/models'
-import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model'
 import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
+import { delayWhen, map, switchMap } from 'rxjs/operators'
 
 @Component({
   selector: 'my-account-video-playlist-update',
@@ -21,7 +20,6 @@ import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
 export class MyAccountVideoPlaylistUpdateComponent extends MyAccountVideoPlaylistEdit implements OnInit, OnDestroy {
   error: string
   videoPlaylistToUpdate: VideoPlaylist
-  videoPlaylistPrivacies: VideoConstant<VideoPlaylistPrivacy>[] = []
 
   private paramsSub: Subscription
 
@@ -41,39 +39,40 @@ export class MyAccountVideoPlaylistUpdateComponent extends MyAccountVideoPlaylis
 
   ngOnInit () {
     this.buildForm({
-      'display-name': this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME,
+      displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME,
       privacy: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_PRIVACY,
       description: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DESCRIPTION,
       videoChannelId: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_CHANNEL_ID,
       thumbnailfile: null
     })
 
-    populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
-
-    this.paramsSub = this.route.params.subscribe(routeParams => {
-      const videoPlaylistId = routeParams['videoPlaylistId']
-
-      this.videoPlaylistService.getVideoPlaylist(videoPlaylistId).subscribe(
-        videoPlaylistToUpdate => {
-          this.videoPlaylistToUpdate = videoPlaylistToUpdate
-
-          this.hydrateFormFromPlaylist()
-
-          this.serverService.videoPlaylistPrivaciesLoaded.subscribe(
-            () => {
-              this.videoPlaylistPrivacies = this.serverService.getVideoPlaylistPrivacies()
-                .filter(p => {
-                  // If the playlist is not private, we cannot put it in private anymore
-                  return this.videoPlaylistToUpdate.privacy.id === VideoPlaylistPrivacy.PRIVATE ||
-                    p.id !== VideoPlaylistPrivacy.PRIVATE
-                })
-            }
-          )
-        },
-
-        err => this.error = err.message
-      )
+    this.form.get('privacy').valueChanges.subscribe(privacy => {
+      this.videoPlaylistValidatorsService.setChannelValidator(this.form.get('videoChannelId'), privacy)
     })
+
+    populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
+      .catch(err => console.error('Cannot populate user video channels.', err))
+
+    this.paramsSub = this.route.params
+                         .pipe(
+                           map(routeParams => routeParams['videoPlaylistId']),
+                           switchMap(videoPlaylistId => {
+                             return forkJoin([
+                               this.videoPlaylistService.getVideoPlaylist(videoPlaylistId),
+                               this.serverService.getVideoPlaylistPrivacies()
+                             ])
+                           })
+                         )
+                         .subscribe(
+                           ([ videoPlaylistToUpdate, videoPlaylistPrivacies]) => {
+                             this.videoPlaylistToUpdate = videoPlaylistToUpdate
+                             this.videoPlaylistPrivacies = videoPlaylistPrivacies
+
+                             this.hydrateFormFromPlaylist()
+                           },
+
+                           err => this.error = err.message
+                         )
   }
 
   ngOnDestroy () {
@@ -85,8 +84,8 @@ export class MyAccountVideoPlaylistUpdateComponent extends MyAccountVideoPlaylis
 
     const body = this.form.value
     const videoPlaylistUpdate: VideoPlaylistUpdate = {
-      displayName: body['display-name'],
-      privacy: body['privacy'],
+      displayName: body.displayName,
+      privacy: body.privacy,
       description: body.description || null,
       videoChannelId: body.videoChannelId || null,
       thumbnailfile: body.thumbnailfile || undefined
@@ -115,7 +114,7 @@ export class MyAccountVideoPlaylistUpdateComponent extends MyAccountVideoPlaylis
 
   private hydrateFormFromPlaylist () {
     this.form.patchValue({
-      'display-name': this.videoPlaylistToUpdate.displayName,
+      displayName: this.videoPlaylistToUpdate.displayName,
       privacy: this.videoPlaylistToUpdate.privacy.id,
       description: this.videoPlaylistToUpdate.description,
       videoChannelId: this.videoPlaylistToUpdate.videoChannel ? this.videoPlaylistToUpdate.videoChannel.id : null