added script for loading the config

This commit is contained in:
RaffaelW 2024-10-26 23:03:17 +02:00
parent 552cf376a0
commit 115442f91e
2 changed files with 36 additions and 0 deletions

View file

@ -6,6 +6,7 @@
<title>Konfiguration</title>
<link rel="stylesheet" href="/style.css" />
<script type="module" src="/input-visibility.js" defer></script>
<script type="module" src="/load-data.js" defer></script>
<script type="module" src="/submit.js" defer></script>
</head>
<body>

35
data/load-data.js Normal file
View file

@ -0,0 +1,35 @@
const form = document.querySelector("form");
async function loadData() {
try {
const req = await fetch("/config", {
method: "GET",
});
if (!req.ok) {
throw new Error(`Response status: ${req.status}`);
}
const json = await req.json();
console.log(json);
return json;
} catch (error) {
console.log(error);
return null;
}
}
function writeDataToInput(data) {
console.log("write data", typeof data);
for (const [key, value] of Object.entries(data)) {
const element = document.querySelector(`[name=${key}]`);
console.log(element);
element.value = value;
}
// send "change" event
form.dispatchEvent(new Event("change", { bubbles: true }));
}
const data = await loadData();
if (data !== null) {
writeDataToInput(data);
}