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';
7 import { AuthService } from '../../core';
8 import { FormReactive, VIDEO_NAME, VIDEO_DESCRIPTION, VIDEO_TAGS } from '../../shared';
11 selector: 'my-videos-add',
12 styleUrls: [ './video-add.component.scss' ],
13 templateUrl: './video-add.component.html'
16 export class VideoAddComponent extends FormReactive implements OnInit {
18 uploader: FileUploader;
27 validationMessages = {
28 name: VIDEO_NAME.MESSAGES,
29 description: VIDEO_DESCRIPTION.MESSAGES,
30 currentTag: VIDEO_TAGS.MESSAGES
33 // Special error messages
38 private authService: AuthService,
39 private elementRef: ElementRef,
40 private formBuilder: FormBuilder,
41 private router: Router
47 if (this.uploader.queue.length === 0) {
51 return this.uploader.queue[0].file.name;
55 this.form = this.formBuilder.group({
56 name: [ '', VIDEO_NAME.VALIDATORS ],
57 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
58 currentTag: [ '', VIDEO_TAGS.VALIDATORS ]
61 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
65 this.uploader = new FileUploader({
66 authToken: this.authService.getRequestHeaderValue(),
68 url: '/api/v1/videos',
69 removeAfterUpload: true
72 this.uploader.onBuildItemForm = (item, form) => {
73 const name = this.form.value['name'];
74 const description = this.form.value['description'];
76 form.append('name', name);
77 form.append('description', description);
79 for (let i = 0; i < this.tags.length; i++) {
80 form.append(`tags[${i}]`, this.tags[i]);
90 if (this.tags.length === 0) {
91 this.tagsError = 'You have 0 tags';
94 if (this.filename === null) {
95 this.fileError = 'You did not add a file.';
98 return this.form.valid === true && this.tagsError === '' && this.fileError === '';
105 onTagKeyPress(event: KeyboardEvent) {
106 const currentTag = this.form.value['currentTag'];
109 if (event.keyCode === 13) {
110 // Check if the tag is valid and does not already exist
112 currentTag.length >= 2 &&
113 this.form.controls['currentTag'].valid &&
114 this.tags.indexOf(currentTag) === -1
116 this.tags.push(currentTag);
117 this.form.patchValue({ currentTag: '' });
119 if (this.tags.length >= 3) {
120 this.form.get('currentTag').disable();
129 this.uploader.clearQueue();
132 removeTag(tag: string) {
133 this.tags.splice(this.tags.indexOf(tag), 1);
134 this.form.get('currentTag').enable();
138 if (this.checkForm() === false) {
142 const item = this.uploader.queue[0];
143 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
144 item.alias = 'videofile';
147 // Run detection change for progress bar
148 const interval = setInterval(() => { ; }, 250);
150 item.onSuccess = () => {
151 clearInterval(interval);
153 console.log('Video uploaded.');
155 // Print all the videos once it's finished
156 this.router.navigate(['/videos/list']);
159 item.onError = (response: string, status: number) => {
160 clearInterval(interval);
162 // We need to handle manually these cases beceause we use the FileUpload component
163 if (status === 400) {
164 this.error = response;
165 } else if (status === 401) {
166 this.error = 'Access token was expired, refreshing token...';
167 this.authService.refreshAccessToken().subscribe(
169 // Update the uploader request header
170 this.uploader.authToken = this.authService.getRequestHeaderValue();
171 this.error += ' access token refreshed. Please retry your request.';
175 this.error = 'Unknow error';
176 console.error(this.error);
180 this.uploader.uploadAll();