1
0
mirror of https://github.com/thomiceli/opengist synced 2024-11-12 07:05:50 +01:00
opengist/public/admin.ts
2023-09-22 17:31:19 +02:00

36 lines
1.2 KiB
TypeScript

document.addEventListener('DOMContentLoaded', () => {
let elems = Array.from(document.getElementsByClassName("toggle-button"));
for (let elem of elems) {
elem.addEventListener('click', () => {
registerDomSetting(elem as HTMLElement)
})
}
});
const setSetting = (key: string, value: string) => {
const data = new URLSearchParams();
data.append('key', key);
data.append('value', value);
if (document.getElementsByName('_csrf').length !== 0) {
data.append('_csrf', ((document.getElementsByName('_csrf')[0] as HTMLInputElement).value));
}
return fetch('/admin-panel/set-config', {
method: 'PUT',
credentials: 'same-origin',
body: data,
});
};
const registerDomSetting = (el: HTMLElement) => {
// @ts-ignore
el.dataset["bool"] = !(el.dataset["bool"] === 'true');
setSetting(el.id, el.dataset["bool"] === 'true' ? '1' : '0')
.then(() => {
el.classList.toggle("bg-primary-600");
el.classList.toggle("dark:bg-gray-400");
el.classList.toggle("bg-gray-300");
(el.childNodes.item(1) as HTMLElement).classList.toggle("translate-x-5");
});
};