diff options
Diffstat (limited to 'js/teachers.js')
-rw-r--r-- | js/teachers.js | 173 |
1 files changed, 77 insertions, 96 deletions
diff --git a/js/teachers.js b/js/teachers.js index bb5137b..c41a621 100644 --- a/js/teachers.js +++ b/js/teachers.js @@ -1,4 +1,4 @@ -const API_ENDPOINT = "https://gimb.tk/test.php"; +// const API_ENDPOINT = "https://gimb.tk/test.php"; // deprecated // const API_ENDPOINT = "http://localhost:5000/test.php"; var teachers = null; @@ -14,91 +14,71 @@ function setLoading(state) { // Function, responsible for fetching and displaying data async function loadTeachers(force_refresh = false) { - setLoading(true); - - // Load required data - let promises_to_run = [ - localforage.getItem("username").then((value) => { - username = value; - }), - localforage.getItem("password").then((value) => { - password = value; - }), - localforage.getItem("teachers").then((value) => { - teachers = value; - }) - ]; - - Promise.all(promises_to_run).then(() => { - - // If we don't have a list of teachers, query it - if (teachers === null || force_refresh) { - $.ajax({ - url: API_ENDPOINT, - crossDomain: true, - - data: { - "u": username, - "p": password, - "m": "fetchprofesorji" - }, - dataType: "json", - - cache: false, - type: "GET", - - success: (data) => { - // If data is null, the request failed - if (data === null) { - UIAlert(D("requestFailed")); - setLoading(false); - } else { - // Save teachers & populate table - localforage.setItem("teachers", data).then((value) => { - teachers = value; - displayData(); - setLoading(false); - }); - } - }, - - error: () => { - UIAlert(D("noInternetConnection")); - setLoading(false); - } - - }) - } else { - displayData(); - setLoading(false); - } - }); + setLoading(true); + // Load required data + let promises_to_run = [ + localforage.getItem("username").then((value) => { + username = value; + }), + localforage.getItem("password").then((value) => { + password = value; + }), + localforage.getItem("teachers").then((value) => { + teachers = value; + }) + ]; + await Promise.all(promises_to_run); + // If we don't have a list of teachers, query it + if (teachers === null || force_refresh) { + try { + let gsecInstance = new gsec(); + await gsecInstance.login(username, password); + gsecInstance.fetchTeachers().then( (value) => { + teachers = value; + localforage.setItem("teachers", value).then((value) => { + displayData(); + setLoading(false); + }); + setLoading(false); + }).catch( (err) => { + gsecErrorHandlerUI(err); + setLoading(false); + }); + } catch (err) { + gsecErrorHandlerUI(err); + setLoading(false); + } + } else { + displayData(); + setLoading(false); + } } // Function for displaying data function displayData() { - - teachers.forEach(element => { - // Create row - let row = document.createElement("tr"); - // Create cell 1 - let cell_name = document.createElement("td"); - let cell_name_text = document.createTextNode(element["ime"]); - // Create cell 2 - let cell_subject = document.createElement("td"); - // Array ([0]) is useless, since every teacher is duplicated (for each subject) - let cell_subject_text = document.createTextNode(element["predmeti"][0]["ime"]); - - cell_name.appendChild(cell_name_text); - row.appendChild(cell_name); - - cell_subject.appendChild(cell_subject_text); - row.appendChild(cell_subject); - - $("#teachers-body").append(row); - }); - // Refresh handlers - refreshTableClickHandlers(); + for(const teacher of Object.keys(teachers)) { + // Create row + let row = document.createElement("tr"); + // Create cell 1 + let cell_name = document.createElement("td"); + let cell_name_text = document.createTextNode(teacher); + // Create cell 2 + let cell_subject = document.createElement("td"); + // Array ([0]) is useless, since every teacher is duplicated (for each subject) // <-- ne velja za gsec.js, velja pa za gimsisextclient, PHP varianta + var subjectsString = ""; + for(const subject of Object.keys(teachers[teacher]["subjects"])) { + subjectsString += subject; + subjectsString += ", "; + } + let cell_subject_text = document.createTextNode(subjectsString.slice(0, -2)); // slajsnemo zadnji ", " + cell_name.appendChild(cell_name_text); + row.appendChild(cell_name); + cell_subject.appendChild(cell_subject_text); + row.appendChild(cell_subject); + $("#teachers-body").append(row); + }; + // Refresh handlers + refreshTableClickHandlers(); } async function checkLogin() { @@ -129,20 +109,21 @@ function refreshTableClickHandlers() { } function teacherInfo(teacher_id) { - let teacher_object = teachers[teacher_id]; - - let name = teacher_object["ime"]; - let subject = teacher_object["predmeti"][0]["ime"]; - let office_day = dateString.day(teacher_object["govorilneure"]["dan"]); - let office_lesson = teacher_object["govorilneure"]["solskaura"]; - - document.getElementById("teacher-name").innerText = name; - document.getElementById("teacher-subject").innerText = S("schoolSubject") + ": " + subject; - - document.getElementById("teacher-office").innerText = office_day + ", " + S("lesson") + " " + office_lesson; - - const modal = document.querySelectorAll('.side-modal')[0]; - M.Sidenav.getInstance(modal).open(); + let name = Object.keys(teachers)[teacher_id]; + let teacher_object = teachers[name]; + var subjectsString = ""; + for(const subject of Object.keys(teacher_object["subjects"])) { + subjectsString += subject; + subjectsString += ", "; + } + let subject = subjectsString.slice(0, -2); + let office_day = dateString.day(teacher_object["tpMeetings"]["day"]); + let office_lesson = teacher_object["tpMeetings"]["period"]; + document.getElementById("teacher-name").innerText = name; + document.getElementById("teacher-subject").innerText = S("schoolSubject") + ": " + subject; + document.getElementById("teacher-office").innerText = office_day + ", " + S("lesson") + " " + office_lesson; + const modal = document.querySelectorAll('.side-modal')[0]; + M.Sidenav.getInstance(modal).open(); } document.addEventListener("DOMContentLoaded", () => { |