refactor(frontend): remove duplicated code by defining make_table

This commit is contained in:
Mariano Riefolo 2024-09-05 15:01:04 +02:00
parent 99a81ba6a0
commit 32b247cfc5

View File

@ -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) {