]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-video-playlist/video-add-to-playlist.component.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-playlist / video-add-to-playlist.component.ts
index 8b019103c2c41fa4949d596adff4d79b4d09ca9f..f802416a4235a8c637d699f6b36e095c4227d010 100644 (file)
@@ -3,11 +3,11 @@ import { Subject, Subscription } from 'rxjs'
 import { debounceTime, filter } from 'rxjs/operators'
 import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core'
 import { AuthService, DisableForReuseHook, Notifier } from '@app/core'
-import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
+import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
 import { secondsToTime } from '@shared/core-utils'
 import {
+  CachedVideoExistInPlaylist,
   Video,
-  VideoExistInPlaylist,
   VideoPlaylistCreate,
   VideoPlaylistElementCreate,
   VideoPlaylistElementUpdate,
@@ -16,7 +16,7 @@ import {
 import { VIDEO_PLAYLIST_DISPLAY_NAME_VALIDATOR } from '../form-validators/video-playlist-validators'
 import { CachedPlaylist, VideoPlaylistService } from './video-playlist.service'
 
-const logger = debug('peertube:playlists:VideoAddToPlaylistComponent')
+const debugLogger = debug('peertube:playlists:VideoAddToPlaylistComponent')
 
 type PlaylistElement = {
   enabled: boolean
@@ -47,7 +47,7 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
   isNewPlaylistBlockOpened = false
 
   videoPlaylistSearch: string
-  videoPlaylistSearchChanged = new Subject<string>()
+  videoPlaylistSearchChanged = new Subject<void>()
 
   videoPlaylists: PlaylistSummary[] = []
 
@@ -56,8 +56,10 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
   private listenToPlaylistChangeSub: Subscription
   private playlistsData: CachedPlaylist[] = []
 
+  private pendingAddId: number
+
   constructor (
-    protected formValidatorService: FormValidatorService,
+    protected formReactiveService: FormReactiveService,
     private authService: AuthService,
     private notifier: Notifier,
     private videoPlaylistService: VideoPlaylistService,
@@ -79,7 +81,7 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
         .subscribe(result => {
           this.playlistsData = result.data
 
-          this.videoPlaylistService.runPlaylistCheck(this.video.id)
+          this.videoPlaylistService.runVideoExistsInPlaylistCheck(this.video.id)
         })
 
     this.videoPlaylistSearchChanged
@@ -108,7 +110,7 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
   }
 
   reload () {
-    logger('Reloading component')
+    debugLogger('Reloading component')
 
     this.videoPlaylists = []
     this.videoPlaylistSearch = undefined
@@ -119,7 +121,7 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
   }
 
   load () {
-    logger('Loading component')
+    debugLogger('Loading component')
 
     this.listenToVideoPlaylistChange()
 
@@ -127,7 +129,7 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
         .subscribe(playlistsResult => {
           this.playlistsData = playlistsResult.data
 
-          this.videoPlaylistService.runPlaylistCheck(this.video.id)
+          this.videoPlaylistService.runVideoExistsInPlaylistCheck(this.video.id)
         })
   }
 
@@ -191,22 +193,23 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
   }
 
   createPlaylist () {
-    const displayName = this.form.value[ 'displayName' ]
+    const displayName = this.form.value['displayName']
 
     const videoPlaylistCreate: VideoPlaylistCreate = {
       displayName,
       privacy: VideoPlaylistPrivacy.PRIVATE
     }
 
-    this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe(
-      () => {
-        this.isNewPlaylistBlockOpened = false
+    this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate)
+      .subscribe({
+        next: () => {
+          this.isNewPlaylistBlockOpened = false
 
-        this.cd.markForCheck()
-      },
+          this.cd.markForCheck()
+        },
 
-      err => this.notifier.error(err.message)
-    )
+        error: err => this.notifier.error(err.message)
+      })
   }
 
   onVideoPlaylistSearchChanged () {
@@ -214,8 +217,9 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
   }
 
   isPrimaryCheckboxChecked (playlist: PlaylistSummary) {
-    return playlist.elements.filter(e => e.enabled)
-                            .length !== 0
+    // Reduce latency when adding a video to a playlist using pendingAddId
+    return this.pendingAddId === playlist.id ||
+      playlist.elements.filter(e => e.enabled).length !== 0
   }
 
   toggleOptionalRow (playlist: PlaylistSummary) {
@@ -268,17 +272,15 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
     }
 
     this.videoPlaylistService.updateVideoOfPlaylist(playlist.id, element.playlistElementId, body, this.video.id)
-        .subscribe(
-          () => {
+        .subscribe({
+          next: () => {
             this.notifier.success($localize`Timestamps updated`)
           },
 
-          err => {
-            this.notifier.error(err.message)
-          },
+          error: err => this.notifier.error(err.message),
 
-          () => this.cd.markForCheck()
-        )
+          complete: () => this.cd.markForCheck()
+        })
   }
 
   private isOptionalRowDisplayed (playlist: PlaylistSummary) {
@@ -302,17 +304,15 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
 
   private removeVideoFromPlaylist (playlist: PlaylistSummary, elementId: number) {
     this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, elementId, this.video.id)
-        .subscribe(
-          () => {
+        .subscribe({
+          next: () => {
             this.notifier.success($localize`Video removed from ${playlist.displayName}`)
           },
 
-          err => {
-            this.notifier.error(err.message)
-          },
+          error: err => this.notifier.error(err.message),
 
-          () => this.cd.markForCheck()
-        )
+          complete: () => this.cd.markForCheck()
+        })
   }
 
   private listenToVideoPlaylistChange () {
@@ -330,8 +330,8 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
     }
   }
 
-  private rebuildPlaylists (existResult: VideoExistInPlaylist[]) {
-    logger('Got existing results for %d.', this.video.id, existResult)
+  private rebuildPlaylists (existResult: CachedVideoExistInPlaylist[]) {
+    debugLogger('Got existing results for %d.', this.video.id, existResult)
 
     const oldPlaylists = this.videoPlaylists
 
@@ -359,7 +359,7 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
       this.videoPlaylists.push(playlistSummary)
     }
 
-    logger('Rebuilt playlist state for video %d.', this.video.id, this.videoPlaylists)
+    debugLogger('Rebuilt playlist state for video %d.', this.video.id, this.videoPlaylists)
 
     this.cd.markForCheck()
   }
@@ -370,9 +370,11 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
     if (element.startTimestamp) body.startTimestamp = element.startTimestamp
     if (element.stopTimestamp && element.stopTimestamp !== this.video.duration) body.stopTimestamp = element.stopTimestamp
 
+    this.pendingAddId = playlist.id
+
     this.videoPlaylistService.addVideoInPlaylist(playlist.id, body)
-      .subscribe(
-        res => {
+      .subscribe({
+        next: res => {
           const message = body.startTimestamp || body.stopTimestamp
             ? $localize`Video added in ${playlist.displayName} at timestamps ${this.formatTimestamp(element)}`
             : $localize`Video added in ${playlist.displayName}`
@@ -382,12 +384,18 @@ export class VideoAddToPlaylistComponent extends FormReactive implements OnInit,
           if (element) element.playlistElementId = res.videoPlaylistElement.id
         },
 
-        err => {
+        error: err => {
+          this.pendingAddId = undefined
+          this.cd.markForCheck()
+
           this.notifier.error(err.message)
         },
 
-        () => this.cd.markForCheck()
-      )
+        complete: () => {
+          this.pendingAddId = undefined
+          this.cd.markForCheck()
+        }
+      })
   }
 
   private formatTimestamp (element: PlaylistElement) {