1 import { Component, ElementRef, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { ActivatedRoute, Router } from '@angular/router'
5 import { FileUploader } from 'ng2-file-upload/ng2-file-upload'
6 import { NotificationsService } from 'angular2-notifications'
8 import { AuthService } from '../../core'
18 import { Video, VideoService } from '../shared'
21 selector: 'my-videos-update',
22 styleUrls: [ './video-edit.component.scss' ],
23 templateUrl: './video-update.component.html'
26 export class VideoUpdateComponent extends FormReactive implements OnInit {
33 tagValidators = VIDEO_TAGS.VALIDATORS
34 tagValidatorsMessages = VIDEO_TAGS.MESSAGES
45 validationMessages = {
46 name: VIDEO_NAME.MESSAGES,
47 category: VIDEO_CATEGORY.MESSAGES,
48 licence: VIDEO_LICENCE.MESSAGES,
49 language: VIDEO_LANGUAGE.MESSAGES,
50 description: VIDEO_DESCRIPTION.MESSAGES
56 private authService: AuthService,
57 private elementRef: ElementRef,
58 private formBuilder: FormBuilder,
59 private route: ActivatedRoute,
60 private router: Router,
61 private notificationsService: NotificationsService,
62 private videoService: VideoService
68 this.form = this.formBuilder.group({
69 name: [ '', VIDEO_NAME.VALIDATORS ],
71 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
72 licence: [ '', VIDEO_LICENCE.VALIDATORS ],
73 language: [ '', VIDEO_LANGUAGE.VALIDATORS ],
74 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
78 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
84 this.videoCategories = this.videoService.videoCategories
85 this.videoLicences = this.videoService.videoLicences
86 this.videoLanguages = this.videoService.videoLanguages
88 const id = this.route.snapshot.params['id']
89 this.videoService.getVideo(id)
94 this.hydrateFormFromVideo()
99 this.error = 'Cannot fetch video.'
107 return this.form.valid
111 if (this.checkForm() === false) {
115 this.video.patch(this.form.value)
117 this.videoService.updateVideo(this.video)
120 this.notificationsService.success('Success', 'Video updated.')
121 this.router.navigate([ '/videos/watch', this.video.id ])
125 this.error = 'Cannot update the video.'
132 private hydrateFormFromVideo () {
133 this.form.patchValue(this.video.toJSON())