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.tags.length === 0) {
109 this.tagsError = 'You have 0 tags';
112 if (this.filename === null) {
113 this.fileError = 'You did not add a file.';
116 return this.form.valid === true && this.tagsError === '' && this.fileError === '';
123 onTagKeyPress(event: KeyboardEvent) {
124 const currentTag = this.form.value['currentTag'];
127 if (event.keyCode === 13) {
128 // Check if the tag is valid and does not already exist
130 currentTag.length >= 2 &&
131 this.form.controls['currentTag'].valid &&
132 this.tags.indexOf(currentTag) === -1
134 this.tags.push(currentTag);
135 this.form.patchValue({ currentTag: '' });
137 if (this.tags.length >= 3) {
138 this.form.get('currentTag').disable();
147 this.uploader.clearQueue();
150 removeTag(tag: string) {
151 this.tags.splice(this.tags.indexOf(tag), 1);
152 this.form.get('currentTag').enable();
156 if (this.checkForm() === false) {
160 const item = this.uploader.queue[0];
161 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
162 item.alias = 'videofile';
165 // Run detection change for progress bar
166 const interval = setInterval(() => { ; }, 250);
168 item.onSuccess = () => {
169 clearInterval(interval);
171 console.log('Video uploaded.');
172 this.notificationsService.success('Success', 'Video uploaded.');
175 // Print all the videos once it's finished
176 this.router.navigate(['/videos/list']);
179 item.onError = (response: string, status: number) => {
180 clearInterval(interval);
182 // We need to handle manually these cases beceause we use the FileUpload component
183 if (status === 400) {
184 this.error = response;
185 } else if (status === 401) {
186 this.error = 'Access token was expired, refreshing token...';
187 this.authService.refreshAccessToken().subscribe(
189 // Update the uploader request header
190 this.uploader.authToken = this.authService.getRequestHeaderValue();
191 this.error += ' access token refreshed. Please retry your request.';
195 this.error = 'Unknow error';
196 console.error(this.error);
200 this.uploader.uploadAll();