]>
Commit | Line | Data |
---|---|---|
230809ef | 1 | import { Component, ElementRef, OnInit } from '@angular/core'; |
4b2f33f3 | 2 | import { FormBuilder, FormGroup } from '@angular/forms'; |
00a44645 | 3 | import { Router } from '@angular/router'; |
dc8bc31b | 4 | |
ab32b0fc | 5 | import { FileUploader } from 'ng2-file-upload/ng2-file-upload'; |
7ddd02c9 | 6 | import { NotificationsService } from 'angular2-notifications'; |
8140a704 | 7 | |
693b1aba | 8 | import { AuthService } from '../../core'; |
6e07c3de C |
9 | import { |
10 | FormReactive, | |
11 | VIDEO_NAME, | |
12 | VIDEO_CATEGORY, | |
13 | VIDEO_DESCRIPTION, | |
14 | VIDEO_TAGS | |
15 | } from '../../shared'; | |
16 | import { VideoService } from '../shared'; | |
1553e15d | 17 | |
dc8bc31b C |
18 | @Component({ |
19 | selector: 'my-videos-add', | |
ec8d8440 C |
20 | styleUrls: [ './video-add.component.scss' ], |
21 | templateUrl: './video-add.component.html' | |
dc8bc31b C |
22 | }) |
23 | ||
4b2f33f3 C |
24 | export class VideoAddComponent extends FormReactive implements OnInit { |
25 | tags: string[] = []; | |
e822fdae | 26 | uploader: FileUploader; |
6e07c3de | 27 | videoCategories = []; |
4b2f33f3 C |
28 | |
29 | error: string = null; | |
30 | form: FormGroup; | |
31 | formErrors = { | |
e822fdae | 32 | name: '', |
6e07c3de | 33 | category: '', |
4b2f33f3 C |
34 | description: '', |
35 | currentTag: '' | |
36 | }; | |
37 | validationMessages = { | |
38 | name: VIDEO_NAME.MESSAGES, | |
6e07c3de | 39 | category: VIDEO_CATEGORY.MESSAGES, |
4b2f33f3 C |
40 | description: VIDEO_DESCRIPTION.MESSAGES, |
41 | currentTag: VIDEO_TAGS.MESSAGES | |
e822fdae | 42 | }; |
dc8bc31b | 43 | |
bf57d5ee C |
44 | // Special error messages |
45 | tagsError = ''; | |
46 | fileError = ''; | |
47 | ||
1553e15d | 48 | constructor( |
9bfe96e1 | 49 | private authService: AuthService, |
4fd8aa32 | 50 | private elementRef: ElementRef, |
4b2f33f3 | 51 | private formBuilder: FormBuilder, |
7ddd02c9 | 52 | private router: Router, |
6e07c3de C |
53 | private notificationsService: NotificationsService, |
54 | private videoService: VideoService | |
4b2f33f3 C |
55 | ) { |
56 | super(); | |
57 | } | |
dc8bc31b | 58 | |
e822fdae C |
59 | get filename() { |
60 | if (this.uploader.queue.length === 0) { | |
61 | return null; | |
62 | } | |
63 | ||
64 | return this.uploader.queue[0].file.name; | |
65 | } | |
66 | ||
4b2f33f3 C |
67 | buildForm() { |
68 | this.form = this.formBuilder.group({ | |
69 | name: [ '', VIDEO_NAME.VALIDATORS ], | |
6e07c3de | 70 | category: [ '', VIDEO_CATEGORY.VALIDATORS ], |
4b2f33f3 C |
71 | description: [ '', VIDEO_DESCRIPTION.VALIDATORS ], |
72 | currentTag: [ '', VIDEO_TAGS.VALIDATORS ] | |
73 | }); | |
74 | ||
75 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | |
e822fdae C |
76 | } |
77 | ||
dc8bc31b | 78 | ngOnInit() { |
6e07c3de C |
79 | this.videoCategories = this.videoService.videoCategories; |
80 | ||
e822fdae C |
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) => { | |
4b2f33f3 | 89 | const name = this.form.value['name']; |
6e07c3de | 90 | const category = this.form.value['category']; |
4b2f33f3 C |
91 | const description = this.form.value['description']; |
92 | ||
93 | form.append('name', name); | |
6e07c3de | 94 | form.append('category', category); |
4b2f33f3 | 95 | form.append('description', description); |
e822fdae | 96 | |
4b2f33f3 C |
97 | for (let i = 0; i < this.tags.length; i++) { |
98 | form.append(`tags[${i}]`, this.tags[i]); | |
e822fdae C |
99 | } |
100 | }; | |
4b2f33f3 C |
101 | |
102 | this.buildForm(); | |
e822fdae C |
103 | } |
104 | ||
bf57d5ee C |
105 | checkForm() { |
106 | this.forceCheck(); | |
107 | ||
108 | if (this.tags.length === 0) { | |
109 | this.tagsError = 'You have 0 tags'; | |
110 | } | |
111 | ||
112 | if (this.filename === null) { | |
113 | this.fileError = 'You did not add a file.'; | |
114 | } | |
115 | ||
116 | return this.form.valid === true && this.tagsError === '' && this.fileError === ''; | |
117 | } | |
118 | ||
119 | fileChanged() { | |
120 | this.fileError = ''; | |
121 | } | |
122 | ||
e822fdae | 123 | onTagKeyPress(event: KeyboardEvent) { |
4b2f33f3 C |
124 | const currentTag = this.form.value['currentTag']; |
125 | ||
e822fdae C |
126 | // Enter press |
127 | if (event.keyCode === 13) { | |
128 | // Check if the tag is valid and does not already exist | |
129 | if ( | |
1a005042 | 130 | currentTag.length >= 2 && |
4b2f33f3 C |
131 | this.form.controls['currentTag'].valid && |
132 | this.tags.indexOf(currentTag) === -1 | |
e822fdae | 133 | ) { |
4b2f33f3 C |
134 | this.tags.push(currentTag); |
135 | this.form.patchValue({ currentTag: '' }); | |
136 | ||
137 | if (this.tags.length >= 3) { | |
138 | this.form.get('currentTag').disable(); | |
139 | } | |
bf57d5ee C |
140 | |
141 | this.tagsError = ''; | |
e822fdae C |
142 | } |
143 | } | |
144 | } | |
145 | ||
146 | removeFile() { | |
147 | this.uploader.clearQueue(); | |
148 | } | |
149 | ||
150 | removeTag(tag: string) { | |
4b2f33f3 | 151 | this.tags.splice(this.tags.indexOf(tag), 1); |
46485303 | 152 | this.form.get('currentTag').enable(); |
dc8bc31b C |
153 | } |
154 | ||
e822fdae | 155 | upload() { |
bf57d5ee C |
156 | if (this.checkForm() === false) { |
157 | return; | |
158 | } | |
159 | ||
e822fdae C |
160 | const item = this.uploader.queue[0]; |
161 | // TODO: wait for https://github.com/valor-software/ng2-file-upload/pull/242 | |
162 | item.alias = 'videofile'; | |
163 | ||
1a005042 C |
164 | // FIXME: remove |
165 | // Run detection change for progress bar | |
166 | const interval = setInterval(() => { ; }, 250); | |
167 | ||
e822fdae | 168 | item.onSuccess = () => { |
1a005042 C |
169 | clearInterval(interval); |
170 | ||
e822fdae | 171 | console.log('Video uploaded.'); |
7ddd02c9 C |
172 | this.notificationsService.success('Success', 'Video uploaded.'); |
173 | ||
e822fdae C |
174 | |
175 | // Print all the videos once it's finished | |
c6de16eb | 176 | this.router.navigate(['/videos/list']); |
e822fdae C |
177 | }; |
178 | ||
179 | item.onError = (response: string, status: number) => { | |
1a005042 C |
180 | clearInterval(interval); |
181 | ||
bd5c83a8 C |
182 | // We need to handle manually these cases beceause we use the FileUpload component |
183 | if (status === 400) { | |
184 | this.error = response; | |
185 | } else if (status === 401) { | |
186 | this.error = 'Access token was expired, refreshing token...'; | |
187 | this.authService.refreshAccessToken().subscribe( | |
188 | () => { | |
189 | // Update the uploader request header | |
190 | this.uploader.authToken = this.authService.getRequestHeaderValue(); | |
191 | this.error += ' access token refreshed. Please retry your request.'; | |
192 | } | |
193 | ); | |
194 | } else { | |
195 | this.error = 'Unknow error'; | |
196 | console.error(this.error); | |
197 | } | |
e822fdae C |
198 | }; |
199 | ||
e822fdae | 200 | this.uploader.uploadAll(); |
dc8bc31b C |
201 | } |
202 | } |