1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
5 import { NotificationsService } from 'angular2-notifications'
7 import { ServerService } from '../../core'
17 import { Video, VideoService } from '../shared'
20 selector: 'my-videos-update',
21 styleUrls: [ './video-edit.component.scss' ],
22 templateUrl: './video-update.component.html'
25 export class VideoUpdateComponent extends FormReactive implements OnInit {
32 tagValidators = VIDEO_TAGS.VALIDATORS
33 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
44 validationMessages = {
45 name: VIDEO_NAME.MESSAGES,
46 category: VIDEO_CATEGORY.MESSAGES,
47 licence: VIDEO_LICENCE.MESSAGES,
48 language: VIDEO_LANGUAGE.MESSAGES,
49 description: VIDEO_DESCRIPTION.MESSAGES
55 private formBuilder: FormBuilder,
56 private route: ActivatedRoute,
57 private router: Router,
58 private notificationsService: NotificationsService,
59 private serverService: ServerService,
60 private videoService: VideoService
66 this.form = this.formBuilder.group({
67 name: [ '', VIDEO_NAME.VALIDATORS ],
69 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
70 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
71 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
72 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
76 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
82 this.videoCategories = this.serverService.getVideoCategories()
83 this.videoLicences = this.serverService.getVideoLicences()
84 this.videoLanguages = this.serverService.getVideoLanguages()
86 const uuid: string = this.route.snapshot.params['uuid']
87 this.videoService.getVideo(uuid)
92 this.hydrateFormFromVideo()
97 this.error = 'Cannot fetch video.'
105 return this.form.valid
109 if (this.checkForm() === false) {
113 this.video.patch(this.form.value)
115 this.videoService.updateVideo(this.video)
118 this.notificationsService.success('Success', 'Video updated.')
119 this.router.navigate([ '/videos/watch', this.video.uuid ])
123 this.error = 'Cannot update the video.'
130 private hydrateFormFromVideo () {
131 this.form.patchValue(this.video.toJSON())