Skip to content
Snippets Groups Projects
Commit 0e756e3f authored by Bekerle, Patrizio's avatar Bekerle, Patrizio :fire: Committed by Reiter, Christoph
Browse files

Improve error handling and allow selecting same files again to upload in file...

Improve error handling and allow selecting same files again to upload in file selector (VPU/Middleware/API#40)
parent d6fe6940
No related branches found
No related tags found
No related merge requests found
...@@ -49,7 +49,7 @@ class VPUFileUpload extends VPULitElement { ...@@ -49,7 +49,7 @@ class VPUFileUpload extends VPULitElement {
super.connectedCallback(); super.connectedCallback();
this.updateComplete.then(() => { this.updateComplete.then(() => {
this.dropArea = this.shadowRoot.querySelector('#dropArea'); this.dropArea = this._('#dropArea');
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => { ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
this.dropArea.addEventListener(eventName, this.preventDefaults, false) this.dropArea.addEventListener(eventName, this.preventDefaults, false)
}); });
...@@ -60,8 +60,7 @@ class VPUFileUpload extends VPULitElement { ...@@ -60,8 +60,7 @@ class VPUFileUpload extends VPULitElement {
this.dropArea.addEventListener(eventName, this.unhighlight.bind(this), false) this.dropArea.addEventListener(eventName, this.unhighlight.bind(this), false)
}); });
this.dropArea.addEventListener('drop', this.handleDrop.bind(this), false); this.dropArea.addEventListener('drop', this.handleDrop.bind(this), false);
this.shadowRoot.querySelector('#fileElem').addEventListener('change', this.handleChange.bind(this)); this._('#fileElem').addEventListener('change', this.handleChange.bind(this));
}); });
} }
...@@ -86,8 +85,17 @@ class VPUFileUpload extends VPULitElement { ...@@ -86,8 +85,17 @@ class VPUFileUpload extends VPULitElement {
this.handleFiles(files); this.handleFiles(files);
} }
handleChange(e) { async handleChange(e) {
this.handleFiles(this.shadowRoot.querySelector('#fileElem').files); let fileElem = this._('#fileElem');
if (fileElem.files.length === 0) {
return;
}
await this.handleFiles(fileElem.files);
// reset the element's value so the user can upload the same file(s) again
fileElem.value = '';
} }
async asyncForEach(array, callback) { async asyncForEach(array, callback) {
...@@ -100,25 +108,32 @@ class VPUFileUpload extends VPULitElement { ...@@ -100,25 +108,32 @@ class VPUFileUpload extends VPULitElement {
console.log('handleFiles: files.length = ' + files.length); console.log('handleFiles: files.length = ' + files.length);
// we need to wait for each upload until we start the next one // we need to wait for each upload until we start the next one
this.asyncForEach(files, async (file) => this.uploadFile(file)); await this.asyncForEach(files, async (file) => this.uploadFile(file));
} }
sendFinishedEvent(response, file, sendFile = false) { async sendFinishedEvent(response, file, sendFile = false) {
response.json().then((json) => { if (response === undefined) {
let data = { return;
status: response.status, }
filename: file.name,
json: json
};
if (sendFile) { let data = {
data.file = file; filename: file.name,
} status: response.status,
json: {"hydra:description": ""}
};
console.log(data); try {
const event = new CustomEvent("vpu-fileupload-finished", { "detail": data, bubbles: true, composed: true }); await response.json().then((json) => {
this.dispatchEvent(event); data.json = json;
}); });
} catch (e) {}
if (sendFile) {
data.file = file;
}
const event = new CustomEvent("vpu-fileupload-finished", { "detail": data, bubbles: true, composed: true });
this.dispatchEvent(event);
} }
/** /**
...@@ -141,7 +156,7 @@ class VPUFileUpload extends VPULitElement { ...@@ -141,7 +156,7 @@ class VPUFileUpload extends VPULitElement {
.then((response) => { .then((response) => {
/* Done. Inform the user */ /* Done. Inform the user */
console.log(`Status: ${response.status} for file ${file.name}`); console.log(`Status: ${response.status} for file ${file.name}`);
this.sendFinishedEvent(response, file, response.status === 503); this.sendFinishedEvent(response, file, response.status !== 201);
}) })
.catch((response) => { .catch((response) => {
/* Error. Inform the user */ /* Error. Inform the user */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment