blob: 2c80793de5b80631807d05c0e1969c9cf21c822e [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 Vilardelle70f23b2020-10-04 23:38:47 +0200128 if (i < classes.length - 1) {
129 duplicateSubjectBoolNext = classes[i+1].friendly_name == classe.friendly_name;
130 }
131 else {
132 duplicateSubjectBoolNext = false;
133 }
134 if (i > 0) {
135 duplicateSubjectBoolPrev = classes[i-1].friendly_name == classe.friendly_name;
136 }
137 else {
138 duplicateSubjectBoolPrev = false;
139 }
delefmeeb0a4702020-10-04 18:15:55 +0200140
delefme44594a42020-10-04 22:23:32 +0200141 // Change the previous classeDiv
delefmef42ec322020-10-04 19:39:13 +0200142 if(duplicateSubjectBoolNext && duplicateSubjectCounter%2 == 1) {
delefmeeb0a4702020-10-04 18:15:55 +0200143 classeDiv.classList.add('message', 'complex-button-full');
Dani Vilardell00b10e22020-10-04 23:11:41 +0200144 duplicateSubjectCounter++;
delefmeeb0a4702020-10-04 18:15:55 +0200145 }
146
delefme44594a42020-10-04 22:23:32 +0200147 classeDiv = document.createElement('div');
148
delefmef42ec322020-10-04 19:39:13 +0200149 if (duplicateSubjectBoolPrev) {
delefmeeb0a4702020-10-04 18:15:55 +0200150 classeDiv.classList.add('message', 'complex-button2Right');
delefmef42ec322020-10-04 19:39:13 +0200151 } else if(duplicateSubjectBoolNext) {
delefmeeb0a4702020-10-04 18:15:55 +0200152 classeDiv.classList.add('message', 'complex-button2Left');
Dani Vilardelld6ce7352020-10-04 22:50:40 +0200153 console.log("hola");
delefmeeb0a4702020-10-04 18:15:55 +0200154 } else {
155 classeDiv.classList.add('message', 'complex-button');
156 }
157
158 classeDiv.classList.add('message', 'complex-button');
159 classeDiv.id = 'subject-' + classe.subject_id + '-' + classe.room;
160 classeDiv.setAttribute('data-class', JSON.stringify(classe));
161
162 var header = document.createElement('div');
163 header.classList.add('message-header');
164
delefmef42ec322020-10-04 19:39:13 +0200165 if (!(duplicateSubjectBoolPrev)) {
delefmeeb0a4702020-10-04 18:15:55 +0200166 header.textContent = classe.friendly_name || classe.calendar_name;
delefme44594a42020-10-04 22:23:32 +0200167 header.style.color = "#FFFFFF";
delefmeeb0a4702020-10-04 18:15:55 +0200168 } else {
169 header.textContent = classe.friendly_name || classe.calendar_name;;
170 header.style.color = "#4A4A4A";
171 }
172
173 var body = document.createElement('div');
174 body.classList.add('message-body');
175
176 var div1 = document.createElement('div');
177 var span = document.createElement('span');
178 span.textContent = classe.room;
179
180 div1.classList.add('has-text-weight-bold');
181
182 div1.textContent = 'Aula ';
183 div1.appendChild(span);
184
185 var div2 = document.createElement('div');
186 div2.textContent = hora_inici + ' - ' + hora_final;
187
188 body.appendChild(div1);
189 body.appendChild(div2);
190
191 classeDiv.appendChild(header);
192 classeDiv.appendChild(body);
193
194 document.getElementById("subject-container").appendChild(classeDiv);
195 ++duplicateSubjectCounter;
196 }
delefmeb5506032020-10-04 19:19:27 +0200197
delefmeeb0a4702020-10-04 18:15:55 +0200198 var elements = document.getElementsByClassName("button");
199 Array.from(elements).forEach(function(element) {
200 element.addEventListener('click', clickButton);
201 element.parent = element.parentNode.id;
202 });
203 var elements = document.getElementsByClassName("complex-button");
204 Array.from(elements).forEach(function(element) {
205 element.addEventListener('click', clickButton);
206 element.parent = element.parentNode.id;
207 });
208}
209
delefmeb5506032020-10-04 19:19:27 +0200210function getDefaultTime() {
211 var time = new Date();
212 time.setSeconds(0);
213 time.setMilliseconds(0);
214 if (time.getMinutes() < 30) time.setMinutes(0);
215 else time.setMinutes(30);
216 if (time.getHours() < MIN_HOUR) {
217 time.setHours(MIN_HOUR);
218 time.setMinutes(0);
219 }
220 if (time.getHours() >= MAX_HOUR) {
221 time.setHours(MAX_HOUR - 1);
222 time.setMinutes(30);
223 }
224 return time
225}
226
227function buildTimeSelector(date) {
228 document.getElementById("date-selector").value = formatDate(date);
229 var end_time = new Date(date.getTime() + 30*60000); // 1 min = 60000 ms
230 document.getElementById("time-selector").value = formatTime(date) + " - " + formatTime(end_time);
231}
232
233function addDateEventListeners(date) {
234 document.getElementById("date-prev").addEventListener('click', function (el) {
235 current_time = new Date(current_time.getTime() - 24*60*60000);
236 buildTimeSelector(current_time);
237 fetchClasses();
238 });
239 document.getElementById("date-next").addEventListener('click', function (el) {
240 current_time = new Date(current_time.getTime() + 24*60*60000);
241 buildTimeSelector(current_time);
242 fetchClasses();
243 });
244 document.getElementById("time-prev").addEventListener('click', function (el) {
245 current_time = new Date(current_time.getTime() - 30*60000);
246 if (current_time.getHours() < MIN_HOUR) {
247 current_time = new Date(current_time.getTime() - 24*60*60000);
248 current_time.setHours(MAX_HOUR - 1);
249 current_time.setMinutes(30);
250 }
251 buildTimeSelector(current_time);
252 fetchClasses();
253 });
254 document.getElementById("time-next").addEventListener('click', function (el) {
255 current_time = new Date(current_time.getTime() + 30*60000);
256 if (current_time.getHours() >= MAX_HOUR) {
257 current_time = new Date(current_time.getTime() + 24*60*60000);
258 current_time.setHours(MIN_HOUR);
259 current_time.setMinutes(0);
260 }
261 buildTimeSelector(current_time);
262 fetchClasses();
263 });
264}
265
266function formatTime(d) {
267 return d.toLocaleTimeString("ca", {timeStyle: 'short'});
268 /* var str = "";
delefmeeb0a4702020-10-04 18:15:55 +0200269 str += d.getHours();
270 str += ":";
271 if (d.getMinutes() < 10) str += "0";
delefmeb5506032020-10-04 19:19:27 +0200272 str += d.getMinutes();
273 return str; */
274}
275
276function formatDate(d) {
277 return d.toLocaleDateString("ca");
278}
279
280function fetchClasses() {
281 console.log(api_url + "getClassesInTime/" + current_time.getTime()/1000);
282 fetch(api_url + "getClassesInTime/" + current_time.getTime()/1000, {
283 "mode": "cors",
284 "credentials": "include"
285 })
286 .then(response => response.json())
287 .then(data => {
288 if (data.payload.classes.length == 0) {
289 document.getElementById('no-subjects').classList.remove('is-hidden');
290 document.getElementById('subject-container').classList.add('is-hidden');
291 document.getElementById('fme-maps-container').classList.add('is-hidden');
292 } else {
293 repeated_subjects = findRepeatedSubjects(data.payload.classes);
294 buildSubjectContainer(data.payload.classes, repeated_subjects);
295 document.getElementById('no-subjects').classList.add('is-hidden');
296 document.getElementById('subject-container').classList.remove('is-hidden');
297 document.getElementById('fme-maps-container').classList.remove('is-hidden');
298 }
299
300 });
delefmeeb0a4702020-10-04 18:15:55 +0200301}
302
303function onPageLoad() {
304
delefmeb5506032020-10-04 19:19:27 +0200305 // Check if user is signed in
delefmeeb0a4702020-10-04 18:15:55 +0200306 if (localStorage.getItem('devMode') == 'true') {
307 var banner = document.getElementById('dev-mode');
308 banner.addEventListener('click', _ => {
309 localStorage.devMode = 'false';
310 location.reload();
311 });
delefmeb5506032020-10-04 19:19:27 +0200312 banner.classList.remove('is-hidden');
delefmeeb0a4702020-10-04 18:15:55 +0200313 api_url = localStorage.getItem('apiUrl') || 'https://covid-tracability-backend-dev.sandbox.avm99963.com/api/v1/'
314 } else {
315 api_url = "https://covid-tracability-backend-prod.sandbox.avm99963.com/api/v1/";
316 }
delefmeb5506032020-10-04 19:19:27 +0200317
318 current_time = getDefaultTime();
319 buildTimeSelector(current_time);
320
delefmeeb0a4702020-10-04 18:15:55 +0200321 fetch(api_url + "isSignedIn", {
322 "mode": "cors",
323 "credentials": "include"
324 })
325 .then(response => response.json())
326 .then(data => {
327 if (!data.payload.signedIn) {
328 console.log("Not signed in!");
329 fetch(api_url + "getAuthUrl", {
330 "mode": "cors",
331 "credentials": "include"
332 })
333 .then(response => response.json())
334 .then(data => {
335 // TODO: redirect here
336 // location.href = data.payload.url;
337 console.warn('Log in here: ', data.payload.url);
338 });
339 }
340 });
341
delefmeb5506032020-10-04 19:19:27 +0200342 fetchClasses();
delefmeeb0a4702020-10-04 18:15:55 +0200343}
344
345function sendForm() {
346 // Add subject to user
347 fetch(api_url + "addUserSubject", {
348 "method": "POST",
349 "body": JSON.stringify({
350 subject: final_JSON.class.subject_id
351 }),
352 "mode": "cors",
353 "credentials": "include"
354 })
355 .then(res => res.json())
356 .then(json => {
357 console.log("Subject added to user: ", json);
358
359 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
360 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
361
362 var params = new URLSearchParams();
363 params.append("entry." + idsFormulari.room, final_JSON.class.room); // class, number, letter
364 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 +0200365 params.append("entry." + idsFormulari.begins, formatTime(begins));
366 params.append("entry." + idsFormulari.ends, formatTime(ends));
delefmeeb0a4702020-10-04 18:15:55 +0200367 params.append("entry." + idsFormulari.rows[final_JSON.letter], 'Columna ' + final_JSON.number);
368 // params.append("entry." + idsFormulari.notes, '[Autogenerat per delefme/covid-tracability -- Assignatura seleccionada: ' + (final_JSON.class.friendly_name || final_JSON.class.calendar_name) + ']');
369
370 var formulari_link = formBaseUrl + '?' + params.toString() + '#i1';
371 window.location.href = formulari_link;
372 });
373}
374
375
376function addEventListeners() {
377 window.addEventListener('load', onPageLoad);
378
379 var elements = document.getElementsByClassName("button");
380 Array.from(elements).forEach(function(element) {
381 element.addEventListener('click', clickButton);
382 element.parent = element.parentNode.id;
383 });
384
385 var elements = document.getElementsByClassName("complex-button");
386 Array.from(elements).forEach(function(element) {
387 element.addEventListener('click', clickButton);
388 element.parent = element.parentNode.id;
389 });
390
391 document.getElementById("send-button").addEventListener('click', function (el) {
392 document.getElementById("send-button").classList.add('is-loading');
393 sendForm();
394 });
delefmeb5506032020-10-04 19:19:27 +0200395
396 addDateEventListeners();
delefmeeb0a4702020-10-04 18:15:55 +0200397}
398
399addEventListeners();