From ec4df02b65782511bfc3332d46097bf9b35ac2bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Fri, 26 Apr 2024 14:55:45 +0200 Subject: [PATCH] Rewrite some stuff --- assets/script.js | 104 +++++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 71 deletions(-) diff --git a/assets/script.js b/assets/script.js index 9d4c322..a51f549 100644 --- a/assets/script.js +++ b/assets/script.js @@ -547,56 +547,33 @@ if ("loading" === document.readyState) { setTimeout(initAjax, 0); } -function uploadFile() { +async function uploadFile() { const fileInput = document.getElementById('fileInput'); - const file = fileInput.files[0]; - let formData = new FormData(); - formData.append('userfile', file); - formData.append('action', 'uploadFiles'); - - let xhr = new XMLHttpRequest(); - xhr.open('POST', '/upload', true); - - xhr.onload = function () { - const respData = JSON.parse(xhr.responseText); - handleResponse(respData, "Súbor bol úspešne nahraný", "Nastala chyba pri nahrávaní súboru").then(() => { - fileInput.reset(); - }) + let data = { + action: "uploadFiles" }; - - xhr.send(formData); + let i = 1; + fileInput.files.forEach((file) => { + data[`userFile${i}`] = file; + }); + await doAction("/upload", data, "Súbor bol úspešne nahraný", "Nastala chyba pri nahrávaní súboru", false); + fileInput.reset(); } -function deleteFile(fileID) { - let formData = new FormData(); - formData.append('action', 'deleteFile'); - formData.append('file_id', fileID); - - let xhr = new XMLHttpRequest(); - xhr.open('POST', '/upload', true); - xhr.onload = function () { - // noinspection JSIgnoredPromiseFromCall - listFiles(); - }; - xhr.send(formData); +async function deleteFile(fileID) { + await doAction("/upload", { + action: "deleteFile", + file_id: fileID + }, "Súbor bol úspešne zmazaný", "Nastala chyba pri mazaní súboru", true); + await listFiles(); } async function getFileList() { - let formData = new FormData(); - formData.append('action', 'getAllFiles'); + const resp = await doAction("/upload", { + action: "getAllFiles" + }, "Zoznam súborov bol úspešne stiahnutý", "Nastala chyba pri sťahovaní zoznamu súborov", true); - const response = await fetch('/upload', { - method: 'POST', - body: formData - }); - - if (!response.ok) { - return false; - } - - const resp = await response.json(); - - if (resp.Status === "Success") { + if ("Success" === resp.Status) { return resp.Files; } else { return false; @@ -610,42 +587,27 @@ async function listFiles() { } } -function addMeme() { - let formData = new FormData(); +async function addMeme() { let memeTitleElement = document.getElementById("meme_title_input"); let memeTextElement = document.getElementById("meme_text_input"); let memeImageElement = document.getElementById("meme_image_input"); - formData.append('action', 'addMeme'); - formData.append('meme_title', memeTitleElement.value); - formData.append('meme_text', memeTextElement.value); - formData.append('meme_image_id', memeImageElement.value); - let xhr = new XMLHttpRequest(); - xhr.open('POST', '/meme', true); - xhr.onload = function () { - const resp = JSON.parse(xhr.responseText); - handleResponse(resp, "Meme bol pridaný", "Nastala chyba pri pridávaní meme-u").then(() => { - memeTitleElement.reset(); - memeTextElement.reset(); - memeImageElement.reset(); - }); - }; - xhr.send(formData); + await doAction("/meme", { + action: "addMeme", + meme_title: memeTitleElement.value, + meme_text: memeTextElement.value, + meme_image_id: memeImageElement.value + }, "Meme bol zmazaný", "Nastala chyba pri mazaní meme-u", false); + memeTitleElement.reset(); + memeTextElement.reset(); + memeImageElement.reset(); } async function deleteMeme(memeId) { - let formData = new FormData(); - formData.append('action', 'deleteMeme'); - formData.append('meme_id', memeId); - - let xhr = new XMLHttpRequest(); - xhr.open('POST', '/meme', true); - xhr.onload = function () { - const resp = JSON.parse(xhr.responseText); - handleResponse(resp, "Meme bol zmazaný", "Nastala chyba pri mazaní meme-u"); - softReload(); - }; - xhr.send(formData); + await doAction("/meme", { + action: "deleteMeme", + meme_id: memeId + }, "Meme bol zmazaný", "Nastala chyba pri mazaní meme-u", false); } async function getMemeImages() {