1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import 'rxjs/add/observable/forkJoin'
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 { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
13 import { VideoEdit } from '../../shared/video/video-edit.model'
14 import { VideoService } from '../../shared/video/video.service'
17 selector: 'my-videos-update',
18 styleUrls: [ './shared/video-edit.component.scss' ],
19 templateUrl: './video-update.component.html'
22 export class VideoUpdateComponent extends FormReactive implements OnInit {
25 isUpdatingVideo = false
27 formErrors: { [ id: string ]: string } = {}
28 validationMessages: ValidatorMessage = {}
30 userVideoChannels = []
33 private formBuilder: FormBuilder,
34 private route: ActivatedRoute,
35 private router: Router,
36 private notificationsService: NotificationsService,
37 private serverService: ServerService,
38 private videoService: VideoService,
39 private authService: AuthService,
40 private loadingBar: LoadingBarService
46 this.form = this.formBuilder.group({})
47 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
53 this.serverService.videoPrivaciesLoaded
54 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
56 const uuid: string = this.route.snapshot.params['uuid']
57 this.videoService.getVideo(uuid)
59 return this.videoService
60 .loadCompleteDescription(video.descriptionPath)
61 .map(description => Object.assign(video, { description }))
65 this.video = new VideoEdit(video)
67 this.userVideoChannels = [
70 label: video.channel.displayName
74 // We cannot set private a video that was not private
75 if (video.privacy !== VideoPrivacy.PRIVATE) {
76 const newVideoPrivacies = []
77 for (const p of this.videoPrivacies) {
78 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
81 this.videoPrivacies = newVideoPrivacies
84 this.hydrateFormFromVideo()
89 this.notificationsService.error('Error', err.message)
97 return this.form.valid
101 if (this.checkForm() === false) {
105 this.video.patch(this.form.value)
107 this.loadingBar.start()
108 this.isUpdatingVideo = true
109 this.videoService.updateVideo(this.video)
112 this.isUpdatingVideo = false
113 this.loadingBar.complete()
114 this.notificationsService.success('Success', 'Video updated.')
115 this.router.navigate([ '/videos/watch', this.video.uuid ])
119 this.isUpdatingVideo = false
120 this.notificationsService.error('Error', err.message)
127 private hydrateFormFromVideo () {
128 this.form.patchValue(this.video.toJSON())
133 name: 'thumbnailfile'
141 for (const obj of objects) {
142 fetch(this.video[obj.url])
143 .then(response => response.blob())
145 this.form.patchValue({