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