1 import { Component, ElementRef, OnInit } from '@angular/core';
2 import { FormBuilder, FormGroup } from '@angular/forms';
3 import { 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';
15 } from '../../shared';
16 import { VideoService } from '../shared';
19 selector: 'my-videos-add',
20 styleUrls: [ './video-add.component.scss' ],
21 templateUrl: './video-add.component.html'
24 export class VideoAddComponent extends FormReactive implements OnInit {
26 uploader: FileUploader;
37 validationMessages = {
38 name: VIDEO_NAME.MESSAGES,
39 category: VIDEO_CATEGORY.MESSAGES,
40 description: VIDEO_DESCRIPTION.MESSAGES,
41 currentTag: VIDEO_TAGS.MESSAGES
44 // Special error messages
49 private authService: AuthService,
50 private elementRef: ElementRef,
51 private formBuilder: FormBuilder,
52 private router: Router,
53 private notificationsService: NotificationsService,
54 private videoService: VideoService
60 if (this.uploader.queue.length === 0) {
64 return this.uploader.queue[0].file.name;
68 this.form = this.formBuilder.group({
69 name: [ '', VIDEO_NAME.VALIDATORS ],
70 category: [ '', VIDEO_CATEGORY.VALIDATORS ],
71 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
72 currentTag: [ '', VIDEO_TAGS.VALIDATORS ]
75 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
79 this.videoCategories = this.videoService.videoCategories;
81 this.uploader = new FileUploader({
82 authToken: this.authService.getRequestHeaderValue(),
84 url: '/api/v1/videos',
85 removeAfterUpload: true
88 this.uploader.onBuildItemForm = (item, form) => {
89 const name = this.form.value['name'];
90 const category = this.form.value['category'];
91 const description = this.form.value['description'];
93 form.append('name', name);
94 form.append('category', category);
95 form.append('description', description);
97 for (let i = 0; i < this.tags.length; i++) {
98 form.append(`tags[${i}]`, this.tags[i]);
108 if (this.filename === null) {
109 this.fileError = 'You did not add a file.';
112 return this.form.valid === true && this.tagsError === '' && this.fileError === '';
119 onTagKeyPress(event: KeyboardEvent) {
121 if (event.keyCode === 13) {
122 this.addTagIfPossible();
127 this.uploader.clearQueue();
130 removeTag(tag: string) {
131 this.tags.splice(this.tags.indexOf(tag), 1);
132 this.form.get('currentTag').enable();
136 // Maybe the user forgot to press "enter" when he filled the field
137 this.addTagIfPossible();
139 if (this.checkForm() === false) {
143 const item = this.uploader.queue[0];
144 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
145 item.alias = 'videofile';
148 // Run detection change for progress bar
149 const interval = setInterval(() => { ; }, 250);
151 item.onSuccess = () => {
152 clearInterval(interval);
154 console.log('Video uploaded.');
155 this.notificationsService.success('Success', 'Video uploaded.');
158 // Print all the videos once it's finished
159 this.router.navigate(['/videos/list']);
162 item.onError = (response: string, status: number) => {
163 clearInterval(interval);
165 // We need to handle manually these cases beceause we use the FileUpload component
166 if (status === 400) {
167 this.error = response;
168 } else if (status === 401) {
169 this.error = 'Access token was expired, refreshing token...';
170 this.authService.refreshAccessToken().subscribe(
172 // Update the uploader request header
173 this.uploader.authToken = this.authService.getRequestHeaderValue();
174 this.error += ' access token refreshed. Please retry your request.';
178 this.error = 'Unknow error';
179 console.error(this.error);
183 this.uploader.uploadAll();
186 private addTagIfPossible() {
187 const currentTag = this.form.value['currentTag'];
188 if (currentTag === undefined) return;
190 // Check if the tag is valid and does not already exist
192 currentTag.length >= 2 &&
193 this.form.controls['currentTag'].valid &&
194 this.tags.indexOf(currentTag) === -1
196 this.tags.push(currentTag);
197 this.form.patchValue({ currentTag: '' });
199 if (this.tags.length >= 3) {
200 this.form.get('currentTag').disable();