1 import { Component, ElementRef, OnInit } from '@angular/core';
2 import { FormControl, FormGroup, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
5 import { FileUploader } from 'ng2-file-upload/ng2-file-upload';
7 import { AuthService } from '../../shared';
10 selector: 'my-videos-add',
11 styles: [ require('./video-add.component.scss') ],
12 template: require('./video-add.component.html')
15 export class VideoAddComponent implements OnInit {
16 currentTag: string; // Tag the user is writing in the input
19 uploader: FileUploader;
27 private authService: AuthService,
28 private elementRef: ElementRef,
29 private router: Router
33 if (this.uploader.queue.length === 0) {
37 return this.uploader.queue[0].file.name;
40 get isTagsInputDisabled () {
41 return this.video.tags.length >= 3;
44 getInvalidFieldsTitle() {
46 const nameControl = this.videoForm.controls['name'];
47 const descriptionControl = this.videoForm.controls['description'];
49 if (!nameControl.valid) {
50 title += 'A name is required\n';
53 if (this.video.tags.length === 0) {
54 title += 'At least one tag is required\n';
57 if (this.filename === null) {
58 title += 'A file is required\n';
61 if (!descriptionControl.valid) {
62 title += 'A description is required\n';
69 this.videoForm = new FormGroup({
70 name: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(50) ]),
71 description: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(250) ]),
72 tags: new FormControl('', <any>Validators.pattern('^[a-zA-Z0-9]{2,10}$'))
76 this.uploader = new FileUploader({
77 authToken: this.authService.getRequestHeaderValue(),
79 url: '/api/v1/videos',
80 removeAfterUpload: true
83 this.uploader.onBuildItemForm = (item, form) => {
84 form.append('name', this.video.name);
85 form.append('description', this.video.description);
87 for (let i = 0; i < this.video.tags.length; i++) {
88 form.append(`tags[${i}]`, this.video.tags[i]);
93 onTagKeyPress(event: KeyboardEvent) {
95 if (event.keyCode === 13) {
96 // Check if the tag is valid and does not already exist
98 this.currentTag !== '' &&
99 this.videoForm.controls['tags'].valid &&
100 this.video.tags.indexOf(this.currentTag) === -1
102 this.video.tags.push(this.currentTag);
103 this.currentTag = '';
109 this.uploader.clearQueue();
112 removeTag(tag: string) {
113 this.video.tags.splice(this.video.tags.indexOf(tag), 1);
117 const item = this.uploader.queue[0];
118 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
119 item.alias = 'videofile';
121 item.onSuccess = () => {
122 console.log('Video uploaded.');
124 // Print all the videos once it's finished
125 this.router.navigate(['/videos/list']);
128 item.onError = (response: string, status: number) => {
129 // We need to handle manually these cases beceause we use the FileUpload component
130 if (status === 400) {
131 this.error = response;
132 } else if (status === 401) {
133 this.error = 'Access token was expired, refreshing token...';
134 this.authService.refreshAccessToken().subscribe(
136 // Update the uploader request header
137 this.uploader.authToken = this.authService.getRequestHeaderValue();
138 this.error += ' access token refreshed. Please retry your request.';
142 this.error = 'Unknow error';
143 console.error(this.error);
148 this.uploader.uploadAll();