]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, OnInit } from '@angular/core'; | |
2 | import { FormBuilder, FormGroup } from '@angular/forms'; | |
3 | import { Router } from '@angular/router'; | |
4 | ||
5 | import { FileUploader } from 'ng2-file-upload/ng2-file-upload'; | |
6 | ||
7 | import { AuthService, FormReactive, VIDEO_NAME, VIDEO_DESCRIPTION, VIDEO_TAGS } from '../../shared'; | |
8 | ||
9 | @Component({ | |
10 | selector: 'my-videos-add', | |
11 | styleUrls: [ './video-add.component.scss' ], | |
12 | templateUrl: './video-add.component.html' | |
13 | }) | |
14 | ||
15 | export class VideoAddComponent extends FormReactive implements OnInit { | |
16 | tags: string[] = []; | |
17 | uploader: FileUploader; | |
18 | ||
19 | error: string = null; | |
20 | form: FormGroup; | |
21 | formErrors = { | |
22 | name: '', | |
23 | description: '', | |
24 | currentTag: '' | |
25 | }; | |
26 | validationMessages = { | |
27 | name: VIDEO_NAME.MESSAGES, | |
28 | description: VIDEO_DESCRIPTION.MESSAGES, | |
29 | currentTag: VIDEO_TAGS.MESSAGES | |
30 | }; | |
31 | ||
32 | constructor( | |
33 | private authService: AuthService, | |
34 | private elementRef: ElementRef, | |
35 | private formBuilder: FormBuilder, | |
36 | private router: Router | |
37 | ) { | |
38 | super(); | |
39 | } | |
40 | ||
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 | ||
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)); | |
57 | } | |
58 | ||
59 | getInvalidFieldsTitle() { | |
60 | let title = ''; | |
61 | const nameControl = this.form.controls['name']; | |
62 | const descriptionControl = this.form.controls['description']; | |
63 | ||
64 | if (!nameControl.valid) { | |
65 | title += 'A name is required\n'; | |
66 | } | |
67 | ||
68 | if (this.tags.length === 0) { | |
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 | ||
83 | ngOnInit() { | |
84 | this.uploader = new FileUploader({ | |
85 | authToken: this.authService.getRequestHeaderValue(), | |
86 | queueLimit: 1, | |
87 | url: '/api/v1/videos', | |
88 | removeAfterUpload: true | |
89 | }); | |
90 | ||
91 | this.uploader.onBuildItemForm = (item, form) => { | |
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); | |
97 | ||
98 | for (let i = 0; i < this.tags.length; i++) { | |
99 | form.append(`tags[${i}]`, this.tags[i]); | |
100 | } | |
101 | }; | |
102 | ||
103 | this.buildForm(); | |
104 | } | |
105 | ||
106 | onTagKeyPress(event: KeyboardEvent) { | |
107 | const currentTag = this.form.value['currentTag']; | |
108 | ||
109 | // Enter press | |
110 | if (event.keyCode === 13) { | |
111 | // Check if the tag is valid and does not already exist | |
112 | if ( | |
113 | currentTag.length >= 2 && | |
114 | this.form.controls['currentTag'].valid && | |
115 | this.tags.indexOf(currentTag) === -1 | |
116 | ) { | |
117 | this.tags.push(currentTag); | |
118 | this.form.patchValue({ currentTag: '' }); | |
119 | ||
120 | if (this.tags.length >= 3) { | |
121 | this.form.get('currentTag').disable(); | |
122 | } | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | removeFile() { | |
128 | this.uploader.clearQueue(); | |
129 | } | |
130 | ||
131 | removeTag(tag: string) { | |
132 | this.tags.splice(this.tags.indexOf(tag), 1); | |
133 | this.form.get('currentTag').enable(); | |
134 | } | |
135 | ||
136 | upload() { | |
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'; | |
140 | ||
141 | // FIXME: remove | |
142 | // Run detection change for progress bar | |
143 | const interval = setInterval(() => { ; }, 250); | |
144 | ||
145 | item.onSuccess = () => { | |
146 | clearInterval(interval); | |
147 | ||
148 | console.log('Video uploaded.'); | |
149 | ||
150 | // Print all the videos once it's finished | |
151 | this.router.navigate(['/videos/list']); | |
152 | }; | |
153 | ||
154 | item.onError = (response: string, status: number) => { | |
155 | clearInterval(interval); | |
156 | ||
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( | |
163 | () => { | |
164 | // Update the uploader request header | |
165 | this.uploader.authToken = this.authService.getRequestHeaderValue(); | |
166 | this.error += ' access token refreshed. Please retry your request.'; | |
167 | } | |
168 | ); | |
169 | } else { | |
170 | this.error = 'Unknow error'; | |
171 | console.error(this.error); | |
172 | } | |
173 | }; | |
174 | ||
175 | this.uploader.uploadAll(); | |
176 | } | |
177 | } |