From 32b247cfc588fa6d3489ada9d3bb13f108bc2ad0 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Thu, 5 Sep 2024 15:01:04 +0200 Subject: [PATCH] refactor(frontend): remove duplicated code by defining make_table --- static/index.js | 59 +++++-------------------------------------------- 1 file changed, 6 insertions(+), 53 deletions(-) diff --git a/static/index.js b/static/index.js index 707d192..ca4bbf4 100644 --- a/static/index.js +++ b/static/index.js @@ -68,62 +68,15 @@ function load_classes(id) { } function make_teacher_table(id, teacher) { - fetch(`/api/professore?id=${id}&professore=${teacher}`) - .then((response) => response.json()) - .then((data) => { - for (let key in data) { - let i = data[key].length - 1; - while (i >= 0 && data[key][i] === "") { - data[key].pop(); - i--; - } - } - const columns = Math.max( - ...Object.values(data).map((vettore) => vettore.length), - ); - let table = document.getElementById("table"); - let tbody = document.createElement("tbody"); - let week = [ - "Lunedì", - "Martedì", - "Mercoledì", - "Giovedì", - "Venerdì", - "Sabato", - ]; - week.forEach((weekday) => { - let tr = document.createElement("tr"); - let th = document.createElement("th"); - th.scope = "row"; - th.textContent = weekday; - tr.appendChild(th); - if (data[weekday] != null) { - let inserted = 0; - data[weekday].forEach((teacher_class) => { - let td = document.createElement("td"); - td.textContent = teacher_class; - tr.appendChild(td); - inserted++; - }); - while (inserted < columns) { - let td = document.createElement("td"); - tr.appendChild(td); - inserted++; - } - tbody.appendChild(tr); - } - }); - table.appendChild(tbody); - document.getElementById("table").removeAttribute("aria-busy"); - }) - .catch((error) => { - alert("Errore nel caricamento dei dati, riprova più tardi."); - console.error("Error:", error); - }); + make_table(`/api/professore?id=${id}&professore=${teacher}`); } function make_student_table(id, student_class) { - fetch(`/api/classe?id=${id}&classe=${student_class}`) + make_table(`/api/classe?id=${id}&classe=${student_class}`); +} + +function make_table(url) { + fetch(url) .then((response) => response.json()) .then((data) => { for (let key in data) {