1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import 'rxjs/add/observable/forkJoin'
6 import { NotificationsService } from 'angular2-notifications'
8 import { ServerService } from '../../core'
19 import { VideoEdit, VideoService } from '../shared'
20 import { VideoPrivacy } from '../../../../../shared/models/videos/video-privacy.enum'
23 selector: 'my-videos-update',
24 styleUrls: [ './shared/video-edit.component.scss' ],
25 templateUrl: './video-update.component.html'
28 export class VideoUpdateComponent extends FormReactive implements OnInit {
36 tagValidators = VIDEO_TAGS.VALIDATORS
37 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
49 validationMessages = {
50 name: VIDEO_NAME.MESSAGES,
51 privacy: VIDEO_PRIVACY.MESSAGES,
52 category: VIDEO_CATEGORY.MESSAGES,
53 licence: VIDEO_LICENCE.MESSAGES,
54 language: VIDEO_LANGUAGE.MESSAGES,
55 description: VIDEO_DESCRIPTION.MESSAGES
61 private formBuilder: FormBuilder,
62 private route: ActivatedRoute,
63 private router: Router,
64 private notificationsService: NotificationsService,
65 private serverService: ServerService,
66 private videoService: VideoService
72 this.form = this.formBuilder.group({
73 name: [ '', VIDEO_NAME.VALIDATORS ],
74 privacy: [ '', VIDEO_PRIVACY.VALIDATORS ],
76 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
77 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
78 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
79 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
83 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
89 this.videoCategories = this.serverService.getVideoCategories()
90 this.videoLicences = this.serverService.getVideoLicences()
91 this.videoLanguages = this.serverService.getVideoLanguages()
92 this.videoPrivacies = this.serverService.getVideoPrivacies()
94 const uuid: string = this.route.snapshot.params['uuid']
96 this.videoService.getVideo(uuid)
98 return this.videoService
99 .loadCompleteDescription(video.descriptionPath)
100 .do(description => video.description = description)
105 this.video = new VideoEdit(video)
107 // We cannot set private a video that was not private anymore
108 if (video.privacy !== VideoPrivacy.PRIVATE) {
109 const newVideoPrivacies = []
110 for (const p of this.videoPrivacies) {
111 if (p.id !== VideoPrivacy.PRIVATE) newVideoPrivacies.push(p)
114 this.videoPrivacies = newVideoPrivacies
117 this.hydrateFormFromVideo()
122 this.error = 'Cannot fetch video.'
130 return this.form.valid
134 if (this.checkForm() === false) {
138 this.video.patch(this.form.value)
140 this.videoService.updateVideo(this.video)
143 this.notificationsService.success('Success', 'Video updated.')
144 this.router.navigate([ '/videos/watch', this.video.uuid ])
148 this.error = 'Cannot update the video.'
155 private hydrateFormFromVideo () {
156 this.form.patchValue(this.video.toJSON())