Create new function for handling setting switches

The new function properly undos failed updates to setting toggles and
also shows and hides the permanent option with the slur/profanity
filters appropriately.
pull/180/head
KindaCrayCray 2023-08-06 15:39:51 -04:00
parent 0fcef3f556
commit 0f2a1a9459
3 changed files with 53 additions and 52 deletions

View File

@ -64,40 +64,51 @@ 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;
}
let oldToast = bootstrap.Toast.getOrCreateInstance(
document.getElementById('toast-post-' + (res.ok ? 'error': 'success'))
); // intentionally reversed here: this is the old toast
oldToast.hide();
showToast(res.ok, message);
input.disabled = false;
input.classList.remove("disabled");
}
const setting_switchs = document.getElementsByClassName('setting_switch');
for (const element of setting_switchs) {
console.log(element);
if (element.dataset.nonce != nonce) {
console.log("Nonce check failed!")
continue
}
element.addEventListener('change', async (event) => {
element.disabled = true;
element.classList.add("disabled");
const form = new FormData();
form.append("formkey", formkey());
const res = await fetch(`/settings/personal?${element.name}=${element.checked}`, {
method: "POST",
headers: {
xhr: "xhr",
},
body: form,
}).catch(() => ({ok: false}));
let message;
if (res.ok) {
({message} = await res.json());
} else {
// toggle the input back if the request doesn't go through
element.checked = !element.checked;
}
let oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (res.ok ? 'error': 'success'))); // intentionally reversed here: this is the old toast
oldToast.hide();
showToast(res.ok, message);
element.disabled = false;
element.classList.remove("disabled");
// postToastSwitch(element,`/settings/personal?${element.name}=${element.checked}`);
});
element.addEventListener('change', handleSettingSwitch);
}
const setting_selects = document.getElementsByClassName('setting_select');

View File

@ -98,14 +98,3 @@ function updatebgselection(){
}
}
updatebgselection();
const sr_toggle = document.getElementById("slurreplacer");
const sr_link = document.getElementById('slurreplacer-perma-link');
const pr_toggle = document.getElementById("profanityreplacer");
const pr_link = document.getElementById('profanityreplacer-perma-link');
sr_toggle.addEventListener('change', function () {
sr_link.hidden = !sr_toggle.checked;
});
pr_toggle.addEventListener('change', function () {
pr_link.hidden = !pr_toggle.checked;
});

View File

@ -247,7 +247,7 @@
{{common.text_area_section('profile-signature', '/settings/personal', 'sig', 'Signature', v.sig, 'Limit of 200 characters', 'Enter a signature...', true, false, 200)}}
{{common.toggle_section('Private Mode', 'privateswitch', 'private', v.is_private, 'This will hide your profile page from others.', false)}}
{{common.toggle_section('Lifetime donated visiblity', 'lifetimedonated_visible', 'lifetimedonated_visible', v.lifetimedonated_visible, 'Make the "lifetime donated" field in your profile visible to everyone instead of just you.', false)}}
{{common.toggle_section('Lifetime donated visiblity', 'lifetimedonated_visibleswitch', 'lifetimedonated_visible', v.lifetimedonated_visible, 'Make the "lifetime donated" field in your profile visible to everyone instead of just you.', false)}}
{{common.toggle_section('Cursor Marsey', 'cursormarseyswitch', 'cursormarsey', v.cursormarsey, 'Have a cute
marsey accompany you during your journey on the site.', false)}}
@ -270,22 +270,27 @@
<h5>Filters</h5>
<div class="settings-section rounded" id="site-settings-filters">
{% set ns = namespace(slurtext='Enable if you would like to automatically replace slurs.', profanitytext='Enable if you would like to automatically replace profanities.') %}
{% if FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.slurreplacer %}
{% if v.slurreplacer == 1 %}
{% set ns.slurtext = 'Enable if you would like to automatically replace slurs. <a id="slurreplacer-perma-link" href="#" class="text-primary" data-bs-toggle="modal" data-bs-target="#modal-slurreplacer">Make filter permanent for a badge!</a>' %}
{% if FEATURES['USERS_PERMANENT_WORD_FILTERS'] %}
{% if v.slurreplacer == 0 %}
{% set ns.slurtext = 'Enable if you would like to automatically replace slurs. <a hidden id="slurreplacer-perma-link" href="#" class="text-primary" data-bs-toggle="modal" data-bs-target="#modal-slurreplacer">Make filter permanent for a badge!</a>' %}
{% elif v.slurreplacer == 1 %}
{% set ns.slurtext = 'Enable if you would like to automatically replace slurs. <a id="slurreplacer-perma-link" href="#" class="text-primary" data-bs-toggle="modal" data-bs-target="#modal-slurreplacer">Make filter permanent for a badge!</a>' %}
{% else %}
{% set ns.slurtext = "You've enabled the slur replacer permanently! ✊🏿" %}
{% endif %}
{% endif %}
{% if FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.profanityreplacer %}
{% if v.profanityreplacer == 1 %}
{% set ns.profanitytext = 'Enable if you would like to automatically replace profanities. <a id="profanityreplacer-perma-link" href="#" class="text-primary" data-bs-toggle="modal" data-bs-target="#modal-profanityreplacer">Make filter permanent for a badge!</a>' %}
{% if FEATURES['USERS_PERMANENT_WORD_FILTERS'] %}
{% if v.profanityreplacer == 0 %}
{% set ns.profanitytext = 'Enable if you would like to automatically replace profanities. <a hidden id="profanityreplacer-perma-link" href="#" class="text-primary" data-bs-toggle="modal" data-bs-target="#modal-profanityreplacer">Make filter permanent for a badge!</a>' %}
{% elif v.profanityreplacer == 1 %}
{% set ns.profanitytext = 'Enable if you would like to automatically replace profanities. <a id="profanityreplacer-perma-link" href="#" class="text-primary" data-bs-toggle="modal" data-bs-target="#modal-profanityreplacer">Make filter permanent for a badge!</a>' %}
{% else %}
{% set ns.profanitytext = "You've enabled the profanity replacer permanently! 😇" %}
{% endif %}
{% endif %}
{{common.toggle_section("Slur Replacer", "slurreplacer", 'slurreplacer', v.slurreplacer, ns.slurtext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.slurreplacer > 1)}}
{{common.toggle_section("Profanity Replacer", "profanityreplacer", 'profanityreplacer', v.profanityreplacer, ns.profanitytext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.profanityreplacer > 1)}}
{{common.toggle_section("Slur Replacer", "slurreplacerswitch", 'slurreplacer', v.slurreplacer, ns.slurtext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.slurreplacer > 1)}}
{{common.toggle_section("Profanity Replacer", "profanityreplacerswitch", 'profanityreplacer', v
.profanityreplacer, ns.profanitytext, FEATURES['USERS_PERMANENT_WORD_FILTERS'] and v.profanityreplacer > 1)}}
</div>
</section>
<section id="site-settings-referral-section" class="settings-section-section">
@ -354,10 +359,6 @@
</div>
{%- endif %}
{% endmacro %}
{% if v.slurreplacer == 1 -%}
{{permanent_filter_modal('slurreplacer', '/settings/personal', 'slurreplacer', 'Slur Replacer', 'Social Justice Berserker')}}
{%- endif %}
{% if v.profanityreplacer == 1 -%}
{{permanent_filter_modal('profanityreplacer', '/settings/personal', 'profanityreplacer', 'Profanity Replacer', 'Soapy-Mouthed Angel')}}
{%- endif %}
{% endblock %}