From 20fc3e29b2bae43a49b78ad6f427be4a983a5c5d Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Thu, 3 Nov 2022 21:07:08 +0200 Subject: [PATCH] use postToastCallback in postToast --- files/assets/js/award_modal.js | 16 +++-- files/assets/js/comments_admin.js | 78 ++++++++++------------- files/assets/js/core.js | 95 ++++++++++------------------- files/assets/js/pinpost.js | 48 +++++---------- files/assets/js/submission_admin.js | 66 ++++++++------------ files/assets/js/userpage_v.js | 16 ++--- files/templates/submit_hats.html | 24 ++------ files/templates/submit_marseys.html | 24 ++------ files/templates/userpage.html | 8 +-- 9 files changed, 131 insertions(+), 244 deletions(-) diff --git a/files/assets/js/award_modal.js b/files/assets/js/award_modal.js index d1b4f34b9..b59926ee2 100644 --- a/files/assets/js/award_modal.js +++ b/files/assets/js/award_modal.js @@ -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; - } + () => { + let owned = document.getElementById(`${kind}-owned`) + let ownednum = Number(owned.textContent) - 1; + owned.textContent = ownednum + if (ownednum == 0) + document.getElementById('giveaward').disabled=true; } ); } diff --git a/files/assets/js/comments_admin.js b/files/assets/js/comments_admin.js index 2f3dcf24b..4608a866c 100644 --- a/files/assets/js/comments_admin.js +++ b/files/assets/js/comments_admin.js @@ -1,31 +1,24 @@ 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) { - if (window.location.pathname == '/admin/reported/comments') - { - document.getElementById("post-info-"+comment_id).remove() - document.getElementById("comment-"+comment_id).remove() - } - else - { - try { - document.getElementById("comment-"+comment_id+"-only").classList.add("banned"); - } catch(e) { - document.getElementById("context").classList.add("banned"); - } - document.getElementById(button1).classList.toggle(cls); - document.getElementById(button2).classList.toggle(cls); + postToast(t, url, + button1, + button2, + cls, + () => { + if (window.location.pathname == '/admin/reported/comments') + { + document.getElementById("post-info-"+comment_id).remove() + document.getElementById("comment-"+comment_id).remove() + } + else + { + try { + document.getElementById("comment-"+comment_id+"-only").classList.add("banned"); + } catch(e) { + document.getElementById("context").classList.add("banned"); } } - 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) { url="/approve_comment/"+comment_id - t.disabled = true; - t.classList.add("disabled"); - postToastCallback(url, - { - }, - (xhr) => { - 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() - } - else - { - try { - document.getElementById("comment-"+comment_id+"-only").classList.remove("banned"); - } catch(e) { - document.getElementById("context").classList.remove("banned"); - } - document.getElementById(button1).classList.toggle(cls); - document.getElementById(button2).classList.toggle(cls); + postToast(t, url, + button1, + button2, + cls, + () => { + if (window.location.pathname == '/admin/reported/comments') + { + document.getElementById("post-info-"+comment_id).remove() + document.getElementById("comment-"+comment_id).remove() + } + else + { + try { + document.getElementById("comment-"+comment_id+"-only").classList.remove("banned"); + } catch(e) { + document.getElementById("context").classList.remove("banned"); } } - t.disabled = false; - t.classList.remove("disabled"); } ); } diff --git a/files/assets/js/core.js b/files/assets/js/core.js index 9c7792675..778f0f5ae 100644 --- a/files/assets/js/core.js +++ b/files/assets/js/core.js @@ -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') diff --git a/files/assets/js/pinpost.js b/files/assets/js/pinpost.js index 155e6beed..1217e13fd 100644 --- a/files/assets/js/pinpost.js +++ b/files/assets/js/pinpost.js @@ -1,48 +1,30 @@ 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") { - t.innerHTML = t.innerHTML.replace(t.textContent, 'Pin for 1 hour'); - t.classList.add('d-none'); - } else { - t.innerHTML = t.innerHTML.replace(t.textContent, 'Pin permanently'); - } - t.nextElementSibling.classList.remove('d-none'); - t.disabled = false; - t.classList.remove("disabled"); + response = JSON.parse(xhr.response); + length = response["length"]; + if (length == "permanently") { + t.innerHTML = t.innerHTML.replace(t.textContent, 'Pin for 1 hour'); + t.classList.add('d-none'); + } else { + t.innerHTML = t.innerHTML.replace(t.textContent, 'Pin permanently'); } + t.nextElementSibling.classList.remove('d-none'); } ); - 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"); - } + () => { + t.classList.add('d-none'); + 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); } diff --git a/files/assets/js/submission_admin.js b/files/assets/js/submission_admin.js index d80ce45da..7c885a367 100644 --- a/files/assets/js/submission_admin.js +++ b/files/assets/js/submission_admin.js @@ -1,27 +1,20 @@ 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) { - if (window.location.pathname == '/admin/reported/posts') - { - document.getElementById("flaggers-"+post_id).remove() - document.getElementById("post-"+post_id).remove() - } - else - { - document.getElementById("post-"+post_id).classList.add("banned"); - document.getElementById(button1).classList.toggle(cls); - document.getElementById(button2).classList.toggle(cls); - } + postToast(t, url, + button1, + button2, + cls, + () => { + if (window.location.pathname == '/admin/reported/posts') + { + document.getElementById("flaggers-"+post_id).remove() + document.getElementById("post-"+post_id).remove() + } + else + { + document.getElementById("post-"+post_id).classList.add("banned"); } - 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) { url="/approve_post/"+post_id - t.disabled = true; - t.classList.add("disabled"); - postToastCallback(url, - { - }, - (xhr) => { - 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() - } - else - { - document.getElementById("post-"+post_id).classList.remove("banned"); - document.getElementById(button1).classList.toggle(cls); - document.getElementById(button2).classList.toggle(cls); - } + postToast(t, url, + button1, + button2, + cls, + () => { + if (window.location.pathname == '/admin/reported/posts') + { + document.getElementById("flaggers-"+post_id).remove() + document.getElementById("post-"+post_id).remove() + } + else + { + document.getElementById("post-"+post_id).classList.remove("banned"); } - t.disabled = false; - t.classList.remove("disabled"); } ); } diff --git a/files/assets/js/userpage_v.js b/files/assets/js/userpage_v.js index ad7cf29a4..8ac738df9 100644 --- a/files/assets/js/userpage_v.js +++ b/files/assets/js/userpage_v.js @@ -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); diff --git a/files/templates/submit_hats.html b/files/templates/submit_hats.html index 67cd6e2a0..192e897b5 100644 --- a/files/templates/submit_hats.html +++ b/files/templates/submit_hats.html @@ -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); } diff --git a/files/templates/submit_marseys.html b/files/templates/submit_marseys.html index a599a1014..67878a327 100644 --- a/files/templates/submit_marseys.html +++ b/files/templates/submit_marseys.html @@ -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); } diff --git a/files/templates/userpage.html b/files/templates/userpage.html index 140f90d90..d49167471 100644 --- a/files/templates/userpage.html +++ b/files/templates/userpage.html @@ -194,14 +194,14 @@
{{u.username}} will receive 0 coins
- +
{{u.username}} will receive 0 marseybux
- +
{{userpage_admintools.userAdminTools('desktop')}} @@ -447,14 +447,14 @@
{{u.username}} will receive 0 coins
- +
{{u.username}} will receive 0 marseybux
- +
{{userpage_admintools.userAdminTools('mobile')}} {% endif %}