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) {
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');

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
}
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]);