use postToastCallback in postToast

master
Aevann1 2022-11-03 21:07:08 +02:00
parent c2c5e61e60
commit 20fc3e29b2
9 changed files with 131 additions and 244 deletions

View File

@ -153,19 +153,17 @@ function buy(mb) {
function giveaward(t) {
const kind = document.getElementById('kind').value;
postToastCallback(t.dataset.action,
postToastCallback(t, t.dataset.action,
{
"kind": kind,
"note": document.getElementById('note').value
},
(xhr) => {
if(xhr.status == 200) {
() => {
let owned = document.getElementById(`${kind}-owned`)
let ownednum = Number(owned.textContent) - 1;
owned.textContent = ownednum
if (ownednum == 0)
document.getElementById('giveaward').disabled=true;
}
}
);
}

View File

@ -1,13 +1,11 @@
function removeComment(t,comment_id,button1,button2,cls) {
url="/remove_comment/"+comment_id
t.disabled = true;
t.classList.add("disabled");
postToastCallback(url,
{
},
(xhr) => {
if (xhr.status >= 200 && xhr.status < 300) {
postToast(t, url,
button1,
button2,
cls,
() => {
if (window.location.pathname == '/admin/reported/comments')
{
document.getElementById("post-info-"+comment_id).remove()
@ -20,26 +18,19 @@ function removeComment(t,comment_id,button1,button2,cls) {
} catch(e) {
document.getElementById("context").classList.add("banned");
}
document.getElementById(button1).classList.toggle(cls);
document.getElementById(button2).classList.toggle(cls);
}
}
t.disabled = false;
t.classList.remove("disabled");
}
);
}
function approveComment(t,comment_id,button1,button2,cls) {
url="/approve_comment/"+comment_id
t.disabled = true;
t.classList.add("disabled");
postToastCallback(url,
{
},
(xhr) => {
if (xhr.status >= 200 && xhr.status < 300) {
postToast(t, url,
button1,
button2,
cls,
() => {
if (window.location.pathname == '/admin/reported/comments')
{
document.getElementById("post-info-"+comment_id).remove()
@ -52,13 +43,8 @@ function approveComment(t,comment_id,button1,button2,cls) {
} catch(e) {
document.getElementById("context").classList.remove("banned");
}
document.getElementById(button1).classList.toggle(cls);
document.getElementById(button2).classList.toggle(cls);
}
}
t.disabled = false;
t.classList.remove("disabled");
}
);
}

View File

@ -1,17 +1,3 @@
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");
}
}
function getMessageFromJsonData(success, json) {
let message = success ? "Success!" : "Error, please try again later";
let key = success ? "message" : "error";
@ -37,43 +23,6 @@ function showToast(success, message, isToastTwo=false) {
bootstrap.Toast.getOrCreateInstance(document.getElementById(element)).show();
}
function postToastLoad(xhr, className, button1, button2, extraActionsOnSuccess, extraActionsOnError) {
let data
try {
data = JSON.parse(xhr.response)
}
catch (e) {
console.log(e)
}
success = xhr.status >= 200 && xhr.status < 300;
showToast(success, getMessageFromJsonData(success, data));
if (success) {
if (button1)
{
if (typeof(button1) == 'boolean') {
location.reload()
} else {
document.getElementById(button1).classList.toggle(className);
document.getElementById(button2).classList.toggle(className);
}
}
if (extraActionsOnSuccess) extraActionsOnSuccess(xhr);
} else {
if (extraActionsOnError) extraActionsOnError(xhr);
}
}
function postPostToastNonShopActions(t, url, button1, button2, className) {
let isShopConfirm = isShopConfirmation(t);
if (!isShopConfirm)
{
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
}
}
function createXhrWithFormKey(url, method="POST", form=new FormData()) {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
@ -83,17 +32,15 @@ function createXhrWithFormKey(url, method="POST", form=new FormData()) {
return [xhr, form]; // hacky but less stupid than what we were doing before
}
function postToast(t, url, button1, button2, className, extraActions, extraActionsError) {
prePostToastNonShopActions(t, url, button1, button2, className);
const xhr = createXhrWithFormKey(url);
xhr[0].onload = function() {
postToastLoad(xhr[0], className, button1, button2, extraActions, extraActionsError)
postPostToastNonShopActions(t, url, button1, button2, className)
};
xhr[0].send(xhr[1]);
}
function postToastCallback(t, url, data, callback) {
const isShopConfirm = t.id.startsWith('buy1-') || t.id.startsWith('buy2-')
if (!isShopConfirm)
{
t.disabled = true;
t.classList.add("disabled");
}
function postToastCallback(url, data, callback) {
let form = new FormData();
if(typeof data === 'object' && data !== null) {
for(let k of Object.keys(data)) {
@ -103,9 +50,9 @@ function postToastCallback(url, data, callback) {
const xhr = createXhrWithFormKey(url, "POST", form);
xhr[0].onload = function() {
let result
if (callback) result = callback(xhr[0]);
let message;
let success = xhr[0].status >= 200 && xhr[0].status < 300;
if (success && callback) result = callback(xhr[0]);
if (typeof result == "string") {
message = result;
} else {
@ -114,9 +61,33 @@ function postToastCallback(url, data, callback) {
let oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (success ? 'error': 'success'))); // intentionally reversed here: this is the old toast
oldToast.hide();
showToast(success, message);
if (!isShopConfirm) {
t.disabled = false;
t.classList.remove("disabled");
}
return success;
};
xhr[0].send(xhr[1]);
if (!isShopConfirm)
{
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
}
}
function postToast(t, url, button1, button2, cls, callback) {
postToastCallback(t, url,
{
},
(xhr) => {
document.getElementById(button1).classList.toggle(cls);
document.getElementById(button2).classList.toggle(cls);
if (callback) callback(xhr);
}
);
}
if (window.location.pathname != '/submit')

View File

@ -1,11 +1,8 @@
function pinPost(t, id) {
t.disabled = true;
t.classList.add("disabled");
postToastCallback(`/sticky/${id}`,
postToastCallback(t, `/sticky/${id}`,
{
},
(xhr) => {
if (xhr.status >= 200 && xhr.status < 300) {
response = JSON.parse(xhr.response);
length = response["length"];
if (length == "permanently") {
@ -15,34 +12,19 @@ function pinPost(t, id) {
t.innerHTML = t.innerHTML.replace(t.textContent, 'Pin permanently');
}
t.nextElementSibling.classList.remove('d-none');
t.disabled = false;
t.classList.remove("disabled");
}
}
);
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
}
function unpinPost(t, id) {
t.disabled = true;
t.classList.add("disabled");
postToastCallback(`/unsticky/${id}`,
postToastCallback(t, `/unsticky/${id}`,
{
},
(xhr) => {
if (xhr.status >= 200 && xhr.status < 300) {
() => {
t.classList.add('d-none');
t.previousElementSibling.classList.remove('d-none');
t.disabled = false;
t.classList.remove("disabled");
}
const prev = t.previousElementSibling;
prev.innerHTML = prev.innerHTML.replace(prev.textContent, 'Pin for 1 hour');
prev.classList.remove('d-none');
}
);
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
}

View File

@ -1,13 +1,11 @@
function removePost(t,post_id,button1,button2,cls) {
url="/remove_post/"+post_id
t.disabled = true;
t.classList.add("disabled");
postToastCallback(url,
{
},
(xhr) => {
if (xhr.status >= 200 && xhr.status < 300) {
postToast(t, url,
button1,
button2,
cls,
() => {
if (window.location.pathname == '/admin/reported/posts')
{
document.getElementById("flaggers-"+post_id).remove()
@ -16,13 +14,8 @@ function removePost(t,post_id,button1,button2,cls) {
else
{
document.getElementById("post-"+post_id).classList.add("banned");
document.getElementById(button1).classList.toggle(cls);
document.getElementById(button2).classList.toggle(cls);
}
}
t.disabled = false;
t.classList.remove("disabled");
}
);
}
@ -30,13 +23,11 @@ function removePost(t,post_id,button1,button2,cls) {
function approvePost(t,post_id,button1,button2,cls) {
url="/approve_post/"+post_id
t.disabled = true;
t.classList.add("disabled");
postToastCallback(url,
{
},
(xhr) => {
if (xhr.status >= 200 && xhr.status < 300) {
postToast(t, url,
button1,
button2,
cls,
() => {
if (window.location.pathname == '/admin/reported/posts')
{
document.getElementById("flaggers-"+post_id).remove()
@ -45,12 +36,7 @@ function approvePost(t,post_id,button1,button2,cls) {
else
{
document.getElementById("post-"+post_id).classList.remove("banned");
document.getElementById(button1).classList.toggle(cls);
document.getElementById(button2).classList.toggle(cls);
}
}
t.disabled = false;
t.classList.remove("disabled");
}
);
}

View File

@ -23,7 +23,7 @@ function updateBux(mobile=false) {
if(amount > 0) document.getElementById("bux-transfer-taxed" + suf).innerText = amount;
}
function transferCoins(mobile=false) {
function transferCoins(t, mobile=false) {
for(let el of document.getElementsByClassName('toggleable')) {
el.classList.add('d-none');
@ -35,24 +35,22 @@ function transferCoins(mobile=false) {
let transferred = amount - Math.ceil(amount*TRANSFER_TAX);
let username = document.getElementById('username').innerHTML;
postToastCallback(`/@${username}/transfer_coins`,
postToastCallback(t, `/@${username}/transfer_coins`,
{
"amount": document.getElementById(mobile ? "coin-transfer-amount-mobile" : "coin-transfer-amount").value,
"reason": document.getElementById(mobile ? "coin-transfer-reason-mobile" : "coin-transfer-reason").value
},
(xhr) => {
if(xhr.status == 200) {
() => {
document.getElementById("user-coins-amount").innerText = parseInt(document.getElementById("user-coins-amount").innerText) - amount;
document.getElementById("profile-coins-amount-mobile").innerText = parseInt(document.getElementById("profile-coins-amount-mobile").innerText) + transferred;
document.getElementById("profile-coins-amount").innerText = parseInt(document.getElementById("profile-coins-amount").innerText) + transferred;
}
}
);
setTimeout(_ => this.disabled = false, 2000);
}
function transferBux(mobile=false) {
function transferBux(t, mobile=false) {
for(let el of document.getElementsByClassName('toggleable')) {
el.classList.add('d-none');
}
@ -62,18 +60,16 @@ function transferBux(mobile=false) {
let amount = parseInt(document.getElementById(mobile ? "bux-transfer-amount-mobile" : "bux-transfer-amount").value);
let username = document.getElementById('username').innerHTML
postToastCallback(`/@${username}/transfer_bux`,
postToastCallback(t, `/@${username}/transfer_bux`,
{
"amount": document.getElementById(mobile ? "bux-transfer-amount-mobile" : "bux-transfer-amount").value,
"reason": document.getElementById(mobile ? "bux-transfer-reason-mobile" : "bux-transfer-reason").value
},
(xhr) => {
if(xhr.status == 200) {
() => {
document.getElementById("user-bux-amount").innerText = parseInt(document.getElementById("user-bux-amount").innerText) - amount;
document.getElementById("profile-bux-amount-mobile").innerText = parseInt(document.getElementById("profile-bux-amount-mobile").innerText) + amount;
document.getElementById("profile-bux-amount").innerText = parseInt(document.getElementById("profile-bux-amount").innerText) + amount;
}
}
);
setTimeout(_ => this.disabled = false, 2000);

View File

@ -114,42 +114,26 @@
})
function approve_hat(t, name) {
t.disabled = true;
t.classList.add("disabled");
postToastCallback(`/admin/approve/hat/${name}`,
postToastCallback(t, `/admin/approve/hat/${name}`,
{
"description": document.getElementById(`${name}-description`).value,
"name": document.getElementById(`${name}-name`).value,
"price": document.getElementById(`${name}-price`).value,
},
(xhr) => {
if(xhr.status == 200) {
() => {
document.getElementById(`${name}-hat`).remove()
}
}
);
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
}
function remove_hat(t, name) {
t.disabled = true;
t.classList.add("disabled");
postToastCallback(`/remove/hat/${name}`,
postToastCallback(t, `/remove/hat/${name}`,
{
},
(xhr) => {
if(xhr.status == 200) {
() => {
document.getElementById(`${name}-hat`).remove()
}
}
);
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
}
</script>

View File

@ -116,41 +116,25 @@
})
function approve_marsey(t, name) {
t.disabled = true;
t.classList.add("disabled");
postToastCallback(`/admin/approve/marsey/${name}`,
postToastCallback(t, `/admin/approve/marsey/${name}`,
{
"tags": document.getElementById(`${name}-tags`).value,
"name": document.getElementById(`${name}-name`).value,
},
(xhr) => {
if(xhr.status == 200) {
() => {
document.getElementById(`${name}-marsey`).remove()
}
}
);
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
}
function remove_marsey(t, name) {
t.disabled = true;
t.classList.add("disabled");
postToastCallback(`/remove/marsey/${name}`,
postToastCallback(t, `/remove/marsey/${name}`,
{
},
(xhr) => {
if(xhr.status == 200) {
() => {
document.getElementById(`${name}-marsey`).remove()
}
}
);
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
}
</script>

View File

@ -194,14 +194,14 @@
<input autocomplete="off" id="coin-transfer-amount" class="form-control" name="amount" type="number" oninput="updateTax()">
<input autocomplete="off" id="coin-transfer-reason" maxlength=200 type="text" class="form-control" name="reason" placeholder="Gift message! (optional)">
<div>{{u.username}} will receive <span id="coins-transfer-taxed">0</span> coins</div>
<button type="button" class="btn btn-primary mt-2 mb-3" onclick="transferCoins()">Gift</button>
<button type="button" class="btn btn-primary mt-2 mb-3" onclick="transferCoins(this)">Gift</button>
</div>
<div class="d-none mt-3 toggleable" id="bux-transfer">
<input autocomplete="off" id="bux-transfer-amount" class="form-control" name="amount" type="number" oninput="updateBux()">
<input autocomplete="off" id="bux-transfer-reason" type="text" class="form-control" name="reason" placeholder="Gift message! (optional)">
<div>{{u.username}} will receive <span id="bux-transfer-taxed">0</span> marseybux</div>
<button type="button" class="btn btn-primary mt-2 mb-3" onclick="transferBux()">Gift</button>
<button type="button" class="btn btn-primary mt-2 mb-3" onclick="transferBux(this)">Gift</button>
</div>
{{userpage_admintools.userAdminTools('desktop')}}
@ -447,14 +447,14 @@
<input autocomplete="off" id="coin-transfer-amount-mobile" class="form-control" name="amount" type="number" oninput="updateTax(true)">
<input autocomplete="off" id="coin-transfer-reason-mobile" maxlength=200 type="text" class="form-control" name="reason" placeholder="Gift message! (optional)">
<div>{{u.username}} will receive <span id="coins-transfer-taxed-mobile">0</span> coins</div>
<button type="button" class="btn btn-primary mt-2 mb-3" onclick="transferCoins(true)">Gift</button>
<button type="button" class="btn btn-primary mt-2 mb-3" onclick="transferCoins(this, true)">Gift</button>
</div>
<div class="d-none mt-3 toggleable" id="bux-transfer-mobile">
<input autocomplete="off" id="bux-transfer-amount-mobile" class="form-control" name="amount" type="number" oninput="updateBux(true)">
<input autocomplete="off" id="bux-transfer-reason-mobile" type="text" class="form-control" name="reason" placeholder="Gift message! (optional)">
<div>{{u.username}} will receive <span id="bux-transfer-taxed-mobile">0</span> marseybux</div>
<button type="button" class="btn btn-primary mt-2 mb-3" onclick="transferBux(true)">Gift</button>
<button type="button" class="btn btn-primary mt-2 mb-3" onclick="transferBux(this, true)">Gift</button>
</div>
{{userpage_admintools.userAdminTools('mobile')}}
{% endif %}