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