blob: 6a2ee0aea53074633553a299f3996d5d56207621 [file] [log] [blame]
delefmeeb0a4702020-10-04 18:15:55 +02001const idsFormulari = {
2 room: "1063142948",
3 day: "2115504093",
4 begins: "1749141911",
5 ends: "1827359679",
6 rows: {
7 A: "208184485",
8 B: "1077148310",
9 C: "642851281",
10 D: "1686039024",
11 E: "697835787",
12 F: "1511799646",
13 G: "809853432",
14 H: "182597499",
15 I: "1890539481",
16 J: "529159478",
17 K: "1615241874",
18 L: "1334263875"
19 },
20 notes: "1600275159"
21};
22
23const formBaseUrl = "https://docs.google.com/forms/d/e/1FAIpQLSfT9o287VqLyhwR8LPdloAQWhuqCgA3NfdhgP5vb9_sVQHL-g/viewform";
24
delefmeb5506032020-10-04 19:19:27 +020025const MIN_HOUR = 8;
26const MAX_HOUR = 23; // Pels altres graus de la Facultat
27
delefmeeb0a4702020-10-04 18:15:55 +020028var final_JSON = {
29 "class": null,
30 "number": "",
31 "letter": ""
32};
33
34var current_section = "section-1";
35
36var repeated_subjects;
delefmeb5506032020-10-04 19:19:27 +020037var current_time;
38
delefmeeb0a4702020-10-04 18:15:55 +020039
40function fillInSummary() {
41 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
42 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
43
44 document.getElementById('subject-final').textContent = final_JSON.class.friendly_name || final_JSON.class.calendar_name;
45 document.getElementById('classroom-final').textContent = final_JSON.class.room;
delefmef6510222020-10-04 20:21:33 +020046 document.getElementById('date-final').textContent = formatDate(begins);
delefmeb5506032020-10-04 19:19:27 +020047 document.getElementById('time-final').textContent = formatTime(begins) + ' - ' + formatTime(ends);
delefmeeb0a4702020-10-04 18:15:55 +020048 document.getElementById('letter-final').textContent = final_JSON.letter;
49 document.getElementById('number-final').textContent = final_JSON.number;
50}
51
52function clickButton(element) {
53 var btn = element.currentTarget;
54 var parent = btn.parent;
55
56 if (parent == "subject-container") {
57 // Canvi de background del button
58 var selectedClass = JSON.parse(btn.getAttribute('data-class'));
59 $("#subject-container .complex-button").removeClass("is-link")
60 btn.classList.add("is-link");
61 // Canvi JSON
62 final_JSON["class"] = selectedClass;
63 // Missatge advertència classe repetida
64 if (repeated_subjects.has(selectedClass.id)) {
delefmeb5506032020-10-04 19:19:27 +020065 document.getElementById('repeated-subject-warning').classList.remove('is-hidden');
delefmeeb0a4702020-10-04 18:15:55 +020066 document.getElementById('repeated-subject-warning-class').textContent = selectedClass.room;
67 } else {
delefmeb5506032020-10-04 19:19:27 +020068 document.getElementById('repeated-subject-warning').classList.add('is-hidden');
delefmeeb0a4702020-10-04 18:15:55 +020069 }
70 // Anchor següent pregunta
71 switchSection("section-2");
72 } else if (parent == "number-container") {
73 // Canvi de background del button
74 $("#number-container .button").removeClass("is-link is-light is-active")
75 btn.classList.add("is-link", "is-light", "is-active");
76 // Canvi JSON
77 final_JSON["number"] = btn.getAttribute('data-number');
78 // Introducció de totes les dades al resum final
79 fillInSummary();
80 // Anchor següent pregunta
81 switchSection("section-send");
82 } else if (parent == "letter-container") {
83 // Canvi de background del button
84 $("#letter-container .button").removeClass("is-link is-light is-active")
85 btn.classList.add("is-link", "is-light", "is-active");
86 // Canvi JSON
87 final_JSON["letter"] = btn.getAttribute('data-letter');
88 // Anchor següent pregunta
89 switchSection("section-3");
90 }
91}
92
93function switchSection(s) {
94 setTimeout(function(){
delefmeb5506032020-10-04 19:19:27 +020095 document.getElementById(current_section).classList.add('is-hidden');
96 document.getElementById(s).classList.remove('is-hidden');
delefmeeb0a4702020-10-04 18:15:55 +020097 current_section = s;
98 }, 75);
99}
100
101function findRepeatedSubjects(classes) {
102 var rep = new Set();
103 for (var [i, classe] of classes.entries()) {
104 if (i > 0 && classes[i-1].calendar_name == classe.calendar_name) {
105 rep.add(classe.id);
106 rep.add(classes[i-1].id);
107 }
108 }
109 return rep;
110}
111
112function buildSubjectContainer(classes, repeated) {
113 var duplicateSubjectBoolNext, duplicateSubjectBoolPrev;
114 var duplicateSubjectCounter = 0;
115
delefme381067d2020-10-04 20:07:21 +0200116 // Flush existing classes
117 document.querySelectorAll('.message').forEach(function(classe) {
118 classe.classList.add('is-hidden');
119 });
120
Dani Vilardelld6ce7352020-10-04 22:50:40 +0200121 for (var [i, classe] of classes.entries()) {
122 console.log(classe);
delefme4f84bd52020-10-04 20:14:49 +0200123 var hora_inici = formatTime(new Date(parseInt(classe.begins)*1000));
124 var hora_final = formatTime(new Date(parseInt(classe.ends)*1000));
delefme44594a42020-10-04 22:23:32 +0200125 var classeDiv;
delefmeeb0a4702020-10-04 18:15:55 +0200126
127 // Check if the subject is repeated
Dani Vilardellc90f4dc2020-10-04 23:23:51 +0200128 if (i < classes.length - 1) duplicateSubjectBoolNext = classes[i+1].friendly_name == classe.friendly_name;
delefmef42ec322020-10-04 19:39:13 +0200129 else duplicateSubjectBoolNext = false;
Dani Vilardell4c803442020-10-04 23:22:50 +0200130 if (i > 0) duplicateSubjectBoolPrev = classes[i-1].friendly_name == classe.friendly_name;
delefmef42ec322020-10-04 19:39:13 +0200131 else duplicateSubjectBoolPrev = false;
delefmeeb0a4702020-10-04 18:15:55 +0200132
delefme44594a42020-10-04 22:23:32 +0200133 // Change the previous classeDiv
delefmef42ec322020-10-04 19:39:13 +0200134 if(duplicateSubjectBoolNext && duplicateSubjectCounter%2 == 1) {
delefmeeb0a4702020-10-04 18:15:55 +0200135 classeDiv.classList.add('message', 'complex-button-full');
Dani Vilardell00b10e22020-10-04 23:11:41 +0200136 duplicateSubjectCounter++;
delefmeeb0a4702020-10-04 18:15:55 +0200137 }
138
delefme44594a42020-10-04 22:23:32 +0200139 classeDiv = document.createElement('div');
140
delefmef42ec322020-10-04 19:39:13 +0200141 if (duplicateSubjectBoolPrev) {
delefmeeb0a4702020-10-04 18:15:55 +0200142 classeDiv.classList.add('message', 'complex-button2Right');
delefmef42ec322020-10-04 19:39:13 +0200143 } else if(duplicateSubjectBoolNext) {
delefmeeb0a4702020-10-04 18:15:55 +0200144 classeDiv.classList.add('message', 'complex-button2Left');
Dani Vilardelld6ce7352020-10-04 22:50:40 +0200145 console.log("hola");
delefmeeb0a4702020-10-04 18:15:55 +0200146 } else {
147 classeDiv.classList.add('message', 'complex-button');
148 }
149
150 classeDiv.classList.add('message', 'complex-button');
151 classeDiv.id = 'subject-' + classe.subject_id + '-' + classe.room;
152 classeDiv.setAttribute('data-class', JSON.stringify(classe));
153
154 var header = document.createElement('div');
155 header.classList.add('message-header');
156
delefmef42ec322020-10-04 19:39:13 +0200157 if (!(duplicateSubjectBoolPrev)) {
delefmeeb0a4702020-10-04 18:15:55 +0200158 header.textContent = classe.friendly_name || classe.calendar_name;
delefme44594a42020-10-04 22:23:32 +0200159 header.style.color = "#FFFFFF";
delefmeeb0a4702020-10-04 18:15:55 +0200160 } else {
161 header.textContent = classe.friendly_name || classe.calendar_name;;
162 header.style.color = "#4A4A4A";
163 }
164
165 var body = document.createElement('div');
166 body.classList.add('message-body');
167
168 var div1 = document.createElement('div');
169 var span = document.createElement('span');
170 span.textContent = classe.room;
171
172 div1.classList.add('has-text-weight-bold');
173
174 div1.textContent = 'Aula ';
175 div1.appendChild(span);
176
177 var div2 = document.createElement('div');
178 div2.textContent = hora_inici + ' - ' + hora_final;
179
180 body.appendChild(div1);
181 body.appendChild(div2);
182
183 classeDiv.appendChild(header);
184 classeDiv.appendChild(body);
185
186 document.getElementById("subject-container").appendChild(classeDiv);
187 ++duplicateSubjectCounter;
188 }
delefmeb5506032020-10-04 19:19:27 +0200189
delefmeeb0a4702020-10-04 18:15:55 +0200190 var elements = document.getElementsByClassName("button");
191 Array.from(elements).forEach(function(element) {
192 element.addEventListener('click', clickButton);
193 element.parent = element.parentNode.id;
194 });
195 var elements = document.getElementsByClassName("complex-button");
196 Array.from(elements).forEach(function(element) {
197 element.addEventListener('click', clickButton);
198 element.parent = element.parentNode.id;
199 });
200}
201
delefmeb5506032020-10-04 19:19:27 +0200202function getDefaultTime() {
203 var time = new Date();
204 time.setSeconds(0);
205 time.setMilliseconds(0);
206 if (time.getMinutes() < 30) time.setMinutes(0);
207 else time.setMinutes(30);
208 if (time.getHours() < MIN_HOUR) {
209 time.setHours(MIN_HOUR);
210 time.setMinutes(0);
211 }
212 if (time.getHours() >= MAX_HOUR) {
213 time.setHours(MAX_HOUR - 1);
214 time.setMinutes(30);
215 }
216 return time
217}
218
219function buildTimeSelector(date) {
220 document.getElementById("date-selector").value = formatDate(date);
221 var end_time = new Date(date.getTime() + 30*60000); // 1 min = 60000 ms
222 document.getElementById("time-selector").value = formatTime(date) + " - " + formatTime(end_time);
223}
224
225function addDateEventListeners(date) {
226 document.getElementById("date-prev").addEventListener('click', function (el) {
227 current_time = new Date(current_time.getTime() - 24*60*60000);
228 buildTimeSelector(current_time);
229 fetchClasses();
230 });
231 document.getElementById("date-next").addEventListener('click', function (el) {
232 current_time = new Date(current_time.getTime() + 24*60*60000);
233 buildTimeSelector(current_time);
234 fetchClasses();
235 });
236 document.getElementById("time-prev").addEventListener('click', function (el) {
237 current_time = new Date(current_time.getTime() - 30*60000);
238 if (current_time.getHours() < MIN_HOUR) {
239 current_time = new Date(current_time.getTime() - 24*60*60000);
240 current_time.setHours(MAX_HOUR - 1);
241 current_time.setMinutes(30);
242 }
243 buildTimeSelector(current_time);
244 fetchClasses();
245 });
246 document.getElementById("time-next").addEventListener('click', function (el) {
247 current_time = new Date(current_time.getTime() + 30*60000);
248 if (current_time.getHours() >= MAX_HOUR) {
249 current_time = new Date(current_time.getTime() + 24*60*60000);
250 current_time.setHours(MIN_HOUR);
251 current_time.setMinutes(0);
252 }
253 buildTimeSelector(current_time);
254 fetchClasses();
255 });
256}
257
258function formatTime(d) {
259 return d.toLocaleTimeString("ca", {timeStyle: 'short'});
260 /* var str = "";
delefmeeb0a4702020-10-04 18:15:55 +0200261 str += d.getHours();
262 str += ":";
263 if (d.getMinutes() < 10) str += "0";
delefmeb5506032020-10-04 19:19:27 +0200264 str += d.getMinutes();
265 return str; */
266}
267
268function formatDate(d) {
269 return d.toLocaleDateString("ca");
270}
271
272function fetchClasses() {
273 console.log(api_url + "getClassesInTime/" + current_time.getTime()/1000);
274 fetch(api_url + "getClassesInTime/" + current_time.getTime()/1000, {
275 "mode": "cors",
276 "credentials": "include"
277 })
278 .then(response => response.json())
279 .then(data => {
280 if (data.payload.classes.length == 0) {
281 document.getElementById('no-subjects').classList.remove('is-hidden');
282 document.getElementById('subject-container').classList.add('is-hidden');
283 document.getElementById('fme-maps-container').classList.add('is-hidden');
284 } else {
285 repeated_subjects = findRepeatedSubjects(data.payload.classes);
286 buildSubjectContainer(data.payload.classes, repeated_subjects);
287 document.getElementById('no-subjects').classList.add('is-hidden');
288 document.getElementById('subject-container').classList.remove('is-hidden');
289 document.getElementById('fme-maps-container').classList.remove('is-hidden');
290 }
291
292 });
delefmeeb0a4702020-10-04 18:15:55 +0200293}
294
295function onPageLoad() {
296
delefmeb5506032020-10-04 19:19:27 +0200297 // Check if user is signed in
delefmeeb0a4702020-10-04 18:15:55 +0200298 if (localStorage.getItem('devMode') == 'true') {
299 var banner = document.getElementById('dev-mode');
300 banner.addEventListener('click', _ => {
301 localStorage.devMode = 'false';
302 location.reload();
303 });
delefmeb5506032020-10-04 19:19:27 +0200304 banner.classList.remove('is-hidden');
delefmeeb0a4702020-10-04 18:15:55 +0200305 api_url = localStorage.getItem('apiUrl') || 'https://covid-tracability-backend-dev.sandbox.avm99963.com/api/v1/'
306 } else {
307 api_url = "https://covid-tracability-backend-prod.sandbox.avm99963.com/api/v1/";
308 }
delefmeb5506032020-10-04 19:19:27 +0200309
310 current_time = getDefaultTime();
311 buildTimeSelector(current_time);
312
delefmeeb0a4702020-10-04 18:15:55 +0200313 fetch(api_url + "isSignedIn", {
314 "mode": "cors",
315 "credentials": "include"
316 })
317 .then(response => response.json())
318 .then(data => {
319 if (!data.payload.signedIn) {
320 console.log("Not signed in!");
321 fetch(api_url + "getAuthUrl", {
322 "mode": "cors",
323 "credentials": "include"
324 })
325 .then(response => response.json())
326 .then(data => {
327 // TODO: redirect here
328 // location.href = data.payload.url;
329 console.warn('Log in here: ', data.payload.url);
330 });
331 }
332 });
333
delefmeb5506032020-10-04 19:19:27 +0200334 fetchClasses();
delefmeeb0a4702020-10-04 18:15:55 +0200335}
336
337function sendForm() {
338 // Add subject to user
339 fetch(api_url + "addUserSubject", {
340 "method": "POST",
341 "body": JSON.stringify({
342 subject: final_JSON.class.subject_id
343 }),
344 "mode": "cors",
345 "credentials": "include"
346 })
347 .then(res => res.json())
348 .then(json => {
349 console.log("Subject added to user: ", json);
350
351 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
352 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
353
354 var params = new URLSearchParams();
355 params.append("entry." + idsFormulari.room, final_JSON.class.room); // class, number, letter
356 params.append("entry." + idsFormulari.day, begins.getFullYear().toString() + '-' + (begins.getMonth() + 1).toString().padStart(2, "0") + '-' + begins.getDate().toString().padStart(2, "0"));
delefmeb5506032020-10-04 19:19:27 +0200357 params.append("entry." + idsFormulari.begins, formatTime(begins));
358 params.append("entry." + idsFormulari.ends, formatTime(ends));
delefmeeb0a4702020-10-04 18:15:55 +0200359 params.append("entry." + idsFormulari.rows[final_JSON.letter], 'Columna ' + final_JSON.number);
360 // params.append("entry." + idsFormulari.notes, '[Autogenerat per delefme/covid-tracability -- Assignatura seleccionada: ' + (final_JSON.class.friendly_name || final_JSON.class.calendar_name) + ']');
361
362 var formulari_link = formBaseUrl + '?' + params.toString() + '#i1';
363 window.location.href = formulari_link;
364 });
365}
366
367
368function addEventListeners() {
369 window.addEventListener('load', onPageLoad);
370
371 var elements = document.getElementsByClassName("button");
372 Array.from(elements).forEach(function(element) {
373 element.addEventListener('click', clickButton);
374 element.parent = element.parentNode.id;
375 });
376
377 var elements = document.getElementsByClassName("complex-button");
378 Array.from(elements).forEach(function(element) {
379 element.addEventListener('click', clickButton);
380 element.parent = element.parentNode.id;
381 });
382
383 document.getElementById("send-button").addEventListener('click', function (el) {
384 document.getElementById("send-button").classList.add('is-loading');
385 sendForm();
386 });
delefmeb5506032020-10-04 19:19:27 +0200387
388 addDateEventListeners();
delefmeeb0a4702020-10-04 18:15:55 +0200389}
390
391addEventListeners();