From 1ed8d0f9259ccb8d9adc0cf12267043ef351e5ec Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Fri, 12 Feb 2021 13:18:35 +0200 Subject: frontend work --- loggedin.js | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'loggedin.js') diff --git a/loggedin.js b/loggedin.js index 8980586..0db4c7c 100644 --- a/loggedin.js +++ b/loggedin.js @@ -2,10 +2,15 @@ const upload_form = document.getElementById("upload_form"); const the_file = document.getElementById("the_file"); const filename_input = document.getElementById("filename"); const upload_btn = document.getElementById("upload_btn"); +const current_directory = document.getElementById("current_directory"); the_file.onchange = on_file_added; -function on_file_added(e) { +const files = []; + +const pending_uploads = []; + +function on_file_added(_e) { if (the_file.files.length >= 1) { filename_input.value = the_file.files[0].name; @@ -13,9 +18,18 @@ function on_file_added(e) { fetch(upload_form.action, { method: upload_form.method, body: new FormData(upload_form) - }) + }).then((resp) => { + if (resp.status == 200) { + add_file_visuals(filename_input.value, true); + } + else { + alert("Upload failed"); + } + }, () => { + alert("Upload failed") + }); + - alert("Sent the upload request"); } else { alert("No files selected"); @@ -23,6 +37,27 @@ function on_file_added(e) { } +function add_file_visuals(name, pending) { + var fileDiv = document.createElement('div'); + + var img = document.createElement('img'); + var filename = document.createElement('div'); + + img.src="/mimeicons/application-pdf.png"; + fileDiv.classList.add('file'); + filename.classList.add('filename'); + filename.innerText = name; + + fileDiv.appendChild(img); + fileDiv.appendChild(filename); + + current_directory.appendChild(fileDiv); + + files.push([name, fileDiv]); + + return fileDiv; +} + function begin_upload() { the_file.click(); } -- cgit v1.2.3 From 7c28081802008f7e451fed7341a8b5aceb573e65 Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Fri, 12 Feb 2021 13:23:41 +0200 Subject: upload not async for now --- loggedin.js | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'loggedin.js') diff --git a/loggedin.js b/loggedin.js index 0db4c7c..436719f 100644 --- a/loggedin.js +++ b/loggedin.js @@ -1,3 +1,5 @@ +var FORM_ASYNC = false; + const upload_form = document.getElementById("upload_form"); const the_file = document.getElementById("the_file"); const filename_input = document.getElementById("filename"); @@ -14,6 +16,11 @@ function on_file_added(_e) { if (the_file.files.length >= 1) { filename_input.value = the_file.files[0].name; + if (!FORM_ASYNC) { + upload_form.submit(); + return; + } + // Send the form asynchronously through the fetch api fetch(upload_form.action, { method: upload_form.method, -- cgit v1.2.3