blob: aedfbdba1a6d318ff395ef2121ff20ec5ed630d8 [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'));
avm99963a83f32a2020-10-09 00:39:17 +020059 $("#subject-container .complex-button, #subject-container .message-body").removeClass("is-selected")
60 btn.classList.add('is-selected');
61 btn.parentNode.parentNode.classList.add('is-selected');
delefmeeb0a4702020-10-04 18:15:55 +020062 // Canvi JSON
63 final_JSON["class"] = selectedClass;
delefmeeb0a4702020-10-04 18:15:55 +020064 // Anchor següent pregunta
65 switchSection("section-2");
66 } else if (parent == "number-container") {
67 // Canvi de background del button
68 $("#number-container .button").removeClass("is-link is-light is-active")
69 btn.classList.add("is-link", "is-light", "is-active");
70 // Canvi JSON
71 final_JSON["number"] = btn.getAttribute('data-number');
72 // Introducció de totes les dades al resum final
73 fillInSummary();
74 // Anchor següent pregunta
75 switchSection("section-send");
76 } else if (parent == "letter-container") {
77 // Canvi de background del button
78 $("#letter-container .button").removeClass("is-link is-light is-active")
79 btn.classList.add("is-link", "is-light", "is-active");
80 // Canvi JSON
81 final_JSON["letter"] = btn.getAttribute('data-letter');
82 // Anchor següent pregunta
83 switchSection("section-3");
84 }
85}
86
87function switchSection(s) {
88 setTimeout(function(){
delefmeb5506032020-10-04 19:19:27 +020089 document.getElementById(current_section).classList.add('is-hidden');
90 document.getElementById(s).classList.remove('is-hidden');
delefmeeb0a4702020-10-04 18:15:55 +020091 current_section = s;
92 }, 75);
93}
94
95function findRepeatedSubjects(classes) {
96 var rep = new Set();
97 for (var [i, classe] of classes.entries()) {
98 if (i > 0 && classes[i-1].calendar_name == classe.calendar_name) {
99 rep.add(classe.id);
100 rep.add(classes[i-1].id);
101 }
102 }
103 return rep;
104}
105
avm99963a83f32a2020-10-09 00:39:17 +0200106function transformClasses(rawClasses) {
107 var classes = [];
108 rawClasses.forEach(c => {
109 var found = false;
110 for (var i = 0; i < classes.length; ++i) {
111 if ((classes[i][0].friendly_name || classes[i][0].calendar_name) == (c.friendly_name || c.calendar_name)) {
112 classes[i].push(c);
113 found = true;
114 break;
115 }
116 }
117
118 if (!found) {
119 classes.push([c]);
120 }
delefme381067d2020-10-04 20:07:21 +0200121 });
avm99963a83f32a2020-10-09 00:39:17 +0200122
123 return classes;
124}
125
126function buildSubjectContainer(classes) {
127 // Flush existing classes
128 document.querySelectorAll('#subject-container .message').forEach(function(classe) {
129 classe.parentNode.removeChild(classe);
130 });
131
132 for (var uniqueClasses of classes) {
133 var firstClass = uniqueClasses[0];
134 var hora_inici = formatTime(new Date(parseInt(firstClass.begins)*1000));
135 var hora_final = formatTime(new Date(parseInt(firstClass.ends)*1000));
136
137 var classeDiv = document.createElement('div');
delefmeeb0a4702020-10-04 18:15:55 +0200138 classeDiv.classList.add('message', 'complex-button');
delefmeeb0a4702020-10-04 18:15:55 +0200139
140 var header = document.createElement('div');
141 header.classList.add('message-header');
avm99963a83f32a2020-10-09 00:39:17 +0200142 header.textContent = firstClass.friendly_name || firstClass.calendar_name;
delefmeeb0a4702020-10-04 18:15:55 +0200143
avm99963a83f32a2020-10-09 00:39:17 +0200144 var roomsDiv = document.createElement('div');
145 roomsDiv.classList.add('rooms-container');
delefmeeb0a4702020-10-04 18:15:55 +0200146
147 classeDiv.appendChild(header);
delefmeeb0a4702020-10-04 18:15:55 +0200148
avm99963a83f32a2020-10-09 00:39:17 +0200149 for (var classe of uniqueClasses) {
150 if (classe.user_selected) {
151 classeDiv.classList.add('is-primary');
152 }
153
154 var body = document.createElement('div');
155 body.classList.add('message-body');
156 body.setAttribute('data-class', JSON.stringify(classe));
157 body.addEventListener('click', clickButton);
158 body.parent = 'subject-container';
159
160 var div1 = document.createElement('div');
161
162 div1.classList.add('has-text-weight-bold');
163 div1.textContent = 'Aula ';
164
165 var span = document.createElement('span');
166 span.textContent = classe.room;
167 div1.appendChild(span);
168
169 var div2 = document.createElement('div');
170 div2.textContent = hora_inici + ' - ' + hora_final;
171
172 body.appendChild(div1);
173 body.appendChild(div2);
174 roomsDiv.appendChild(body);
175 }
176
177 classeDiv.appendChild(roomsDiv);
delefmeeb0a4702020-10-04 18:15:55 +0200178 document.getElementById("subject-container").appendChild(classeDiv);
delefmeeb0a4702020-10-04 18:15:55 +0200179 }
delefmeeb0a4702020-10-04 18:15:55 +0200180}
181
delefmeb5506032020-10-04 19:19:27 +0200182function getDefaultTime() {
183 var time = new Date();
184 time.setSeconds(0);
185 time.setMilliseconds(0);
186 if (time.getMinutes() < 30) time.setMinutes(0);
187 else time.setMinutes(30);
188 if (time.getHours() < MIN_HOUR) {
189 time.setHours(MIN_HOUR);
190 time.setMinutes(0);
191 }
192 if (time.getHours() >= MAX_HOUR) {
193 time.setHours(MAX_HOUR - 1);
194 time.setMinutes(30);
195 }
196 return time
197}
198
199function buildTimeSelector(date) {
200 document.getElementById("date-selector").value = formatDate(date);
201 var end_time = new Date(date.getTime() + 30*60000); // 1 min = 60000 ms
202 document.getElementById("time-selector").value = formatTime(date) + " - " + formatTime(end_time);
203}
204
avm99963ebb53842020-10-09 01:28:59 +0200205function isDateAfterTomorrow(potential_time) {
206 var now = new Date();
207 var tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
208
209 return (potential_time >= tomorrow);
210}
211
delefmeb5506032020-10-04 19:19:27 +0200212function addDateEventListeners(date) {
213 document.getElementById("date-prev").addEventListener('click', function (el) {
214 current_time = new Date(current_time.getTime() - 24*60*60000);
215 buildTimeSelector(current_time);
216 fetchClasses();
217 });
218 document.getElementById("date-next").addEventListener('click', function (el) {
avm99963ebb53842020-10-09 01:28:59 +0200219 var potential_time = new Date(current_time.getTime() + 24*60*60000);
220
221 if (isDateAfterTomorrow(potential_time)) return;
222 current_time = potential_time;
223
delefmeb5506032020-10-04 19:19:27 +0200224 buildTimeSelector(current_time);
225 fetchClasses();
226 });
227 document.getElementById("time-prev").addEventListener('click', function (el) {
228 current_time = new Date(current_time.getTime() - 30*60000);
229 if (current_time.getHours() < MIN_HOUR) {
230 current_time = new Date(current_time.getTime() - 24*60*60000);
231 current_time.setHours(MAX_HOUR - 1);
232 current_time.setMinutes(30);
233 }
234 buildTimeSelector(current_time);
235 fetchClasses();
236 });
237 document.getElementById("time-next").addEventListener('click', function (el) {
avm99963ebb53842020-10-09 01:28:59 +0200238 var potential_time = new Date(current_time.getTime() + 30*60000);
239 if (potential_time.getHours() >= MAX_HOUR) {
240 potential_time = new Date(potential_time.getTime() + 24*60*60000);
241 potential_time.setHours(MIN_HOUR);
242 potential_time.setMinutes(0);
delefmeb5506032020-10-04 19:19:27 +0200243 }
avm99963ebb53842020-10-09 01:28:59 +0200244
245 if (isDateAfterTomorrow(potential_time)) return;
246 current_time = potential_time;
247
delefmeb5506032020-10-04 19:19:27 +0200248 buildTimeSelector(current_time);
249 fetchClasses();
250 });
251}
252
253function formatTime(d) {
254 return d.toLocaleTimeString("ca", {timeStyle: 'short'});
255 /* var str = "";
delefmeeb0a4702020-10-04 18:15:55 +0200256 str += d.getHours();
257 str += ":";
258 if (d.getMinutes() < 10) str += "0";
delefmeb5506032020-10-04 19:19:27 +0200259 str += d.getMinutes();
260 return str; */
261}
262
263function formatDate(d) {
264 return d.toLocaleDateString("ca");
265}
266
267function fetchClasses() {
delefmeb5506032020-10-04 19:19:27 +0200268 fetch(api_url + "getClassesInTime/" + current_time.getTime()/1000, {
269 "mode": "cors",
270 "credentials": "include"
271 })
272 .then(response => response.json())
273 .then(data => {
274 if (data.payload.classes.length == 0) {
275 document.getElementById('no-subjects').classList.remove('is-hidden');
276 document.getElementById('subject-container').classList.add('is-hidden');
277 document.getElementById('fme-maps-container').classList.add('is-hidden');
278 } else {
avm99963a83f32a2020-10-09 00:39:17 +0200279 var transformedClasses = transformClasses(data.payload.classes);
280 buildSubjectContainer(transformedClasses);
delefmeb5506032020-10-04 19:19:27 +0200281 document.getElementById('no-subjects').classList.add('is-hidden');
282 document.getElementById('subject-container').classList.remove('is-hidden');
283 document.getElementById('fme-maps-container').classList.remove('is-hidden');
284 }
285
286 });
delefmeeb0a4702020-10-04 18:15:55 +0200287}
288
289function onPageLoad() {
avm999630a9f0fe2020-10-09 00:50:44 +0200290 var searchParams = new URLSearchParams(location.search);
291 if (searchParams.has('apiUrl')) {
delefmeeb0a4702020-10-04 18:15:55 +0200292 var banner = document.getElementById('dev-mode');
delefmeb5506032020-10-04 19:19:27 +0200293 banner.classList.remove('is-hidden');
avm999630a9f0fe2020-10-09 00:50:44 +0200294 api_url = searchParams.get('apiUrl') || 'https://covid-tracability-backend-dev.sandbox.avm99963.com/api/v1/'
delefmeeb0a4702020-10-04 18:15:55 +0200295 } else {
296 api_url = "https://covid-tracability-backend-prod.sandbox.avm99963.com/api/v1/";
297 }
delefmeb5506032020-10-04 19:19:27 +0200298
299 current_time = getDefaultTime();
300 buildTimeSelector(current_time);
301
avm999630a9f0fe2020-10-09 00:50:44 +0200302 // Check if user is signed in
delefmeeb0a4702020-10-04 18:15:55 +0200303 fetch(api_url + "isSignedIn", {
304 "mode": "cors",
305 "credentials": "include"
306 })
307 .then(response => response.json())
308 .then(data => {
309 if (!data.payload.signedIn) {
310 console.log("Not signed in!");
311 fetch(api_url + "getAuthUrl", {
312 "mode": "cors",
313 "credentials": "include"
314 })
315 .then(response => response.json())
316 .then(data => {
317 // TODO: redirect here
318 // location.href = data.payload.url;
319 console.warn('Log in here: ', data.payload.url);
320 });
321 }
322 });
323
delefmeb5506032020-10-04 19:19:27 +0200324 fetchClasses();
delefmeeb0a4702020-10-04 18:15:55 +0200325}
326
327function sendForm() {
328 // Add subject to user
329 fetch(api_url + "addUserSubject", {
330 "method": "POST",
331 "body": JSON.stringify({
332 subject: final_JSON.class.subject_id
333 }),
334 "mode": "cors",
335 "credentials": "include"
336 })
337 .then(res => res.json())
338 .then(json => {
339 console.log("Subject added to user: ", json);
340
341 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
342 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
343
344 var params = new URLSearchParams();
345 params.append("entry." + idsFormulari.room, final_JSON.class.room); // class, number, letter
346 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 +0200347 params.append("entry." + idsFormulari.begins, formatTime(begins));
348 params.append("entry." + idsFormulari.ends, formatTime(ends));
delefmeeb0a4702020-10-04 18:15:55 +0200349 params.append("entry." + idsFormulari.rows[final_JSON.letter], 'Columna ' + final_JSON.number);
350 // params.append("entry." + idsFormulari.notes, '[Autogenerat per delefme/covid-tracability -- Assignatura seleccionada: ' + (final_JSON.class.friendly_name || final_JSON.class.calendar_name) + ']');
351
352 var formulari_link = formBaseUrl + '?' + params.toString() + '#i1';
353 window.location.href = formulari_link;
354 });
355}
356
357
358function addEventListeners() {
359 window.addEventListener('load', onPageLoad);
360
361 var elements = document.getElementsByClassName("button");
362 Array.from(elements).forEach(function(element) {
363 element.addEventListener('click', clickButton);
364 element.parent = element.parentNode.id;
365 });
366
367 var elements = document.getElementsByClassName("complex-button");
368 Array.from(elements).forEach(function(element) {
369 element.addEventListener('click', clickButton);
370 element.parent = element.parentNode.id;
371 });
372
373 document.getElementById("send-button").addEventListener('click', function (el) {
374 document.getElementById("send-button").classList.add('is-loading');
375 sendForm();
376 });
delefmeb5506032020-10-04 19:19:27 +0200377
378 addDateEventListeners();
delefmeeb0a4702020-10-04 18:15:55 +0200379}
380
381addEventListeners();