mirror of
https://github.com/HendrikRauh/dmx-interface.git
synced 2025-07-05 13:48:53 +00:00
added script that formats and sends the input to the server
This commit is contained in:
parent
5d05d30c4d
commit
552cf376a0
2 changed files with 62 additions and 2 deletions
|
@ -5,12 +5,13 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Konfiguration</title>
|
||||
<link rel="stylesheet" href="/style.css" />
|
||||
<script src="/input-visibility.js" defer></script>
|
||||
<script type="module" src="/input-visibility.js" defer></script>
|
||||
<script type="module" src="/submit.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Konfiguration</h1>
|
||||
|
||||
<form action="" method="post">
|
||||
<form>
|
||||
<fieldset>
|
||||
<legend>Verbindung</legend>
|
||||
|
||||
|
@ -80,6 +81,8 @@
|
|||
type="checkbox"
|
||||
name="input-or-output-1"
|
||||
id="input-input-or-output-1"
|
||||
data-value-not-checked="output"
|
||||
data-value-checked="input"
|
||||
/>
|
||||
<span class="slider"></span>
|
||||
<span>Input</span>
|
||||
|
@ -104,6 +107,8 @@
|
|||
type="checkbox"
|
||||
name="input-or-output-2"
|
||||
id="input-input-or-output-2"
|
||||
data-value-not-checked="output"
|
||||
data-value-checked="input"
|
||||
/>
|
||||
<span class="slider"></span>
|
||||
<span>Input</span>
|
||||
|
|
55
data/submit.js
Normal file
55
data/submit.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
const form = document.querySelector("form");
|
||||
|
||||
function parseValue(input) {
|
||||
if (input.type === "checkbox") {
|
||||
return input.checked
|
||||
? input.dataset.valueChecked
|
||||
: input.dataset.valueNotChecked;
|
||||
}
|
||||
|
||||
if (input.value === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (input.type === "number") {
|
||||
const number = Number(input.value);
|
||||
return isNaN(number) ? null : number;
|
||||
}
|
||||
|
||||
return input.value;
|
||||
}
|
||||
|
||||
form.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
const inputFields = document.querySelectorAll(
|
||||
"form :is(input, select, textarea):not(:disabled)"
|
||||
);
|
||||
|
||||
const data = Array.from(inputFields).reduce((data, input) => {
|
||||
data[input.name] = parseValue(input);
|
||||
return data;
|
||||
}, {});
|
||||
console.log(data);
|
||||
|
||||
putData(data);
|
||||
});
|
||||
|
||||
async function putData(data) {
|
||||
try {
|
||||
const res = await fetch("/config", {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error(`Response status: ${res.status}`);
|
||||
}
|
||||
|
||||
const json = await res.json();
|
||||
console.log(json);
|
||||
} catch (error) {
|
||||
console.error(error.message);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue