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
34 private authService: AuthService,
35 private elementRef: ElementRef,
36 private formBuilder: FormBuilder,
37 private router: Router
43 if (this.uploader.queue.length === 0) {
47 return this.uploader.queue[0].file.name;
51 this.form = this.formBuilder.group({
52 name: [ '', VIDEO_NAME.VALIDATORS ],
53 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
54 currentTag: [ '', VIDEO_TAGS.VALIDATORS ]
57 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
60 getInvalidFieldsTitle() {
62 const nameControl = this.form.controls['name'];
63 const descriptionControl = this.form.controls['description'];
65 if (!nameControl.valid) {
66 title += 'A name is required\n';
69 if (this.tags.length === 0) {
70 title += 'At least one tag is required\n';
73 if (this.filename === null) {
74 title += 'A file is required\n';
77 if (!descriptionControl.valid) {
78 title += 'A description is required\n';
85 this.uploader = new FileUploader({
86 authToken: this.authService.getRequestHeaderValue(),
88 url: '/api/v1/videos',
89 removeAfterUpload: true
92 this.uploader.onBuildItemForm = (item, form) => {
93 const name = this.form.value['name'];
94 const description = this.form.value['description'];
96 form.append('name', name);
97 form.append('description', description);
99 for (let i = 0; i < this.tags.length; i++) {
100 form.append(`tags[${i}]`, this.tags[i]);
107 onTagKeyPress(event: KeyboardEvent) {
108 const currentTag = this.form.value['currentTag'];
111 if (event.keyCode === 13) {
112 // Check if the tag is valid and does not already exist
114 currentTag.length >= 2 &&
115 this.form.controls['currentTag'].valid &&
116 this.tags.indexOf(currentTag) === -1
118 this.tags.push(currentTag);
119 this.form.patchValue({ currentTag: '' });
121 if (this.tags.length >= 3) {
122 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 const item = this.uploader.queue[0];
139 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
140 item.alias = 'videofile';
143 // Run detection change for progress bar
144 const interval = setInterval(() => { ; }, 250);
146 item.onSuccess = () => {
147 clearInterval(interval);
149 console.log('Video uploaded.');
151 // Print all the videos once it's finished
152 this.router.navigate(['/videos/list']);
155 item.onError = (response: string, status: number) => {
156 clearInterval(interval);
158 // We need to handle manually these cases beceause we use the FileUpload component
159 if (status === 400) {
160 this.error = response;
161 } else if (status === 401) {
162 this.error = 'Access token was expired, refreshing token...';
163 this.authService.refreshAccessToken().subscribe(
165 // Update the uploader request header
166 this.uploader.authToken = this.authService.getRequestHeaderValue();
167 this.error += ' access token refreshed. Please retry your request.';
171 this.error = 'Unknow error';
172 console.error(this.error);
176 this.uploader.uploadAll();