diff options
author | LarsVomMars | 2021-01-28 12:53:19 +0100 |
---|---|---|
committer | LarsVomMars | 2021-01-28 15:25:53 +0100 |
commit | 2eff2deb98280aa61241e06163c87155911bbfae (patch) | |
tree | 92d2219abcb303fcade5649a6755e97ce5b51f26 /prediction/public/script.js | |
parent | 5feb9b1af9f6ff5b2e08bf9c7cc85ffc0f313945 (diff) |
TEACHER PREDICTION HAHAHA
Diffstat (limited to 'prediction/public/script.js')
-rw-r--r-- | prediction/public/script.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/prediction/public/script.js b/prediction/public/script.js new file mode 100644 index 0000000..a4d0527 --- /dev/null +++ b/prediction/public/script.js @@ -0,0 +1,37 @@ +const dropdown = document.getElementById("prediction"); +const submit = document.querySelector('button[type="submit"]'); +let method = "POST"; + +dropdown.insertAdjacentHTML( + "beforeend", + '<option disabled value="" selected="true" disabled>Lehrer auswählen...</option>', +); + +function appendOption(response) { + response.forEach((elem, i) => { + dropdown.insertAdjacentHTML( + "beforeend", + `<option value="${elem["id"]}">${elem["name"]} ${elem["middlename"] ? elem["middlename"] + " " : ""}${elem["surname"]}</option>`, + ); + }); +} + +function selectOption(response) { + if (Object.keys(response).length > 0) { + dropdown.value = response.teacher_id; + method = "PUT"; + } +} + +fetch("/auth/api/list?class=teacher") + .then((response) => response.json()) + .then((response) => appendOption(response)) + .then(() => fetch("api/get")) + .then((response) => response.json()) + .then(selectOption); + +submit.addEventListener("click", async (e) => { + const teacher = dropdown.value; + const body = JSON.stringify({ teacher }); + await fetch("api/set", { method, body, headers: { "Content-Type": "application/json" } }); +});
\ No newline at end of file |