1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { FormBuilder, FormGroup } from '@angular/forms'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { LoadingBarService } from '@ngx-loading-bar/core'
6 import { NotificationsService } from 'angular2-notifications'
7 import { VideoPrivacy } from '../../../../../shared/models/videos'
8 import { ServerService } from '../../core'
9 import { AuthService } from '../../core/auth'
10 import { FormReactive } from '../../shared'
11 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
12 import { VideoEdit } from '../../shared/video/video-edit.model'
13 import { VideoService } from '../../shared/video/video.service'
14 import { populateAsyncUserVideoChannels } from '@app/shared/misc/utils'
17 selector: 'my-videos-update',
18 styleUrls: [ './shared/video-edit.component.scss' ],
19 templateUrl: './video-update.component.html'
21 export class VideoUpdateComponent extends FormReactive implements OnInit {
24 isUpdatingVideo = false
26 formErrors: { [ id: string ]: string } = {}
27 validationMessages: ValidatorMessage = {}
29 userVideoChannels = []
32 private formBuilder: FormBuilder,
33 private route: ActivatedRoute,
34 private router: Router,
35 private notificationsService: NotificationsService,
36 private serverService: ServerService,
37 private videoService: VideoService,
38 private authService: AuthService,
39 private loadingBar: LoadingBarService
45 this.form = this.formBuilder.group({})
46 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
52 this.serverService.videoPrivaciesLoaded
53 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
55 const uuid: string = this.route.snapshot.params[ 'uuid' ]
56 this.videoService.getVideo(uuid)
59 return this.videoService
60 .loadCompleteDescription(video.descriptionPath)
61 .pipe(map(description => Object.assign(video, { description })))
66 this.video = new VideoEdit(video)
68 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
69 .catch(err => console.error(err))
71 // We cannot set private a video that was not private
72 if (video.privacy.id !== VideoPrivacy.PRIVATE) {
73 const newVideoPrivacies = []
74 for (const p of this.videoPrivacies) {
75 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
78 this.videoPrivacies = newVideoPrivacies
81 this.hydrateFormFromVideo()
86 this.notificationsService.error('Error', err.message)
94 return this.form.valid
98 if (this.checkForm() === false) {
102 this.video.patch(this.form.value)
104 this.loadingBar.start()
105 this.isUpdatingVideo = true
106 this.videoService.updateVideo(this.video)
109 this.isUpdatingVideo = false
110 this.loadingBar.complete()
111 this.notificationsService.success('Success', 'Video updated.')
112 this.router.navigate([ '/videos/watch', this.video.uuid ])
116 this.isUpdatingVideo = false
117 this.notificationsService.error('Error', err.message)
124 private hydrateFormFromVideo () {
125 this.form.patchValue(this.video.toJSON())
130 name: 'thumbnailfile'
138 for (const obj of objects) {
139 fetch(this.video[obj.url])
140 .then(response => response.blob())
142 this.form.patchValue({