forked from rDrama/rDrama
1
0
Fork 0

refactor post_toast_callback a bit and create createXhrWithFormKey function

master
justcool393 2022-10-14 04:31:02 -07:00
parent 6884774906
commit f22ef3a7e5
2 changed files with 28 additions and 64 deletions

View File

@ -86,12 +86,8 @@ function vote(type, id, dir) {
} }
} }
} }
const xhr = new XMLHttpRequest(); const xhr = createXhrWithFormKey("/vote/" + type.replace('-mobile','') + "/" + id + "/" + votedirection);
xhr.open("POST", "/vote/" + type.replace('-mobile','') + "/" + id + "/" + votedirection);
xhr.setRequestHeader('xhr', 'xhr');
const form = new FormData()
form.append("formkey", formkey());
xhr.send(form); xhr.send(form);
} }
@ -127,14 +123,9 @@ function pick(kind, canbuy1, canbuy2) {
function buy(mb) { function buy(mb) {
const kind = document.getElementById('kind').value; const kind = document.getElementById('kind').value;
const xhr = new XMLHttpRequest();
url = `/buy/${kind}` url = `/buy/${kind}`
if (mb) url += "?mb=true" if (mb) url += "?mb=true"
xhr.open("POST", url); const xhr = createXhrWithFormKey(url);
xhr.setRequestHeader('xhr', 'xhr');
const form = new FormData()
form.append("formkey", formkey());
if(typeof data === 'object' && data !== null) { if(typeof data === 'object' && data !== null) {
for(let k of Object.keys(data)) { for(let k of Object.keys(data)) {
form.append(k, data[k]); form.append(k, data[k]);

View File

@ -1,5 +1,3 @@
/*const bootstrap = require("./bootstrap");*/
function isShopConfirmation(t) { function isShopConfirmation(t) {
return t.id.startsWith('buy1-') || t.id.startsWith('buy2-'); return t.id.startsWith('buy1-') || t.id.startsWith('buy2-');
} }
@ -76,14 +74,20 @@ function postPostToastNonShopActions(t, url, button1, button2, className) {
} }
} }
function postToast(t, url, button1, button2, className, extraActions, extraActionsError) { function createXhrWithFormKey(url, method="POST", form=null) {
prePostToastNonShopActions(t, url, button1, button2, className) const xhr = new XMLHttpRequest();
const xhr = new XMLHttpRequest(); xhr.open(method, url);
xhr.open("POST", url); xhr.setRequestHeader('xhr', 'xhr');
xhr.setRequestHeader('xhr', 'xhr'); if (!form) {
const form = new FormData() form = new FormData();
form.append("formkey", formkey()); }
form.append("formkey", formkey());
return xhr;
}
function postToast(t, url, button1, button2, className, extraActions, extraActionsError) {
prePostToastNonShopActions(t, url, button1, button2, className);
const xhr = createXhrWithFormKey(url);
xhr.onload = function() { xhr.onload = function() {
postToastLoad(xhr, className, extraActions, extraActionsError) postToastLoad(xhr, className, extraActions, extraActionsError)
postPostToastNonShopActions(t, url, button1, button2, className) postPostToastNonShopActions(t, url, button1, button2, className)
@ -97,57 +101,26 @@ function post_toast(t, url, button1, button2, classname, extra_actions, extra_ac
} }
function post_toast_callback(url, data, callback) { function post_toast_callback(url, data, callback) {
const xhr = new XMLHttpRequest(); const xhr = createXhrWithFormKey(url);
xhr.open("POST", url);
xhr.setRequestHeader('xhr', 'xhr');
const form = new FormData()
form.append("formkey", formkey());
if(typeof data === 'object' && data !== null) { if(typeof data === 'object' && data !== null) {
for(let k of Object.keys(data)) { for(let k of Object.keys(data)) {
form.append(k, data[k]); form.append(k, data[k]);
} }
} }
form.append("formkey", formkey());
xhr.onload = function() { xhr.onload = function() {
let result let result
if (callback) result = callback(xhr); if (callback) result = callback(xhr);
if (xhr.status >= 200 && xhr.status < 300) { let message;
var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-error')); let success = xhr.status >= 200 && xhr.status < 300;
myToast.hide(); if (typeof result == "string") {
message = result;
var myToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-success')); } else {
myToast.show(); message = getMessageFromJsonData(success, JSON.parse(xhr.response));
}
try { let oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (success ? 'error': 'success'))); // intentionally reversed here: this is the old toast
if(typeof result == "string") { oldToast.hide();
document.getElementById('toast-post-success-text').innerText = result; showToast(success, message);
} else { return success;
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); xhr.send(form);
} }