]>
Commit | Line | Data |
---|---|---|
0f6da32b | 1 | import { Validators } from '@angular/common'; |
230809ef | 2 | import { Component, ElementRef, OnInit } from '@angular/core'; |
0f6da32b | 3 | import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; |
00a44645 | 4 | import { Router } from '@angular/router'; |
dc8bc31b | 5 | |
8140a704 | 6 | import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe'; |
41a2aee3 | 7 | import { PROGRESSBAR_DIRECTIVES } from 'ng2-bootstrap/components/progressbar'; |
e822fdae | 8 | import { FileSelectDirective, FileUploader } from 'ng2-file-upload/ng2-file-upload'; |
8140a704 | 9 | |
e822fdae | 10 | import { AuthService } from '../../shared'; |
1553e15d | 11 | |
dc8bc31b C |
12 | @Component({ |
13 | selector: 'my-videos-add', | |
4a6995be C |
14 | styles: [ require('./video-add.component.scss') ], |
15 | template: require('./video-add.component.html'), | |
0f6da32b | 16 | directives: [ FileSelectDirective, PROGRESSBAR_DIRECTIVES, REACTIVE_FORM_DIRECTIVES ], |
8140a704 | 17 | pipes: [ BytesPipe ] |
dc8bc31b C |
18 | }) |
19 | ||
41a2aee3 | 20 | export class VideoAddComponent implements OnInit { |
e822fdae | 21 | currentTag: string; // Tag the user is writing in the input |
1cdb5c0f | 22 | error: string = null; |
0f6da32b | 23 | videoForm: FormGroup; |
e822fdae C |
24 | uploader: FileUploader; |
25 | video = { | |
26 | name: '', | |
27 | tags: [], | |
28 | description: '' | |
29 | }; | |
dc8bc31b | 30 | |
1553e15d | 31 | constructor( |
9bfe96e1 | 32 | private authService: AuthService, |
4fd8aa32 | 33 | private elementRef: ElementRef, |
9bfe96e1 | 34 | private router: Router |
1553e15d | 35 | ) {} |
dc8bc31b | 36 | |
e822fdae C |
37 | get filename() { |
38 | if (this.uploader.queue.length === 0) { | |
39 | return null; | |
40 | } | |
41 | ||
42 | return this.uploader.queue[0].file.name; | |
43 | } | |
44 | ||
45 | get isTagsInputDisabled () { | |
46 | return this.video.tags.length >= 3; | |
47 | } | |
48 | ||
49 | getInvalidFieldsTitle() { | |
50 | let title = ''; | |
51 | const nameControl = this.videoForm.controls['name']; | |
52 | const descriptionControl = this.videoForm.controls['description']; | |
53 | ||
54 | if (!nameControl.valid) { | |
55 | title += 'A name is required\n'; | |
56 | } | |
57 | ||
58 | if (this.video.tags.length === 0) { | |
59 | title += 'At least one tag is required\n'; | |
60 | } | |
61 | ||
62 | if (this.filename === null) { | |
63 | title += 'A file is required\n'; | |
64 | } | |
65 | ||
66 | if (!descriptionControl.valid) { | |
67 | title += 'A description is required\n'; | |
68 | } | |
69 | ||
70 | return title; | |
71 | } | |
72 | ||
dc8bc31b | 73 | ngOnInit() { |
0f6da32b C |
74 | this.videoForm = new FormGroup({ |
75 | name: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(50) ]), | |
76 | description: new FormControl('', [ <any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(250) ]), | |
77 | tags: new FormControl('', <any>Validators.pattern('^[a-zA-Z0-9]{2,10}$')) | |
e822fdae C |
78 | }); |
79 | ||
80 | ||
81 | this.uploader = new FileUploader({ | |
82 | authToken: this.authService.getRequestHeaderValue(), | |
83 | queueLimit: 1, | |
98b01bac | 84 | url: '/api/v1/videos', |
e822fdae | 85 | removeAfterUpload: true |
dc8bc31b | 86 | }); |
e822fdae C |
87 | |
88 | this.uploader.onBuildItemForm = (item, form) => { | |
89 | form.append('name', this.video.name); | |
90 | form.append('description', this.video.description); | |
91 | ||
92 | for (let i = 0; i < this.video.tags.length; i++) { | |
93 | form.append(`tags[${i}]`, this.video.tags[i]); | |
94 | } | |
95 | }; | |
96 | } | |
97 | ||
98 | onTagKeyPress(event: KeyboardEvent) { | |
99 | // Enter press | |
100 | if (event.keyCode === 13) { | |
101 | // Check if the tag is valid and does not already exist | |
102 | if ( | |
103 | this.currentTag !== '' && | |
104 | this.videoForm.controls['tags'].valid && | |
105 | this.video.tags.indexOf(this.currentTag) === -1 | |
106 | ) { | |
107 | this.video.tags.push(this.currentTag); | |
108 | this.currentTag = ''; | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
113 | removeFile() { | |
114 | this.uploader.clearQueue(); | |
115 | } | |
116 | ||
117 | removeTag(tag: string) { | |
118 | this.video.tags.splice(this.video.tags.indexOf(tag), 1); | |
dc8bc31b C |
119 | } |
120 | ||
e822fdae C |
121 | upload() { |
122 | const item = this.uploader.queue[0]; | |
123 | // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242 | |
124 | item.alias = 'videofile'; | |
125 | ||
126 | item.onSuccess = () => { | |
127 | console.log('Video uploaded.'); | |
128 | ||
129 | // Print all the videos once it's finished | |
c6de16eb | 130 | this.router.navigate(['/videos/list']); |
e822fdae C |
131 | }; |
132 | ||
133 | item.onError = (response: string, status: number) => { | |
bd5c83a8 C |
134 | // We need to handle manually these cases beceause we use the FileUpload component |
135 | if (status === 400) { | |
136 | this.error = response; | |
137 | } else if (status === 401) { | |
138 | this.error = 'Access token was expired, refreshing token...'; | |
139 | this.authService.refreshAccessToken().subscribe( | |
140 | () => { | |
141 | // Update the uploader request header | |
142 | this.uploader.authToken = this.authService.getRequestHeaderValue(); | |
143 | this.error += ' access token refreshed. Please retry your request.'; | |
144 | } | |
145 | ); | |
146 | } else { | |
147 | this.error = 'Unknow error'; | |
148 | console.error(this.error); | |
149 | } | |
e822fdae C |
150 | }; |
151 | ||
152 | ||
153 | this.uploader.uploadAll(); | |
dc8bc31b C |
154 | } |
155 | } |