1 import { Component, ElementRef, 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 { AuthService } 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 authService: AuthService,
56 private elementRef: ElementRef,
57 private formBuilder: FormBuilder,
58 private route: ActivatedRoute,
59 private router: Router,
60 private notificationsService: NotificationsService,
61 private videoService: VideoService
67 this.form = this.formBuilder.group({
68 name: [ '', VIDEO_NAME.VALIDATORS ],
70 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
71 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
72 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
73 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
77 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
83 this.videoCategories = this.videoService.videoCategories
84 this.videoLicences = this.videoService.videoLicences
85 this.videoLanguages = this.videoService.videoLanguages
87 const uuid: string = this.route.snapshot.params['uuid']
88 this.videoService.getVideo(uuid)
93 this.hydrateFormFromVideo()
98 this.error = 'Cannot fetch video.'
106 return this.form.valid
110 if (this.checkForm() === false) {
114 this.video.patch(this.form.value)
116 this.videoService.updateVideo(this.video)
119 this.notificationsService.success('Success', 'Video updated.')
120 this.router.navigate([ '/videos/watch', this.video.uuid ])
124 this.error = 'Cannot update the video.'
131 private hydrateFormFromVideo () {
132 this.form.patchValue(this.video.toJSON())