orario-scolastico-itet/static/index.js

191 lines
5.7 KiB
JavaScript
Raw Normal View History

function load_links() {
fetch("/api/pdf")
.then((response) => response.json())
.then((data) => {
let i = 0;
data.links.forEach((el) => {
const select = document.getElementById("links");
const option = document.createElement("option");
option.value = i;
option.text = el.split("/").pop().split(".pdf")[0];
select.appendChild(option);
i++;
});
});
}
function load_teachers(id) {
fetch(`/api/professori?id=${id}`)
.then((response) => response.json())
.then((data) => {
sessionStorage.setItem("teachers", JSON.stringify(data));
let i = 0;
data.forEach((el) => {
const select = document.getElementById("teachers");
const option = document.createElement("option");
option.value = el;
select.appendChild(option);
i++;
});
});
}
function load_classes(id) {
fetch(`/api/classi?id=${id}`)
.then((response) => response.json())
.then((data) => {
for (let i = 0; i < data.length; i++) {
const select = document.getElementById("year");
const option = document.createElement("option");
option.value = i;
option.text = i + 1;
select.appendChild(option);
}
sessionStorage.setItem("classes", JSON.stringify(data));
});
}
function make_teacher_table(id, teacher) {
fetch(`/api/professore?id=${id}&professore=${teacher}`)
.then((response) => response.json())
.then((data) => {
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);
data[weekday].forEach((teacher_class) => {
let td = document.createElement("td");
td.textContent = teacher_class;
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
});
}
function make_student_table(id, student_class) {
fetch(`/api/classe?id=${id}&classe=${student_class}`)
.then((response) => response.json())
.then((data) => {
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);
data[weekday].forEach((student_class) => {
let td = document.createElement("td");
td.textContent = student_class;
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
});
}
function select_pdf() {
Cookies.set("id", document.getElementById("links").value);
document.getElementById("form-type").removeAttribute("hidden");
}
function select_type() {
let id = document.getElementById("links").value;
let value = document.getElementById("type").value;
if (value === "teacher") {
load_teachers(id);
document.getElementById("form-teacher").removeAttribute("hidden");
document.getElementById("form-classes").setAttribute("hidden", "");
} else if (value === "student") {
load_classes(id);
document.getElementById("form-classes").removeAttribute("hidden");
document.getElementById("form-teacher").setAttribute("hidden", "");
}
}
function load_section() {
let data = JSON.parse(sessionStorage.getItem("classes"));
let year = document.getElementById("year").value;
Object.keys(data[year]).forEach((el) => {
const select = document.getElementById("section");
const option = document.createElement("option");
option.value = el;
option.text = el;
select.appendChild(option);
});
}
function load_major() {
let data = JSON.parse(sessionStorage.getItem("classes"));
let year = document.getElementById("year").value;
let section = document.getElementById("section").value;
data[year][section].forEach((el) => {
const select = document.getElementById("major");
const option = document.createElement("option");
option.value = el;
option.text = el;
select.appendChild(option);
});
}
function select_teacher() {
let teacher = document.getElementById("teacher").value;
let teachers = JSON.parse(sessionStorage.getItem("teachers"));
if (!teachers.includes(teacher)) {
return false;
}
Cookies.set("type", "teacher");
Cookies.set("teacher", teacher);
}
function select_class() {
Cookies.set("type", "student");
let student_class = document.getElementById("year").value;
student_class += document.getElementById("section").value;
student_class += document.getElementById("major").value;
Cookies.set("class", student_class);
}
function delete_cookies() {
Cookies.remove("id");
Cookies.remove("type");
Cookies.remove("teacher");
Cookies.remove("class");
}
if (Cookies.get("id") && Cookies.get("type")) {
if (Cookies.get("type") === "teacher" && Cookies.get("teacher")) {
make_teacher_table(Cookies.get("id"), Cookies.get("teacher"));
document.getElementById("dashboard").removeAttribute("hidden");
} else if (Cookies.get("type") === "student" && Cookies.get("class")) {
make_student_table(Cookies.get("id"), Cookies.get("class"));
document.getElementById("dashboard").removeAttribute("hidden");
}
} else {
document.getElementById("forms").removeAttribute("hidden");
load_links();
}