refactor of post_toast

master
justcool393 2022-10-14 01:19:09 -07:00
parent 2559794135
commit b06ee9ac9b
1 changed files with 62 additions and 43 deletions

View File

@ -1,32 +1,38 @@
function post_toast(t, url, button1, button2, classname, extra_actions) {
let isShopConfirm = t.id.startsWith('buy1-') || t.id.startsWith('buy2-');
/*const bootstrap = require("./bootstrap");*/
function isShopConfirmation(t) {
return t.id.startsWith('buy1-') || t.id.startsWith('buy2-');
}
function prePostToastNonShopActions(t, url, button1, button2, className) {
let isShopConfirm = isShopConfirmation(t);
if (!isShopConfirm)
{
t.disabled = true;
t.classList.add("disabled");
}
const xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader('xhr', 'xhr');
const form = new FormData()
form.append("formkey", formkey());
}
function showToast(success, message) {
let element = success ? "toast-post-success" : "toast-post-error"
if (!message) {
message = success ? "Success" : "Error, please try again later";
}
document.getElementById(element + "-text").innerText = message;
bootstrap.Toast.getOrCreateInstance(document.getElementById(element)).show();
}
xhr.onload = function() {
function postToastLoad(xhr, className, extraActionsOnSuccess, extraActionsOnError) {
let data
try {
data = JSON.parse(xhr.response)
}
catch(e) {
catch (e) {
console.log(e)
}
if (xhr.status >= 200 && xhr.status < 300) {
statusMsg = "Success!"
if (data && data['message']) {
document.getElementById('toast-post-success-text').innerText = data["message"];
}
bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')).show();
showToast(true, data && data["message"] ? data["message"] : "Success!");
if (button1)
{
if (typeof(button1) == 'boolean')
@ -36,13 +42,17 @@ function post_toast(t, url, button1, button2, classname, extra_actions) {
document.getElementById(button2).classList.toggle(classname);
}
}
if (extra_actions) extra_actions(xhr);
if (extraActionsOnSuccess) extraActionsOnSuccess(xhr);
} 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();
let message = data && data["error"] ? data["error"] : "Error, please try again later"
if (data && data["details"]) message = data["details"];
showToast(true, message);
if (extraActionsOnError) extraActionsOnError(xhr);
}
}
function postPostToastNonShopActions(t, url, button1, button2, className) {
let isShopConfirm = isShopConfirmation(t);
if (!isShopConfirm)
{
setTimeout(() => {
@ -50,10 +60,21 @@ function post_toast(t, url, button1, button2, classname, extra_actions) {
t.classList.remove("disabled");
}, 2000);
}
}
function post_toast(t, url, button1, button2, classname, extra_actions, extra_actions_error) {
prePostToastNonShopActions(t, url, button1, button2, classname)
const xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader('xhr', 'xhr');
const form = new FormData()
form.append("formkey", formkey());
xhr.onload = function() {
postToastLoad(xhr, classname, extra_actions, extra_actions_error)
postPostToastNonShopActions(t, url, button1, button2, classname)
};
xhr.send(form);
}
function post_toast_callback(url, data, callback) {
@ -109,9 +130,7 @@ function post_toast_callback(url, data, callback) {
return false;
}
};
xhr.send(form);
}
if (window.location.pathname != '/submit')