forked from rDrama/rDrama
1
0
Fork 0

fix the issue rDrama/rDrama#189 is attempting to solve

master
Aevann 2023-08-12 13:54:10 +03:00
parent 0dc5761cda
commit 07ba5a65b2
2 changed files with 17 additions and 34 deletions

View File

@ -64,38 +64,20 @@ for (const element of undisable_element) {
}); });
} }
async function handleSettingSwitch(event) { function handleSettingSwitch(t) {
let input = event.currentTarget; postToast(t, `/settings/personal?${t.name}=${t.checked}`,
input.disabled = true; {},
input.classList.add("disabled"); () => {
const form = new FormData(); if (["slurreplacerswitch", "profanityreplacerswitch"].includes(t.id)) {
form.append("formkey", formkey()); document.getElementById(
const res = await fetch( `${t.id.replace("switch", "")}-perma-link`
`/settings/personal?${input.name}=${input.checked}`, ).hidden = !t.checked;
{ }
method: "POST", },
headers: { () => {
xhr: "xhr", t.checked = !t.checked;
}, },
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");
} }
const setting_switchs = document.getElementsByClassName('setting_switch'); const setting_switchs = document.getElementsByClassName('setting_switch');
@ -104,7 +86,7 @@ for (const element of setting_switchs) {
console.log("Nonce check failed!") console.log("Nonce check failed!")
continue continue
} }
element.addEventListener('change', handleSettingSwitch); element.addEventListener('change', () => {handleSettingSwitch(element)});
} }
const setting_selects = document.getElementsByClassName('setting_select'); const setting_selects = document.getElementsByClassName('setting_select');

View File

@ -32,7 +32,7 @@ function createXhrWithFormKey(url, 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, data, extraActionsOnSuccess) { function postToast(t, url, data, extraActionsOnSuccess, extraActionsOnFailure) {
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)) {
@ -57,6 +57,7 @@ function postToast(t, url, data, extraActionsOnSuccess) {
} }
showToast(success, message); showToast(success, message);
if (success && extraActionsOnSuccess) extraActionsOnSuccess(xhr[0]); if (success && extraActionsOnSuccess) extraActionsOnSuccess(xhr[0]);
if (!success && extraActionsOnFailure) extraActionsOnFailure(xhr[0]);
return success; return success;
}; };
xhr[0].send(xhr[1]); xhr[0].send(xhr[1]);