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, FormReactive, VIDEO_NAME, VIDEO_DESCRIPTION, VIDEO_TAGS } from '../../shared';
10 selector: 'my-videos-add',
11 styleUrls: [ './video-add.component.scss' ],
12 templateUrl: './video-add.component.html'
15 export class VideoAddComponent extends FormReactive implements OnInit {
17 uploader: FileUploader;
26 validationMessages = {
27 name: VIDEO_NAME.MESSAGES,
28 description: VIDEO_DESCRIPTION.MESSAGES,
29 currentTag: VIDEO_TAGS.MESSAGES
33 private authService: AuthService,
34 private elementRef: ElementRef,
35 private formBuilder: FormBuilder,
36 private router: Router
42 if (this.uploader.queue.length === 0) {
46 return this.uploader.queue[0].file.name;
50 this.form = this.formBuilder.group({
51 name: [ '', VIDEO_NAME.VALIDATORS ],
52 description: [ '', VIDEO_DESCRIPTION.VALIDATORS ],
53 currentTag: [ '', VIDEO_TAGS.VALIDATORS ]
56 this.form.valueChanges.subscribe(data => this.onValueChanged(data));
59 getInvalidFieldsTitle() {
61 const nameControl = this.form.controls['name'];
62 const descriptionControl = this.form.controls['description'];
64 if (!nameControl.valid) {
65 title += 'A name is required\n';
68 if (this.tags.length === 0) {
69 title += 'At least one tag is required\n';
72 if (this.filename === null) {
73 title += 'A file is required\n';
76 if (!descriptionControl.valid) {
77 title += 'A description is required\n';
84 this.uploader = new FileUploader({
85 authToken: this.authService.getRequestHeaderValue(),
87 url: '/api/v1/videos',
88 removeAfterUpload: true
91 this.uploader.onBuildItemForm = (item, form) => {
92 const name = this.form.value['name'];
93 const description = this.form.value['description'];
95 form.append('name', name);
96 form.append('description', description);
98 for (let i = 0; i < this.tags.length; i++) {
99 form.append(`tags[${i}]`, this.tags[i]);
106 onTagKeyPress(event: KeyboardEvent) {
107 const currentTag = this.form.value['currentTag'];
110 if (event.keyCode === 13) {
111 // Check if the tag is valid and does not already exist
113 currentTag.length >= 2 &&
114 this.form.controls['currentTag'].valid &&
115 this.tags.indexOf(currentTag) === -1
117 this.tags.push(currentTag);
118 this.form.patchValue({ currentTag: '' });
120 if (this.tags.length >= 3) {
121 this.form.get('currentTag').disable();
128 this.uploader.clearQueue();
131 removeTag(tag: string) {
132 this.tags.splice(this.tags.indexOf(tag), 1);
133 this.form.get('currentTag').enable();
137 const item = this.uploader.queue[0];
138 // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242
139 item.alias = 'videofile';
142 // Run detection change for progress bar
143 const interval = setInterval(() => { ; }, 250);
145 item.onSuccess = () => {
146 clearInterval(interval);
148 console.log('Video uploaded.');
150 // Print all the videos once it's finished
151 this.router.navigate(['/videos/list']);
154 item.onError = (response: string, status: number) => {
155 clearInterval(interval);
157 // We need to handle manually these cases beceause we use the FileUpload component
158 if (status === 400) {
159 this.error = response;
160 } else if (status === 401) {
161 this.error = 'Access token was expired, refreshing token...';
162 this.authService.refreshAccessToken().subscribe(
164 // Update the uploader request header
165 this.uploader.authToken = this.authService.getRequestHeaderValue();
166 this.error += ' access token refreshed. Please retry your request.';
170 this.error = 'Unknow error';
171 console.error(this.error);
175 this.uploader.uploadAll();