From 5042e48e5cb6b9163116e91b248c35684919daf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Ko=C5=82ecki?= Date: Wed, 1 Mar 2023 16:12:08 +0100 Subject: [PATCH] Changed minimize function to backend endpoint. --- Frontend/assets/scripts/tools/json.js | 35 ++++++++++++++++++--------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/Frontend/assets/scripts/tools/json.js b/Frontend/assets/scripts/tools/json.js index 1ce3473..04e2c55 100644 --- a/Frontend/assets/scripts/tools/json.js +++ b/Frontend/assets/scripts/tools/json.js @@ -20,33 +20,44 @@ function formatAndValidateJson(errorElement) { input.innerText = data; processInfo.innerText = ""; hljs.highlightElement(input); + + const end = new Date(); + processInfo.innerHTML = "Validation and formatting time: " + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; }) .catch((error) => { processInfo.innerHTML = "" + error + ""; console.error('Error:', error); }); - - const end = new Date(); - processInfo.innerHTML = "Validation and formatting time: " + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; } function minimizeJson(errorElement) { const input = document.querySelector('#jsonBlock'); const processInfo = document.getElementById(errorElement); - try { - const start = new Date(); + const start = new Date(); + const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/minimize" - const obj = JSON.parse(input.textContent); - input.innerHTML = JSON.stringify(obj); + fetch(address, { + method: 'POST', + body: input.textContent + }) + .then(async (response) => { + if (!response.ok) { + throw Error(await response.text()); + } + + return response.text(); + }) + .then((data) => { + input.innerText = data; processInfo.innerText = ""; hljs.highlightElement(input); const end = new Date(); - processInfo.innerHTML = "Validation and formatting time: " - + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; - } catch (error) { + processInfo.innerHTML = "Validation and formatting time: " + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; + }) + .catch((error) => { processInfo.innerHTML = "" + error + ""; - console.error("Error: ", error) - } + console.error('Error:', error); + }); } \ No newline at end of file