diff --git a/files/assets/js/bottom.js b/files/assets/js/bottom.js index c42800604..bfe2d2d5b 100644 --- a/files/assets/js/bottom.js +++ b/files/assets/js/bottom.js @@ -64,38 +64,20 @@ for (const element of undisable_element) { }); } -async function handleSettingSwitch(event) { - let input = event.currentTarget; - input.disabled = true; - input.classList.add("disabled"); - const form = new FormData(); - form.append("formkey", formkey()); - const res = await fetch( - `/settings/personal?${input.name}=${input.checked}`, - { - method: "POST", - headers: { - xhr: "xhr", - }, - body: form, - }, - ).catch(() => ({ ok: false })); - let message; - if (res.ok) { - ({message} = await res.json()); - // the slur and profanity replacers have special make-permanent switches - if (["slurreplacerswitch", "profanityreplacerswitch"].includes(input.id)) { - document.getElementById( - `${input.id.replace("switch", "")}-perma-link` - ).hidden = !input.checked; - } - } else { - // toggle the input back if the request doesn't go through - input.checked = !input.checked; - } - showToast(res.ok, message); - input.disabled = false; - input.classList.remove("disabled"); +function handleSettingSwitch(t) { + postToast(t, `/settings/personal?${t.name}=${t.checked}`, + {}, + () => { + if (["slurreplacerswitch", "profanityreplacerswitch"].includes(t.id)) { + document.getElementById( + `${t.id.replace("switch", "")}-perma-link` + ).hidden = !t.checked; + } + }, + () => { + t.checked = !t.checked; + }, + ); } const setting_switchs = document.getElementsByClassName('setting_switch'); @@ -104,7 +86,7 @@ for (const element of setting_switchs) { console.log("Nonce check failed!") continue } - element.addEventListener('change', handleSettingSwitch); + element.addEventListener('change', () => {handleSettingSwitch(element)}); } const setting_selects = document.getElementsByClassName('setting_select'); diff --git a/files/assets/js/core.js b/files/assets/js/core.js index 658c6ba90..d54d9301b 100644 --- a/files/assets/js/core.js +++ b/files/assets/js/core.js @@ -32,7 +32,7 @@ function createXhrWithFormKey(url, form=new FormData()) { return [xhr, form]; // hacky but less stupid than what we were doing before } -function postToast(t, url, data, extraActionsOnSuccess) { +function postToast(t, url, data, extraActionsOnSuccess, extraActionsOnFailure) { let form = new FormData(); if (typeof data === 'object' && data !== null) { for(let k of Object.keys(data)) { @@ -57,6 +57,7 @@ function postToast(t, url, data, extraActionsOnSuccess) { } showToast(success, message); if (success && extraActionsOnSuccess) extraActionsOnSuccess(xhr[0]); + if (!success && extraActionsOnFailure) extraActionsOnFailure(xhr[0]); return success; }; xhr[0].send(xhr[1]);