From b38b4726d9102320f47ab5775891a330402f84e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Thu, 25 Apr 2024 22:08:02 +0200 Subject: [PATCH] Adlerka SMP FTW --- assets/script.js | 34 +++++++++++++++++++++++++++------- endpoints/meme.php | 2 +- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/assets/script.js b/assets/script.js index fd12195..8e353a2 100644 --- a/assets/script.js +++ b/assets/script.js @@ -553,11 +553,9 @@ function uploadFile() { xhr.open('POST', '/upload', true); xhr.onload = function () { - if (xhr.status === 200) { - document.getElementById('uploadStatus').innerHTML = 'File uploaded successfully.'; - } else { - document.getElementById('uploadStatus').innerHTML = 'Error uploading file.'; - } + const respData = JSON.parse(xhr.responseText); + handleResponse(resp, "Súbor bol úspešne nahraný", "Nastala chyba pri nahrávaní súboru"); + fileInput.reset(); }; xhr.send(formData); @@ -584,10 +582,32 @@ async function listFiles() { if (xhr.status === 200) { const resp = JSON.parse(xhr.responseText); if (resp.Status == "Success") { - displayList(resp['Files'], "filelist", deleteFile); + displayList(resp.Files, "filelist", deleteFile); } } - } + }; + xhr.send(formData); +} + +function addMeme() { + let formData = new FormData(); + 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"); + memeTitleElement.reset(); + memeTextElement.reset(); + memeImageElement.reset(); + }; xhr.send(formData); } \ No newline at end of file diff --git a/endpoints/meme.php b/endpoints/meme.php index 82a0d5e..c9d1cb7 100644 --- a/endpoints/meme.php +++ b/endpoints/meme.php @@ -6,7 +6,7 @@ function endpoint($endpoint_data): array { return match ($endpoint_data["action"]) { - "addMeme" => addMeme($endpoint_data['memetitle'], $endpoint_data['memetext'], $endpoint_data['imageid']), + "addMeme" => addMeme($endpoint_data['meme_title'], $endpoint_data['meme_text'], $endpoint_data['meme_image_id']), "renderGallery" => renderMemeGallery(), default => ["Status" => "Fail", "message" => "Invalid action"], };