summaryrefslogtreecommitdiffstats
path: root/js/teachers.js
blob: bb5137b36bd7bbe69f94764527f2eb1379088865 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const API_ENDPOINT = "https://gimb.tk/test.php";
// const API_ENDPOINT = "http://localhost:5000/test.php";

var teachers = null;

// Set loading bar visibility
function setLoading(state) {
    if (state) {
        $("#loading-bar").removeClass("hidden");
    } else {
        $("#loading-bar").addClass("hidden");
    }
}

// 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);
        }
    });
}

// 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();
}

async function checkLogin() {
    localforage.getItem("logged_in").then((value) => {
        // This code runs once the value has been loaded
        // from the offline store.
        if (value !== true) {
            window.location.replace("/index.html");
        }
    }).catch((err) => {
        // This code runs if there were any errors
        console.log(err);
    });
}

function clearTable() {
    const table = document.getElementById("teachers-body");
    while (table.firstChild) {
        table.removeChild(table.firstChild);
    }
}

function refreshTableClickHandlers() {
    // Arrow function doesn't work apparently?
    $('#teachers-body').find("tr").click(function () {
        teacherInfo($(this).index());
    });
}

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();
}

document.addEventListener("DOMContentLoaded", () => {
    checkLogin();
    loadTeachers();

    // Setup refresh handler
    $("#refresh-icon").click(() => {
        clearTable();
        loadTeachers(true);
    });

    // Set row onClick functions
    refreshTableClickHandlers();

    // Setup side menu
    const menus = document.querySelectorAll('.side-menu');
    M.Sidenav.init(menus, { edge: 'right', draggable: true });

    // Setup side modal
    const modals = document.querySelectorAll('.side-modal');
    M.Sidenav.init(modals, { edge: 'left', draggable: false });
});