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/video-privacy.enum'
7 import { ServerService } from '../../core'
8 import { FormReactive } from '../../shared'
9 import { ValidatorMessage } from '../../shared/forms/form-validators'
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 = {}
31 private formBuilder: FormBuilder,
32 private route: ActivatedRoute,
33 private router: Router,
34 private notificationsService: NotificationsService,
35 private serverService: ServerService,
36 private videoService: VideoService
42 this.form = this.formBuilder.group({})
43 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
49 this.videoPrivacies = this.serverService.getVideoPrivacies()
51 const uuid: string = this.route.snapshot.params['uuid']
52 this.videoService.getVideo(uuid)
54 return this.videoService
55 .loadCompleteDescription(video.descriptionPath)
56 .do(description => video.description = description)
61 this.video = new VideoEdit(video)
63 // We cannot set private a video that was not private
64 if (video.privacy !== VideoPrivacy.PRIVATE) {
65 const newVideoPrivacies = []
66 for (const p of this.videoPrivacies) {
67 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
70 this.videoPrivacies = newVideoPrivacies
73 this.hydrateFormFromVideo()
78 this.error = 'Cannot fetch video.'
86 return this.form.valid
90 if (this.checkForm() === false) {
94 this.video.patch(this.form.value)
96 this.videoService.updateVideo(this.video)
99 this.notificationsService.success('Success', 'Video updated.')
100 this.router.navigate([ '/videos/watch', this.video.uuid ])
104 this.error = 'Cannot update the video.'
111 private hydrateFormFromVideo () {
112 this.form.patchValue(this.video.toJSON())