-
Notifications
You must be signed in to change notification settings - Fork 3
/
scheduleDay.js
248 lines (224 loc) · 8.49 KB
/
scheduleDay.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
let docTitle = document.title;
window.addEventListener("blur", () => {
document.title = "Rooster Klascal";
});
window.addEventListener("focus", () => {
document.title = docTitle;
});
// Dutch month names
const dutchMonthNames = [
"januari",
"februari",
"maart",
"april",
"mei",
"juni",
"juli",
"augustus",
"september",
"oktober",
"november",
"december",
];
// Functie om de access token te verkrijgen door middel van de koppelcode.
async function fetchToken(authorizationCode, schoolName) {
try {
const url = `https://${schoolName}.zportal.nl/api/v3/oauth/token?grant_type=authorization_code&code=${authorizationCode}`;
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
console.log(response);
if (!response.ok) {
throw new Error(`Error ${response.status}: ${response.statusText}`);
}
const parsedresp = await response.json();
console.log(parsedresp);
const accessToken = parsedresp["access_token"];
return accessToken;
} catch (error) {
console.error("Error fetching acces token:", error.message);
displayError("Error fetching acces token. Please try again.");
}
}
// Function to fetch appointments for the specified date
async function fetchAppointments(date) {
const [day, monthName] = date.split(" ");
const monthIndex = dutchMonthNames.findIndex(
(month) => month.toLowerCase() === monthName.toLowerCase()
);
if (monthIndex === -1 || isNaN(parseInt(day))) {
console.error(
"Invalid date format. Please enter date in the format 'DD Month', e.g., '12 augustus'."
);
return;
}
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const startDate = new Date(currentYear, monthIndex, parseInt(day, 10));
const endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 1);
const user = document.getElementById("user").value;
const schoolName = document.getElementById("schoolName").value;
const authorizationCode = document
.getElementById("authorizationCode")
.value.replace(/\s/g, "");
const startTimestamp = Math.floor(startDate.getTime() / 1000);
const endTimestamp = Math.floor(endDate.getTime() / 1000);
let accessToken = localStorage.getItem("access_token");
if (accessToken == null || accessToken === "undefined") {
accessToken = await fetchToken(authorizationCode, schoolName);
localStorage.setItem("access_token", accessToken);
}
const apiUrl = `https://${schoolName}.zportal.nl/api/v3/appointments?user=${user}&start=${startTimestamp}&end=${endTimestamp}&valid=true&fields=subjects,cancelled,locations,startTimeSlot,start,end,groups,teachers,changeDescription&access_token=${accessToken}`;
fetch(apiUrl)
.then((response) => response.json())
.then((data) => {
const appointments = data.response.data;
appointments.sort((a, b) => a.start - b.start);
const scheduleDiv = document.getElementById("schedule");
scheduleDiv.innerHTML = "";
const errorMessageDiv = document.getElementById("error-message");
if (appointments.length === 0) {
scheduleDiv.style.display = "none";
errorMessageDiv.style.display = "block";
} else {
scheduleDiv.style.display = "block";
errorMessageDiv.style.display = "none";
}
const filteredAppointments = filterCancelledLessons(appointments);
filteredAppointments.forEach((appointment) => {
const startTime = new Date(appointment.start * 1000);
const endTime = new Date(appointment.end * 1000);
const startTimeString = startTime.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
});
const endTimeString = endTime.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
});
const subjectsMapping = {
ak: "Aardrijkskunde",
en: "Engels",
fa: "Frans",
gd: "Godsdienst",
gs: "Geschiedenis",
ha: "Handvaardigheid",
ict: "Informatiekunde",
in: "Informatiekunde",
kcv: "Klassieke Culturele Vorming",
lo: "Lichamelijke opvoeding",
men: "Mentorles",
mn: "Mens en natuur",
mu: "Muziek",
ne: "Nederlands",
te: "Tekenen/Techniek",
wi: "Wiskunde",
wis: "Wiskunde",
fr: "Frans",
nl: "Nederlands",
du: "Duits",
bi: "Biologie",
na: "Natuurkunde",
nat: "Natuurkunde",
sk: "Scheikunde",
nask: "Natuurkunde/Scheikunde",
ec: "Economie",
econ: "Economie",
ma: "Maatschappijleer",
maat: "Maatschappijleer",
be: "Beeldende Vorming",
kv: "Kunstvakken",
fi: "Filosofie",
la: "Latijn",
gr: "Grieks",
PROJECT: "Project",
rkn: "Rekentoets",
pe: "Physical education",
ontw: "Ontwerpen",
ltc: "Latijn",
gtc: "Grieks",
entl: "Engels",
ges: "Geschiedenis",
fatl: "Frans",
biol: "Biologie",
netl: "Nederlands",
gds: "Godsdienst",
wis: "Wiskunde",
die: "Diëtetiek",
kubv: "Kunst Beeldende Vakken",
bg: "Begeleiding",
sp: "Spaans",
BSA: "Bindend studieadvies",
bsa: "Bindend studieadvies",
bo: "Bewegingsonderwijs",
glstnl: "Global Studies NL",
nlt: "Natuur leven technologie",
wisb: "Wiskunde B",
};
const subjectsFullNames = appointment.subjects.map(
(subject) => subjectsMapping[subject] || subject
);
let changeDescription = "";
if (appointment.changeDescription) {
changeDescription = `<p>${appointment.changeDescription}</p>`;
}
const appointmentDiv = document.createElement("div");
appointmentDiv.innerHTML = `
<p><strong id="vaknaam">${subjectsFullNames.join(
", "
)}</strong><strong style="position:absolute;right:25px;">${
appointment.startTimeSlot
}</strong></p>
<p>${startTimeString} - ${endTimeString} <span style="margin-left: 10px;">${appointment.locations.join(
", "
)} (${appointment.teachers.join(", ")})</span></p>
<p class="className">${appointment.groups.join(", ")}</p>
<p>
${
appointment.changeDescription
? '<svg width="24" height="24" fill="none" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" style="vertical-align: sub;"><path d="M10.909 2.782a2.25 2.25 0 0 1 2.975.74l.083.138 7.759 14.009a2.25 2.25 0 0 1-1.814 3.334l-.154.006H4.242A2.25 2.25 0 0 1 2.2 17.812l.072-.143L10.03 3.66a2.25 2.25 0 0 1 .879-.878ZM12 16.002a.999.999 0 1 0 0 1.997.999.999 0 0 0 0-1.997Zm-.002-8.004a1 1 0 0 0-.993.884L11 8.998 11 14l.007.117a1 1 0 0 0 1.987 0l.006-.117L13 8.998l-.007-.117a1 1 0 0 0-.994-.883Z" fill="#ff9800"/></svg>'
: ""
}
${appointment.changeDescription}
</p>
`;
if (appointment.cancelled === true) {
appointmentDiv.classList.add("cancelled");
}
scheduleDiv.appendChild(appointmentDiv);
});
})
.catch((error) => console.error("Error fetching data:", error));
}
// Function to filter out cancelled lessons if there are multiple lessons for the same hour
function filterCancelledLessons(appointments) {
const filteredAppointments = [];
appointments.forEach((appointment) => {
const existingAppointment = filteredAppointments.find(
(appt) =>
appt.start === appointment.start &&
appt.startTimeSlot === appointment.startTimeSlot
);
if (existingAppointment) {
if (!appointment.cancelled) {
const index = filteredAppointments.indexOf(existingAppointment);
filteredAppointments[index] = appointment;
}
} else {
filteredAppointments.push(appointment);
}
});
return filteredAppointments;
}
// Event listener for form submission
document
.getElementById("schedule-form")
.addEventListener("submit", function (event) {
event.preventDefault();
const date = document.getElementById("date").value;
fetchAppointments(date);
});