1 import { forkJoin, Subscription } from 'rxjs'
2 import { map, switchMap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { AuthService, Notifier, ServerService } from '@app/core'
6 import { populateAsyncUserVideoChannels } from '@app/helpers'
7 import { FormValidatorService, VideoPlaylistValidatorsService } from '@app/shared/shared-forms'
8 import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { VideoPlaylistUpdate } from '@shared/models'
11 import { MyAccountVideoPlaylistEdit } from './my-account-video-playlist-edit'
14 selector: 'my-account-video-playlist-update',
15 templateUrl: './my-account-video-playlist-edit.component.html',
16 styleUrls: [ './my-account-video-playlist-edit.component.scss' ]
18 export class MyAccountVideoPlaylistUpdateComponent extends MyAccountVideoPlaylistEdit implements OnInit, OnDestroy {
20 videoPlaylistToUpdate: VideoPlaylist
22 private paramsSub: Subscription
25 protected formValidatorService: FormValidatorService,
26 private authService: AuthService,
27 private videoPlaylistValidatorsService: VideoPlaylistValidatorsService,
28 private notifier: Notifier,
29 private router: Router,
30 private route: ActivatedRoute,
31 private videoPlaylistService: VideoPlaylistService,
33 private serverService: ServerService
40 displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME,
41 privacy: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_PRIVACY,
42 description: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DESCRIPTION,
43 videoChannelId: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_CHANNEL_ID,
47 this.form.get('privacy').valueChanges.subscribe(privacy => {
48 this.videoPlaylistValidatorsService.setChannelValidator(this.form.get('videoChannelId'), privacy)
51 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
52 .catch(err => console.error('Cannot populate user video channels.', err))
54 this.paramsSub = this.route.params
56 map(routeParams => routeParams['videoPlaylistId']),
57 switchMap(videoPlaylistId => {
59 this.videoPlaylistService.getVideoPlaylist(videoPlaylistId),
60 this.serverService.getVideoPlaylistPrivacies()
65 ([ videoPlaylistToUpdate, videoPlaylistPrivacies]) => {
66 this.videoPlaylistToUpdate = videoPlaylistToUpdate
67 this.videoPlaylistPrivacies = videoPlaylistPrivacies
69 this.hydrateFormFromPlaylist()
72 err => this.error = err.message
77 if (this.paramsSub) this.paramsSub.unsubscribe()
81 this.error = undefined
83 const body = this.form.value
84 const videoPlaylistUpdate: VideoPlaylistUpdate = {
85 displayName: body.displayName,
86 privacy: body.privacy,
87 description: body.description || null,
88 videoChannelId: body.videoChannelId || null,
89 thumbnailfile: body.thumbnailfile || undefined
92 this.videoPlaylistService.updateVideoPlaylist(this.videoPlaylistToUpdate, videoPlaylistUpdate).subscribe(
94 this.notifier.success(
95 this.i18n('Playlist {{videoPlaylistName}} updated.', { videoPlaylistName: videoPlaylistUpdate.displayName })
98 this.router.navigate([ '/my-account', 'video-playlists' ])
101 err => this.error = err.message
109 getFormButtonTitle () {
110 return this.i18n('Update')
113 private hydrateFormFromPlaylist () {
114 this.form.patchValue({
115 displayName: this.videoPlaylistToUpdate.displayName,
116 privacy: this.videoPlaylistToUpdate.privacy.id,
117 description: this.videoPlaylistToUpdate.description,
118 videoChannelId: this.videoPlaylistToUpdate.videoChannel ? this.videoPlaylistToUpdate.videoChannel.id : null
121 fetch(this.videoPlaylistToUpdate.thumbnailUrl)
122 .then(response => response.blob())
124 this.form.patchValue({