diff --git a/files/assets/js/comments_v.js b/files/assets/js/comments_v.js index 5e11bdcbe..96418c1af 100644 --- a/files/assets/js/comments_v.js +++ b/files/assets/js/comments_v.js @@ -1,44 +1,3 @@ -function post_toast3(t, url, button1, button2) { - t.disabled=true; - t.classList.add("disabled"); - const xhr = new XMLHttpRequest(); - xhr.open("POST", url); - xhr.setRequestHeader('xhr', 'xhr'); - var form = new FormData() - form.append("formkey", formkey()); - - if(typeof data === 'object' && data !== null) { - for(let k of Object.keys(data)) { - form.append(k, data[k]); - } - } - - xhr.onload = function() { - let data - try {data = JSON.parse(xhr.response)} - catch(e) {console.log(e)} - if (xhr.status >= 200 && xhr.status < 300 && data && data["message"]) { - document.getElementById('toast-post-success-text').innerText = data["message"]; - bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')).show(); - - document.getElementById(button1).classList.toggle("d-md-inline-block"); - document.getElementById(button2).classList.toggle("d-md-inline-block"); - - } else { - document.getElementById('toast-post-error-text').innerText = "Error, please try again later." - if (data && data["error"]) document.getElementById('toast-post-error-text').innerText = data["error"]; - if (data && data["details"]) document.getElementById('toast-post-error-text').innerText = data["details"]; - bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')).show(); - } - setTimeout(() => { - t.disabled = false; - t.classList.remove("disabled"); - }, 2000); - }; - - xhr.send(form); -} - function report_commentModal(id, author) { document.getElementById("comment-author").textContent = author; diff --git a/files/assets/js/header.js b/files/assets/js/header.js index 975620308..3aa83eee6 100644 --- a/files/assets/js/header.js +++ b/files/assets/js/header.js @@ -81,45 +81,91 @@ function expandDesktopImage(image) { document.getElementById("desktop-expanded-image-wrap-link").href = image; }; -function post_toast(t, url, reload, data) { - if (t) { +function postToastCallback2(targetElement, url, method, data, callbackFn) { + if (targetElement) { // disable element to avoid repeated requests t.disabled = true; t.classList.add("disabled"); } - const xhr = new XMLHttpRequest(); - xhr.open("POST", url); - xhr.setRequestHeader('xhr', 'xhr'); - var form = new FormData() + const xhr = new XMLHttpRequest(); // set up the request now + xhr.open(method, url); + xhr.setRequestHeader("xhr", "xhr"); + var form = new FormData(); form.append("formkey", formkey()); - if(typeof data === 'object' && data !== null) { - for(let k of Object.keys(data)) { + if (typeof data === 'object' && data !== null) { + for (let k of Object.keys(data)) { form.append(k, data[k]); } } xhr.onload = function() { - let data - try {data = JSON.parse(xhr.response)} - catch(e) {console.log(e)} - if (xhr.status >= 200 && xhr.status < 300 && data && data['message']) { - document.getElementById('toast-post-success-text').innerText = data["message"]; - bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')).show(); - if (reload == 1) {location.reload()} + let data; + try { + data = JSON.parse(xhr.response); + } catch (e) { + console.error("Failed to parse response as JSON", e); + } + + try { + var result = callbackFn(xhr); + } catch (e) { + console.error("Failed to run callback function for postToast", e, xhr); + var result = null; + } + if (typeof result === 'string' && result !== null) { + var messageOverride = result; } else { - document.getElementById('toast-post-error-text').innerText = "Error, please try again later." - if (data && data["error"]) document.getElementById('toast-post-error-text').innerText = data["error"]; - if (data && data["details"]) document.getElementById('toast-post-error-text').innerText = data["details"]; + var messageOverride = null; + } + + if (xhr.status >= 200 && xhr.status < 300 && data && data['message']) { + const toastPostSuccessTextElement = document.getElementById("toast-post-success-text"); + toastPostSuccessTextElement.innerText = data["message"]; + if (messageOverride) toastPostSuccessTextElement.innerText = messageOverride; + bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')).show(); + callbackFn(xhr); + } else { + const toastPostErrorTextElement = document.getElementById('toast-post-error-text'); + toastPostErrorTextElement.innerText = "Error, please try again later." + if (data && data["error"]) toastPostErrorTextElement.innerText = data["error"]; + if (data && data["details"]) toastPostErrorTextElement.innerText = data["details"]; + if (messageOverride) toastPostErrorTextElement.innerText = messageOverride; bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')).show(); } - setTimeout(() => { - t.disabled = false; - t.classList.remove("disabled"); - }, 2000); - }; + } + + setTimeout(() => { + t.disabled = false; + t.classList.remove("disabled"); + }, 1500); xhr.send(form); +} +function post_toast(t, url, reload, data) { + postToastCallback2(t, url, "POST", data, (xhr) => { + if (reload) { + location.reload(); + } + }); +} + +function post_toast2(t, url, button1, button2) { + postToastCallback2(t, url, "POST", null, (xhr) => { + document.getElementById(button1).classList.toggle("d-none"); + document.getElementById(button2).classList.toggle("d-none"); + }); +} + +function post_toast3(t, url, button1, button2) { + postToastCallback2(t, url, "POST", null, (xhr) => { + document.getElementById(button1).classList.toggle("d-md-inline-block"); + document.getElementById(button2).classList.toggle("d-md-inline-block"); + }); +} + +function post_toast_callback(url, data, callback) { + postToastCallback2(null, url, "POST", data, (xhr) => callback(xhr)); } function escapeHTML(unsafe) { diff --git a/files/assets/js/post_toast2.js b/files/assets/js/post_toast2.js deleted file mode 100644 index 1772709ff..000000000 --- a/files/assets/js/post_toast2.js +++ /dev/null @@ -1,43 +0,0 @@ -function post_toast2(t, url, button1, button2) { - t.disabled=true; - t.classList.add("disabled"); - const xhr = new XMLHttpRequest(); - xhr.open("POST", url); - xhr.setRequestHeader('xhr', 'xhr'); - var form = new FormData() - form.append("formkey", formkey()); - - if(typeof data === 'object' && data !== null) { - for(let k of Object.keys(data)) { - form.append(k, data[k]); - } - } - - - form.append("formkey", formkey()); - - xhr.onload = function() { - let data - try {data = JSON.parse(xhr.response)} - catch(e) {console.log(e)} - if (xhr.status >= 200 && xhr.status < 300 && data && data["message"]) { - document.getElementById('toast-post-success-text').innerText = data["message"]; - bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')).show(); - - document.getElementById(button1).classList.toggle("d-none"); - document.getElementById(button2).classList.toggle("d-none"); - - } else { - document.getElementById('toast-post-error-text').innerText = "Error, please try again later." - if (data && data["error"]) document.getElementById('toast-post-error-text').innerText = data["error"]; - if (data && data["details"]) document.getElementById('toast-post-error-text').innerText = data["details"]; - bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')).show(); - } - setTimeout(() => { - t.disabled = false; - t.classList.remove("disabled"); - }, 2000); - }; - - xhr.send(form); -} diff --git a/files/assets/js/userpage_v.js b/files/assets/js/userpage_v.js index de59fb909..4912e9491 100644 --- a/files/assets/js/userpage_v.js +++ b/files/assets/js/userpage_v.js @@ -1,60 +1,3 @@ -function post_toast_callback(url, data, callback) { - const xhr = new XMLHttpRequest(); - xhr.open("POST", url); - xhr.setRequestHeader('xhr', 'xhr'); - var form = new FormData() - form.append("formkey", formkey()); - - if(typeof data === 'object' && data !== null) { - for(let k of Object.keys(data)) { - form.append(k, data[k]); - } - } - - form.append("formkey", formkey()); - xhr.onload = function() { - let result = callback(xhr); - if (xhr.status >= 200 && xhr.status < 300) { - var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')); - myToast.hide(); - - var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')); - myToast.show(); - - try { - if(typeof result == "string") { - document.getElementById('toast-post-success-text').innerText = result; - } else { - document.getElementById('toast-post-success-text').innerText = JSON.parse(xhr.response)["message"]; - } - } catch(e) { - } - - return true; - } else { - var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')); - myToast.hide(); - - var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')); - myToast.show(); - - try { - if(typeof result == "string") { - document.getElementById('toast-post-error-text').innerText = result; - } else { - document.getElementById('toast-post-error-text').innerText = JSON.parse(xhr.response)["error"]; - } - return false - } catch(e) {console.log(e)} - - return false; - } - }; - - xhr.send(form); - -} - function toggleElement(id, id2) { for(let el of document.getElementsByClassName('toggleable')) { if(el.id != id) {