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) { function giveaward(t) {
const kind = document.getElementById('kind').value; const kind = document.getElementById('kind').value;
postToastCallback(t.dataset.action, postToastCallback(t, t.dataset.action,
{ {
"kind": kind, "kind": kind,
"note": document.getElementById('note').value "note": document.getElementById('note').value
}, },
(xhr) => { () => {
if(xhr.status == 200) { let owned = document.getElementById(`${kind}-owned`)
let owned = document.getElementById(`${kind}-owned`) let ownednum = Number(owned.textContent) - 1;
let ownednum = Number(owned.textContent) - 1; owned.textContent = ownednum
owned.textContent = ownednum if (ownednum == 0)
if (ownednum == 0) document.getElementById('giveaward').disabled=true;
document.getElementById('giveaward').disabled=true;
}
} }
); );
} }

View File

@ -1,31 +1,24 @@
function removeComment(t,comment_id,button1,button2,cls) { function removeComment(t,comment_id,button1,button2,cls) {
url="/remove_comment/"+comment_id url="/remove_comment/"+comment_id
t.disabled = true; postToast(t, url,
t.classList.add("disabled"); button1,
postToastCallback(url, button2,
{ cls,
}, () => {
(xhr) => { if (window.location.pathname == '/admin/reported/comments')
if (xhr.status >= 200 && xhr.status < 300) { {
if (window.location.pathname == '/admin/reported/comments') document.getElementById("post-info-"+comment_id).remove()
{ document.getElementById("comment-"+comment_id).remove()
document.getElementById("post-info-"+comment_id).remove() }
document.getElementById("comment-"+comment_id).remove() else
} {
else try {
{ document.getElementById("comment-"+comment_id+"-only").classList.add("banned");
try { } catch(e) {
document.getElementById("comment-"+comment_id+"-only").classList.add("banned"); document.getElementById("context").classList.add("banned");
} 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");
} }
); );
} }
@ -33,31 +26,24 @@ function removeComment(t,comment_id,button1,button2,cls) {
function approveComment(t,comment_id,button1,button2,cls) { function approveComment(t,comment_id,button1,button2,cls) {
url="/approve_comment/"+comment_id url="/approve_comment/"+comment_id
t.disabled = true; postToast(t, url,
t.classList.add("disabled"); button1,
postToastCallback(url, button2,
{ cls,
}, () => {
(xhr) => { if (window.location.pathname == '/admin/reported/comments')
if (xhr.status >= 200 && xhr.status < 300) { {
if (window.location.pathname == '/admin/reported/comments') document.getElementById("post-info-"+comment_id).remove()
{ document.getElementById("comment-"+comment_id).remove()
document.getElementById("post-info-"+comment_id).remove() }
document.getElementById("comment-"+comment_id).remove() else
} {
else try {
{ document.getElementById("comment-"+comment_id+"-only").classList.remove("banned");
try { } catch(e) {
document.getElementById("comment-"+comment_id+"-only").classList.remove("banned"); document.getElementById("context").classList.remove("banned");
} 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) { function getMessageFromJsonData(success, json) {
let message = success ? "Success!" : "Error, please try again later"; let message = success ? "Success!" : "Error, please try again later";
let key = success ? "message" : "error"; let key = success ? "message" : "error";
@ -37,43 +23,6 @@ function showToast(success, message, isToastTwo=false) {
bootstrap.Toast.getOrCreateInstance(document.getElementById(element)).show(); 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()) { function createXhrWithFormKey(url, method="POST", form=new FormData()) {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open(method, url); 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 return [xhr, form]; // hacky but less stupid than what we were doing before
} }
function postToast(t, url, button1, button2, className, extraActions, extraActionsError) { function postToastCallback(t, url, data, callback) {
prePostToastNonShopActions(t, url, button1, button2, className); const isShopConfirm = t.id.startsWith('buy1-') || t.id.startsWith('buy2-')
const xhr = createXhrWithFormKey(url);
xhr[0].onload = function() { if (!isShopConfirm)
postToastLoad(xhr[0], className, button1, button2, extraActions, extraActionsError) {
postPostToastNonShopActions(t, url, button1, button2, className) t.disabled = true;
}; t.classList.add("disabled");
xhr[0].send(xhr[1]); }
}
function postToastCallback(url, data, callback) {
let form = new FormData(); let form = new FormData();
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)) {
@ -103,9 +50,9 @@ function postToastCallback(url, data, callback) {
const xhr = createXhrWithFormKey(url, "POST", form); const xhr = createXhrWithFormKey(url, "POST", form);
xhr[0].onload = function() { xhr[0].onload = function() {
let result let result
if (callback) result = callback(xhr[0]);
let message; let message;
let success = xhr[0].status >= 200 && xhr[0].status < 300; let success = xhr[0].status >= 200 && xhr[0].status < 300;
if (success && callback) result = callback(xhr[0]);
if (typeof result == "string") { if (typeof result == "string") {
message = result; message = result;
} else { } 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 let oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (success ? 'error': 'success'))); // intentionally reversed here: this is the old toast
oldToast.hide(); oldToast.hide();
showToast(success, message); showToast(success, message);
if (!isShopConfirm) {
t.disabled = false;
t.classList.remove("disabled");
}
return success; return success;
}; };
xhr[0].send(xhr[1]); 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') if (window.location.pathname != '/submit')

View File

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

View File

@ -1,27 +1,20 @@
function removePost(t,post_id,button1,button2,cls) { function removePost(t,post_id,button1,button2,cls) {
url="/remove_post/"+post_id url="/remove_post/"+post_id
t.disabled = true; postToast(t, url,
t.classList.add("disabled"); button1,
postToastCallback(url, button2,
{ cls,
}, () => {
(xhr) => { if (window.location.pathname == '/admin/reported/posts')
if (xhr.status >= 200 && xhr.status < 300) { {
if (window.location.pathname == '/admin/reported/posts') document.getElementById("flaggers-"+post_id).remove()
{ document.getElementById("post-"+post_id).remove()
document.getElementById("flaggers-"+post_id).remove() }
document.getElementById("post-"+post_id).remove() else
} {
else document.getElementById("post-"+post_id).classList.add("banned");
{
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,27 +23,20 @@ function removePost(t,post_id,button1,button2,cls) {
function approvePost(t,post_id,button1,button2,cls) { function approvePost(t,post_id,button1,button2,cls) {
url="/approve_post/"+post_id url="/approve_post/"+post_id
t.disabled = true; postToast(t, url,
t.classList.add("disabled"); button1,
postToastCallback(url, button2,
{ cls,
}, () => {
(xhr) => { if (window.location.pathname == '/admin/reported/posts')
if (xhr.status >= 200 && xhr.status < 300) { {
if (window.location.pathname == '/admin/reported/posts') document.getElementById("flaggers-"+post_id).remove()
{ document.getElementById("post-"+post_id).remove()
document.getElementById("flaggers-"+post_id).remove() }
document.getElementById("post-"+post_id).remove() else
} {
else document.getElementById("post-"+post_id).classList.remove("banned");
{
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; 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')) { for(let el of document.getElementsByClassName('toggleable')) {
el.classList.add('d-none'); el.classList.add('d-none');
@ -35,24 +35,22 @@ function transferCoins(mobile=false) {
let transferred = amount - Math.ceil(amount*TRANSFER_TAX); let transferred = amount - Math.ceil(amount*TRANSFER_TAX);
let username = document.getElementById('username').innerHTML; 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, "amount": document.getElementById(mobile ? "coin-transfer-amount-mobile" : "coin-transfer-amount").value,
"reason": document.getElementById(mobile ? "coin-transfer-reason-mobile" : "coin-transfer-reason").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("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-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; document.getElementById("profile-coins-amount").innerText = parseInt(document.getElementById("profile-coins-amount").innerText) + transferred;
} }
}
); );
setTimeout(_ => this.disabled = false, 2000); setTimeout(_ => this.disabled = false, 2000);
} }
function transferBux(mobile=false) { function transferBux(t, mobile=false) {
for(let el of document.getElementsByClassName('toggleable')) { for(let el of document.getElementsByClassName('toggleable')) {
el.classList.add('d-none'); 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 amount = parseInt(document.getElementById(mobile ? "bux-transfer-amount-mobile" : "bux-transfer-amount").value);
let username = document.getElementById('username').innerHTML 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, "amount": document.getElementById(mobile ? "bux-transfer-amount-mobile" : "bux-transfer-amount").value,
"reason": document.getElementById(mobile ? "bux-transfer-reason-mobile" : "bux-transfer-reason").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("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-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; document.getElementById("profile-bux-amount").innerText = parseInt(document.getElementById("profile-bux-amount").innerText) + amount;
} }
}
); );
setTimeout(_ => this.disabled = false, 2000); setTimeout(_ => this.disabled = false, 2000);

View File

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

View File

@ -116,41 +116,25 @@
}) })
function approve_marsey(t, name) { function approve_marsey(t, name) {
t.disabled = true; postToastCallback(t, `/admin/approve/marsey/${name}`,
t.classList.add("disabled");
postToastCallback(`/admin/approve/marsey/${name}`,
{ {
"tags": document.getElementById(`${name}-tags`).value, "tags": document.getElementById(`${name}-tags`).value,
"name": document.getElementById(`${name}-name`).value, "name": document.getElementById(`${name}-name`).value,
}, },
(xhr) => { () => {
if(xhr.status == 200) {
document.getElementById(`${name}-marsey`).remove() document.getElementById(`${name}-marsey`).remove()
} }
}
); );
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
} }
function remove_marsey(t, name) { function remove_marsey(t, name) {
t.disabled = true; postToastCallback(t, `/remove/marsey/${name}`,
t.classList.add("disabled");
postToastCallback(`/remove/marsey/${name}`,
{ {
}, },
(xhr) => { () => {
if(xhr.status == 200) {
document.getElementById(`${name}-marsey`).remove() document.getElementById(`${name}-marsey`).remove()
} }
}
); );
setTimeout(() => {
t.disabled = false;
t.classList.remove("disabled");
}, 2000);
} }
</script> </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-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)"> <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> <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>
<div class="d-none mt-3 toggleable" id="bux-transfer"> <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-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)"> <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> <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> </div>
{{userpage_admintools.userAdminTools('desktop')}} {{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-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)"> <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> <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>
<div class="d-none mt-3 toggleable" id="bux-transfer-mobile"> <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-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)"> <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> <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> </div>
{{userpage_admintools.userAdminTools('mobile')}} {{userpage_admintools.userAdminTools('mobile')}}
{% endif %} {% endif %}