blob: 76ded35fd96818bb8792bcad9c1997b283236b6e [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
205function addDateEventListeners(date) {
206 document.getElementById("date-prev").addEventListener('click', function (el) {
207 current_time = new Date(current_time.getTime() - 24*60*60000);
208 buildTimeSelector(current_time);
209 fetchClasses();
210 });
211 document.getElementById("date-next").addEventListener('click', function (el) {
212 current_time = new Date(current_time.getTime() + 24*60*60000);
213 buildTimeSelector(current_time);
214 fetchClasses();
215 });
216 document.getElementById("time-prev").addEventListener('click', function (el) {
217 current_time = new Date(current_time.getTime() - 30*60000);
218 if (current_time.getHours() < MIN_HOUR) {
219 current_time = new Date(current_time.getTime() - 24*60*60000);
220 current_time.setHours(MAX_HOUR - 1);
221 current_time.setMinutes(30);
222 }
223 buildTimeSelector(current_time);
224 fetchClasses();
225 });
226 document.getElementById("time-next").addEventListener('click', function (el) {
227 current_time = new Date(current_time.getTime() + 30*60000);
228 if (current_time.getHours() >= MAX_HOUR) {
229 current_time = new Date(current_time.getTime() + 24*60*60000);
230 current_time.setHours(MIN_HOUR);
231 current_time.setMinutes(0);
232 }
233 buildTimeSelector(current_time);
234 fetchClasses();
235 });
236}
237
238function formatTime(d) {
239 return d.toLocaleTimeString("ca", {timeStyle: 'short'});
240 /* var str = "";
delefmeeb0a4702020-10-04 18:15:55 +0200241 str += d.getHours();
242 str += ":";
243 if (d.getMinutes() < 10) str += "0";
delefmeb5506032020-10-04 19:19:27 +0200244 str += d.getMinutes();
245 return str; */
246}
247
248function formatDate(d) {
249 return d.toLocaleDateString("ca");
250}
251
252function fetchClasses() {
delefmeb5506032020-10-04 19:19:27 +0200253 fetch(api_url + "getClassesInTime/" + current_time.getTime()/1000, {
254 "mode": "cors",
255 "credentials": "include"
256 })
257 .then(response => response.json())
258 .then(data => {
259 if (data.payload.classes.length == 0) {
260 document.getElementById('no-subjects').classList.remove('is-hidden');
261 document.getElementById('subject-container').classList.add('is-hidden');
262 document.getElementById('fme-maps-container').classList.add('is-hidden');
263 } else {
avm99963a83f32a2020-10-09 00:39:17 +0200264 var transformedClasses = transformClasses(data.payload.classes);
265 buildSubjectContainer(transformedClasses);
delefmeb5506032020-10-04 19:19:27 +0200266 document.getElementById('no-subjects').classList.add('is-hidden');
267 document.getElementById('subject-container').classList.remove('is-hidden');
268 document.getElementById('fme-maps-container').classList.remove('is-hidden');
269 }
270
271 });
delefmeeb0a4702020-10-04 18:15:55 +0200272}
273
274function onPageLoad() {
avm999630a9f0fe2020-10-09 00:50:44 +0200275 var searchParams = new URLSearchParams(location.search);
276 if (searchParams.has('apiUrl')) {
delefmeeb0a4702020-10-04 18:15:55 +0200277 var banner = document.getElementById('dev-mode');
delefmeb5506032020-10-04 19:19:27 +0200278 banner.classList.remove('is-hidden');
avm999630a9f0fe2020-10-09 00:50:44 +0200279 api_url = searchParams.get('apiUrl') || 'https://covid-tracability-backend-dev.sandbox.avm99963.com/api/v1/'
delefmeeb0a4702020-10-04 18:15:55 +0200280 } else {
281 api_url = "https://covid-tracability-backend-prod.sandbox.avm99963.com/api/v1/";
282 }
delefmeb5506032020-10-04 19:19:27 +0200283
284 current_time = getDefaultTime();
285 buildTimeSelector(current_time);
286
avm999630a9f0fe2020-10-09 00:50:44 +0200287 // Check if user is signed in
delefmeeb0a4702020-10-04 18:15:55 +0200288 fetch(api_url + "isSignedIn", {
289 "mode": "cors",
290 "credentials": "include"
291 })
292 .then(response => response.json())
293 .then(data => {
294 if (!data.payload.signedIn) {
295 console.log("Not signed in!");
296 fetch(api_url + "getAuthUrl", {
297 "mode": "cors",
298 "credentials": "include"
299 })
300 .then(response => response.json())
301 .then(data => {
302 // TODO: redirect here
303 // location.href = data.payload.url;
304 console.warn('Log in here: ', data.payload.url);
305 });
306 }
307 });
308
delefmeb5506032020-10-04 19:19:27 +0200309 fetchClasses();
delefmeeb0a4702020-10-04 18:15:55 +0200310}
311
312function sendForm() {
313 // Add subject to user
314 fetch(api_url + "addUserSubject", {
315 "method": "POST",
316 "body": JSON.stringify({
317 subject: final_JSON.class.subject_id
318 }),
319 "mode": "cors",
320 "credentials": "include"
321 })
322 .then(res => res.json())
323 .then(json => {
324 console.log("Subject added to user: ", json);
325
326 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
327 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
328
329 var params = new URLSearchParams();
330 params.append("entry." + idsFormulari.room, final_JSON.class.room); // class, number, letter
331 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 +0200332 params.append("entry." + idsFormulari.begins, formatTime(begins));
333 params.append("entry." + idsFormulari.ends, formatTime(ends));
delefmeeb0a4702020-10-04 18:15:55 +0200334 params.append("entry." + idsFormulari.rows[final_JSON.letter], 'Columna ' + final_JSON.number);
335 // params.append("entry." + idsFormulari.notes, '[Autogenerat per delefme/covid-tracability -- Assignatura seleccionada: ' + (final_JSON.class.friendly_name || final_JSON.class.calendar_name) + ']');
336
337 var formulari_link = formBaseUrl + '?' + params.toString() + '#i1';
338 window.location.href = formulari_link;
339 });
340}
341
342
343function addEventListeners() {
344 window.addEventListener('load', onPageLoad);
345
346 var elements = document.getElementsByClassName("button");
347 Array.from(elements).forEach(function(element) {
348 element.addEventListener('click', clickButton);
349 element.parent = element.parentNode.id;
350 });
351
352 var elements = document.getElementsByClassName("complex-button");
353 Array.from(elements).forEach(function(element) {
354 element.addEventListener('click', clickButton);
355 element.parent = element.parentNode.id;
356 });
357
358 document.getElementById("send-button").addEventListener('click', function (el) {
359 document.getElementById("send-button").classList.add('is-loading');
360 sendForm();
361 });
delefmeb5506032020-10-04 19:19:27 +0200362
363 addDateEventListeners();
delefmeeb0a4702020-10-04 18:15:55 +0200364}
365
366addEventListeners();