From dc78e203ea11d861421c9baa3d7ecf7fcf2c8cfb Mon Sep 17 00:00:00 2001 From: tracer Date: Fri, 28 Oct 2022 17:02:16 +0200 Subject: [PATCH] added ajax error handling --- public/assets/js/functions.js | 59 ++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/public/assets/js/functions.js b/public/assets/js/functions.js index b8120e2..a82da41 100644 --- a/public/assets/js/functions.js +++ b/public/assets/js/functions.js @@ -30,8 +30,15 @@ function editAddress(id) { .then( response => response.text() ).then( - html => console.log(html) - ); + json => { + let jsonObject = JSON.parse(json) + if (jsonObject.status === 200) { + setInfo('Data successfully saved.') + } else { + setError(jsonObject.message); + } + } + ); document.getElementById('first_' + id).disabled = true document.getElementById('last_' + id).disabled = true @@ -66,8 +73,15 @@ function deleteAddress(id) { .then( response => response.text() ).then( - html => console.log(html) - ); + json => { + let jsonObject = JSON.parse(json) + if (jsonObject.status === 200) { + setInfo('Data successfully saved.') + } else { + setError(jsonObject.message); + } + } + ); let row = document.getElementById('row_' + id) row.parentNode.removeChild(row) } @@ -132,6 +146,43 @@ function sortBy(column) { } } + +function setInfo(info) { + const infoBox = document.getElementById('info_box') + infoBox.innerHTML = info + infoBox.style.display = 'block' + infoBox.classList.add('panel_float') + setTimeout(() => { + infoBox.style.display = 'none' + }, 2500) +} + +function setError(error) { + const errorBox = document.getElementById('error_box') + const errorText = document.getElementById('error_text') + const infoButton = document.getElementById('info_button') + if (errorBox.style.display === 'block') { + errorBox.style.display = 'none' + return + } + if (infoButton != null) { + infoButton.disabled = true + } + errorText.innerHTML = error + errorBox.style.display = 'block' + errorBox.classList.add('panel_float') +} + +function closeError() { + const errorBox = document.getElementById('error_box') + const infoButton = document.getElementById('info_button') + if (infoButton) { + infoButton.disabled = false + } + errorBox.style.display = 'none' + +} + document.addEventListener('DOMContentLoaded', () => { const table = document.getElementById('address_table') || false if (table) {