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';
17 } from '../../shared';
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();
97 err => this.error = 'Cannot fetch video.'
104 return this.form.valid;
108 if (this.checkForm() === false) {
112 this.video.patch(this.form.value);
114 this.videoService.updateVideo(this.video)
117 this.notificationsService.success('Success', 'Video updated.');
118 this.router.navigate([ '/videos/watch', this.video.id ]);
122 this.error = 'Cannot update the video.';
129 private hydrateFormFromVideo() {
130 this.form.patchValue(this.video.toJSON());