blob: 81b76ed289fac75798f7317192ee741285bd66ce [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;
46 document.getElementById('date-final').textContent = begins.toLocaleDateString();
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
116 for (var [i, classe] of classes) {
117 var hora_inici = formatDate(new Date(parseInt(classe.begins)*1000));
118 var hora_final = formatDate(new Date(parseInt(classe.ends)*1000));
119 var classeDiv = document.createElement('div');
120
121 // Check if the subject is repeated
122 duplicateSubjectBoolNext = data.payload.classes[i+1].friendly_name == classe.friendly_name;
123 duplicateSubjectBoolPrev = data.payload.classes[i-1].friendly_name == classe.friendly_name;
124
125 if(duplicateSubjectBoolNext && i < classes.length - 1 && duplicateSubjectCounter%2 == 1) {
126 classeDiv.classList.add('message', 'complex-button-full');
127 }
128
129 if (duplicateSubjectBoolPrev && i > 0) {
130 classeDiv.classList.add('message', 'complex-button2Right');
131 } else if(duplicateSubjectBoolNext && i < data.payload.classes.length - 1) {
132 classeDiv.classList.add('message', 'complex-button2Left');
133 } else {
134 classeDiv.classList.add('message', 'complex-button');
135 }
136
137 classeDiv.classList.add('message', 'complex-button');
138 classeDiv.id = 'subject-' + classe.subject_id + '-' + classe.room;
139 classeDiv.setAttribute('data-class', JSON.stringify(classe));
140
141 var header = document.createElement('div');
142 header.classList.add('message-header');
143
144 if (!(duplicateSubjectBoolPrev && i > 0)) {
145 header.textContent = classe.friendly_name || classe.calendar_name;
146 } else {
147 header.textContent = classe.friendly_name || classe.calendar_name;;
148 header.style.color = "#4A4A4A";
149 }
150
151 var body = document.createElement('div');
152 body.classList.add('message-body');
153
154 var div1 = document.createElement('div');
155 var span = document.createElement('span');
156 span.textContent = classe.room;
157
158 div1.classList.add('has-text-weight-bold');
159
160 div1.textContent = 'Aula ';
161 div1.appendChild(span);
162
163 var div2 = document.createElement('div');
164 div2.textContent = hora_inici + ' - ' + hora_final;
165
166 body.appendChild(div1);
167 body.appendChild(div2);
168
169 classeDiv.appendChild(header);
170 classeDiv.appendChild(body);
171
172 document.getElementById("subject-container").appendChild(classeDiv);
173 ++duplicateSubjectCounter;
174 }
delefmeb5506032020-10-04 19:19:27 +0200175
delefmeeb0a4702020-10-04 18:15:55 +0200176 var elements = document.getElementsByClassName("button");
177 Array.from(elements).forEach(function(element) {
178 element.addEventListener('click', clickButton);
179 element.parent = element.parentNode.id;
180 });
181 var elements = document.getElementsByClassName("complex-button");
182 Array.from(elements).forEach(function(element) {
183 element.addEventListener('click', clickButton);
184 element.parent = element.parentNode.id;
185 });
186}
187
delefmeb5506032020-10-04 19:19:27 +0200188function getDefaultTime() {
189 var time = new Date();
190 time.setSeconds(0);
191 time.setMilliseconds(0);
192 if (time.getMinutes() < 30) time.setMinutes(0);
193 else time.setMinutes(30);
194 if (time.getHours() < MIN_HOUR) {
195 time.setHours(MIN_HOUR);
196 time.setMinutes(0);
197 }
198 if (time.getHours() >= MAX_HOUR) {
199 time.setHours(MAX_HOUR - 1);
200 time.setMinutes(30);
201 }
202 return time
203}
204
205function buildTimeSelector(date) {
206 document.getElementById("date-selector").value = formatDate(date);
207 var end_time = new Date(date.getTime() + 30*60000); // 1 min = 60000 ms
208 document.getElementById("time-selector").value = formatTime(date) + " - " + formatTime(end_time);
209}
210
211function addDateEventListeners(date) {
212 document.getElementById("date-prev").addEventListener('click', function (el) {
213 current_time = new Date(current_time.getTime() - 24*60*60000);
214 buildTimeSelector(current_time);
215 fetchClasses();
216 });
217 document.getElementById("date-next").addEventListener('click', function (el) {
218 current_time = new Date(current_time.getTime() + 24*60*60000);
219 buildTimeSelector(current_time);
220 fetchClasses();
221 });
222 document.getElementById("time-prev").addEventListener('click', function (el) {
223 current_time = new Date(current_time.getTime() - 30*60000);
224 if (current_time.getHours() < MIN_HOUR) {
225 current_time = new Date(current_time.getTime() - 24*60*60000);
226 current_time.setHours(MAX_HOUR - 1);
227 current_time.setMinutes(30);
228 }
229 buildTimeSelector(current_time);
230 fetchClasses();
231 });
232 document.getElementById("time-next").addEventListener('click', function (el) {
233 current_time = new Date(current_time.getTime() + 30*60000);
234 if (current_time.getHours() >= MAX_HOUR) {
235 current_time = new Date(current_time.getTime() + 24*60*60000);
236 current_time.setHours(MIN_HOUR);
237 current_time.setMinutes(0);
238 }
239 buildTimeSelector(current_time);
240 fetchClasses();
241 });
242}
243
244function formatTime(d) {
245 return d.toLocaleTimeString("ca", {timeStyle: 'short'});
246 /* var str = "";
delefmeeb0a4702020-10-04 18:15:55 +0200247 str += d.getHours();
248 str += ":";
249 if (d.getMinutes() < 10) str += "0";
delefmeb5506032020-10-04 19:19:27 +0200250 str += d.getMinutes();
251 return str; */
252}
253
254function formatDate(d) {
255 return d.toLocaleDateString("ca");
256}
257
258function fetchClasses() {
259 console.log(api_url + "getClassesInTime/" + current_time.getTime()/1000);
260 fetch(api_url + "getClassesInTime/" + current_time.getTime()/1000, {
261 "mode": "cors",
262 "credentials": "include"
263 })
264 .then(response => response.json())
265 .then(data => {
266 if (data.payload.classes.length == 0) {
267 document.getElementById('no-subjects').classList.remove('is-hidden');
268 document.getElementById('subject-container').classList.add('is-hidden');
269 document.getElementById('fme-maps-container').classList.add('is-hidden');
270 } else {
271 repeated_subjects = findRepeatedSubjects(data.payload.classes);
272 buildSubjectContainer(data.payload.classes, repeated_subjects);
273 document.getElementById('no-subjects').classList.add('is-hidden');
274 document.getElementById('subject-container').classList.remove('is-hidden');
275 document.getElementById('fme-maps-container').classList.remove('is-hidden');
276 }
277
278 });
delefmeeb0a4702020-10-04 18:15:55 +0200279}
280
281function onPageLoad() {
282
delefmeb5506032020-10-04 19:19:27 +0200283 // Check if user is signed in
delefmeeb0a4702020-10-04 18:15:55 +0200284 if (localStorage.getItem('devMode') == 'true') {
285 var banner = document.getElementById('dev-mode');
286 banner.addEventListener('click', _ => {
287 localStorage.devMode = 'false';
288 location.reload();
289 });
delefmeb5506032020-10-04 19:19:27 +0200290 banner.classList.remove('is-hidden');
delefmeeb0a4702020-10-04 18:15:55 +0200291 api_url = localStorage.getItem('apiUrl') || 'https://covid-tracability-backend-dev.sandbox.avm99963.com/api/v1/'
292 } else {
293 api_url = "https://covid-tracability-backend-prod.sandbox.avm99963.com/api/v1/";
294 }
delefmeb5506032020-10-04 19:19:27 +0200295
296 current_time = getDefaultTime();
297 buildTimeSelector(current_time);
298
delefmeeb0a4702020-10-04 18:15:55 +0200299 fetch(api_url + "isSignedIn", {
300 "mode": "cors",
301 "credentials": "include"
302 })
303 .then(response => response.json())
304 .then(data => {
305 if (!data.payload.signedIn) {
306 console.log("Not signed in!");
307 fetch(api_url + "getAuthUrl", {
308 "mode": "cors",
309 "credentials": "include"
310 })
311 .then(response => response.json())
312 .then(data => {
313 // TODO: redirect here
314 // location.href = data.payload.url;
315 console.warn('Log in here: ', data.payload.url);
316 });
317 }
318 });
319
delefmeb5506032020-10-04 19:19:27 +0200320 fetchClasses();
delefmeeb0a4702020-10-04 18:15:55 +0200321}
322
323function sendForm() {
324 // Add subject to user
325 fetch(api_url + "addUserSubject", {
326 "method": "POST",
327 "body": JSON.stringify({
328 subject: final_JSON.class.subject_id
329 }),
330 "mode": "cors",
331 "credentials": "include"
332 })
333 .then(res => res.json())
334 .then(json => {
335 console.log("Subject added to user: ", json);
336
337 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
338 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
339
340 var params = new URLSearchParams();
341 params.append("entry." + idsFormulari.room, final_JSON.class.room); // class, number, letter
342 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 +0200343 params.append("entry." + idsFormulari.begins, formatTime(begins));
344 params.append("entry." + idsFormulari.ends, formatTime(ends));
delefmeeb0a4702020-10-04 18:15:55 +0200345 params.append("entry." + idsFormulari.rows[final_JSON.letter], 'Columna ' + final_JSON.number);
346 // params.append("entry." + idsFormulari.notes, '[Autogenerat per delefme/covid-tracability -- Assignatura seleccionada: ' + (final_JSON.class.friendly_name || final_JSON.class.calendar_name) + ']');
347
348 var formulari_link = formBaseUrl + '?' + params.toString() + '#i1';
349 window.location.href = formulari_link;
350 });
351}
352
353
354function addEventListeners() {
355 window.addEventListener('load', onPageLoad);
356
357 var elements = document.getElementsByClassName("button");
358 Array.from(elements).forEach(function(element) {
359 element.addEventListener('click', clickButton);
360 element.parent = element.parentNode.id;
361 });
362
363 var elements = document.getElementsByClassName("complex-button");
364 Array.from(elements).forEach(function(element) {
365 element.addEventListener('click', clickButton);
366 element.parent = element.parentNode.id;
367 });
368
369 document.getElementById("send-button").addEventListener('click', function (el) {
370 document.getElementById("send-button").classList.add('is-loading');
371 sendForm();
372 });
delefmeb5506032020-10-04 19:19:27 +0200373
374 addDateEventListeners();
delefmeeb0a4702020-10-04 18:15:55 +0200375}
376
377addEventListeners();