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 { FormReactive } from '../../shared'
9 import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
10 import { VideoEdit } from '../../shared/video/video-edit.model'
11 import { VideoService } from '../../shared/video/video.service'
14 selector: 'my-videos-update',
15 styleUrls: [ './shared/video-edit.component.scss' ],
16 templateUrl: './video-update.component.html'
19 export class VideoUpdateComponent extends FormReactive implements OnInit {
24 formErrors: { [ id: string ]: string } = {}
25 validationMessages: ValidatorMessage = {}
29 private formBuilder: FormBuilder,
30 private route: ActivatedRoute,
31 private router: Router,
32 private notificationsService: NotificationsService,
33 private serverService: ServerService,
34 private videoService: VideoService
40 this.form = this.formBuilder.group({})
41 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
47 this.videoPrivacies = this.serverService.getVideoPrivacies()
49 const uuid: string = this.route.snapshot.params['uuid']
50 this.videoService.getVideo(uuid)
52 return this.videoService
53 .loadCompleteDescription(video.descriptionPath)
54 .do(description => video.description = description)
59 this.video = new VideoEdit(video)
61 // We cannot set private a video that was not private
62 if (video.privacy !== VideoPrivacy.PRIVATE) {
63 const newVideoPrivacies = []
64 for (const p of this.videoPrivacies) {
65 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
68 this.videoPrivacies = newVideoPrivacies
71 this.hydrateFormFromVideo()
76 this.error = 'Cannot fetch video.'
84 return this.form.valid
88 if (this.checkForm() === false) {
92 this.video.patch(this.form.value)
94 this.videoService.updateVideo(this.video)
97 this.notificationsService.success('Success', 'Video updated.')
98 this.router.navigate([ '/videos/watch', this.video.uuid ])
102 this.error = 'Cannot update the video.'
109 private hydrateFormFromVideo () {
110 this.form.patchValue(this.video.toJSON())