1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { NotificationsService } from 'angular2-notifications'
5 import 'rxjs/add/observable/forkJoin'
6 import { VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { AuthService } from '../../core/auth'
9 import { FormReactive } from '../../shared'
10 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
11 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
12 import { VideoEdit } from '../../shared/video/video-edit.model'
13 import { VideoService } from '../../shared/video/video.service'
16 selector: 'my-videos-update',
17 styleUrls: [ './shared/video-edit.component.scss' ],
18 templateUrl: './video-update.component.html'
21 export class VideoUpdateComponent extends FormReactive implements OnInit {
25 formErrors: { [ id: string ]: string } = {}
26 validationMessages: ValidatorMessage = {}
28 userVideoChannels = []
31 private formBuilder: FormBuilder,
32 private route: ActivatedRoute,
33 private router: Router,
34 private notificationsService: NotificationsService,
35 private serverService: ServerService,
36 private videoService: VideoService,
37 private authService: AuthService
43 this.form = this.formBuilder.group({})
44 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
50 this.serverService.videoPrivaciesLoaded
52 () => this.videoPrivacies = this.serverService.getVideoPrivacies()
55 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
57 const uuid: string = this.route.snapshot.params['uuid']
58 this.videoService.getVideo(uuid)
60 return this.videoService
61 .loadCompleteDescription(video.descriptionPath)
62 .do(description => video.description = description)
67 this.video = new VideoEdit(video)
69 // We cannot set private a video that was not private
70 if (video.privacy !== VideoPrivacy.PRIVATE) {
71 const newVideoPrivacies = []
72 for (const p of this.videoPrivacies) {
73 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
76 this.videoPrivacies = newVideoPrivacies
79 this.hydrateFormFromVideo()
84 this.notificationsService.error('Error', err.message)
92 return this.form.valid
96 if (this.checkForm() === false) {
100 this.video.patch(this.form.value)
102 this.videoService.updateVideo(this.video)
105 this.notificationsService.success('Success', 'Video updated.')
106 this.router.navigate([ '/videos/watch', this.video.uuid ])
110 this.notificationsService.error('Error', err.message)
117 private hydrateFormFromVideo () {
118 this.form.patchValue(this.video.toJSON())