Revert "submitFormAjax is different enough from postToast to be annoying"
This reverts commit 0565e8d6a9
.
This commit is contained in:
parent
0565e8d6a9
commit
90186bd99f
2 changed files with 73 additions and 55 deletions
|
@ -81,56 +81,6 @@ function expandDesktopImage(image) {
|
||||||
document.getElementById("desktop-expanded-image-wrap-link").href = image;
|
document.getElementById("desktop-expanded-image-wrap-link").href = image;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function showToastFromXhr(xhr, callbackFn) { // If callbackFn is specified, we'll try to use that first
|
|
||||||
var message = null;
|
|
||||||
|
|
||||||
if (callbackFn !== null) {
|
|
||||||
try {
|
|
||||||
const result = callbackFn(xhr);
|
|
||||||
if (typeof result === 'string' && result !== null) {
|
|
||||||
message = result;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("Failed to run callback function for postToast", e, xhr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let data;
|
|
||||||
try {
|
|
||||||
data = JSON.parse(xhr.response);
|
|
||||||
} catch (e) {
|
|
||||||
console.error("Failed to parse response as JSON", e);
|
|
||||||
if (message === null) return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xhr.status >= 200 && xhr.status < 300 && data && data['message']) {
|
|
||||||
const toastPostSuccessTextElement = document.getElementById("toast-post-success-text");
|
|
||||||
if (message !== null) {
|
|
||||||
message = data["message"];
|
|
||||||
}
|
|
||||||
if (message === null) {
|
|
||||||
message = "Success!";
|
|
||||||
}
|
|
||||||
toastPostSuccessTextElement.innerText = message;
|
|
||||||
bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')).show();
|
|
||||||
callbackFn(xhr);
|
|
||||||
} else {
|
|
||||||
const toastPostErrorTextElement = document.getElementById('toast-post-error-text');
|
|
||||||
if (message != null && data) {
|
|
||||||
if (data["details"]) {
|
|
||||||
message = data["details"];
|
|
||||||
} else if (data["error"]) {
|
|
||||||
message = data["error"];
|
|
||||||
} else {
|
|
||||||
message = "Error, please try again later.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (messageOverride) toastPostErrorTextElement.innerText = message;
|
|
||||||
bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function postToast(targetElement, url, method, data, callbackFn) {
|
function postToast(targetElement, url, method, data, callbackFn) {
|
||||||
if (targetElement) { // disable element to avoid repeated requests
|
if (targetElement) { // disable element to avoid repeated requests
|
||||||
t.disabled = true;
|
t.disabled = true;
|
||||||
|
@ -149,7 +99,51 @@ function postToast(targetElement, url, method, data, callbackFn) {
|
||||||
}
|
}
|
||||||
|
|
||||||
xhr.onload = function() {
|
xhr.onload = function() {
|
||||||
showToastFromXhr(xhr, callbackFn);
|
let data;
|
||||||
|
try {
|
||||||
|
data = JSON.parse(xhr.response);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to parse response as JSON", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
var message = null;
|
||||||
|
|
||||||
|
if (callbackFn !== null) {
|
||||||
|
try {
|
||||||
|
const result = callbackFn(xhr);
|
||||||
|
if (typeof result === 'string' && result !== null) {
|
||||||
|
message = result;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to run callback function for postToast", e, xhr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (xhr.status >= 200 && xhr.status < 300 && data && data['message']) {
|
||||||
|
const toastPostSuccessTextElement = document.getElementById("toast-post-success-text");
|
||||||
|
if (message !== null) {
|
||||||
|
message = data["message"];
|
||||||
|
}
|
||||||
|
if (message === null) {
|
||||||
|
message = "Success!";
|
||||||
|
}
|
||||||
|
toastPostSuccessTextElement.innerText = message;
|
||||||
|
bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')).show();
|
||||||
|
callbackFn(xhr);
|
||||||
|
} else {
|
||||||
|
const toastPostErrorTextElement = document.getElementById('toast-post-error-text');
|
||||||
|
if (message != null && data) {
|
||||||
|
if (data["details"]) {
|
||||||
|
message = data["details"];
|
||||||
|
} else if (data["error"]) {
|
||||||
|
message = data["error"];
|
||||||
|
} else {
|
||||||
|
message = "Error, please try again later.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (messageOverride) toastPostErrorTextElement.innerText = message;
|
||||||
|
bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')).show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|
|
@ -75,7 +75,6 @@ function transferBux(mobile=false) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function submitFormAjax(e) {
|
function submitFormAjax(e) {
|
||||||
e.preventDefault();
|
|
||||||
document.getElementById('message').classList.add('d-none');
|
document.getElementById('message').classList.add('d-none');
|
||||||
document.getElementById('message-mobile').classList.add('d-none');
|
document.getElementById('message-mobile').classList.add('d-none');
|
||||||
document.getElementById('message-preview').classList.add('d-none');
|
document.getElementById('message-preview').classList.add('d-none');
|
||||||
|
@ -83,7 +82,7 @@ function submitFormAjax(e) {
|
||||||
|
|
||||||
const form = e.target;
|
const form = e.target;
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
// TODO: partially combine with postToast?
|
e.preventDefault();
|
||||||
|
|
||||||
formData = new FormData(form);
|
formData = new FormData(form);
|
||||||
|
|
||||||
|
@ -99,9 +98,34 @@ function submitFormAjax(e) {
|
||||||
xhr.setRequestHeader('xhr', 'xhr');
|
xhr.setRequestHeader('xhr', 'xhr');
|
||||||
|
|
||||||
xhr.onload = function() {
|
xhr.onload = function() {
|
||||||
showToastFromXhr(xhr, null);
|
if (xhr.status >= 200 && xhr.status < 300) {
|
||||||
|
let data = JSON.parse(xhr.response);
|
||||||
|
try {
|
||||||
|
document.getElementById('toast-post-success-text').innerText = data["message"];
|
||||||
|
} catch(e) {
|
||||||
|
document.getElementById('toast-post-success-text').innerText = "Action successful!";
|
||||||
|
}
|
||||||
|
var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success'));
|
||||||
|
myToast.show();
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
document.getElementById('toast-post-error-text').innerText = "Error, please try again later."
|
||||||
|
try {
|
||||||
|
let data=JSON.parse(xhr.response);
|
||||||
|
var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error'));
|
||||||
|
myToast.show();
|
||||||
|
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"];
|
||||||
|
} catch(e) {
|
||||||
|
var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success'));
|
||||||
|
myToast.hide();
|
||||||
|
var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error'));
|
||||||
|
myToast.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
xhr.send(formData);
|
xhr.send(formData);
|
||||||
return false;
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue