delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 1 | const 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 |
|
| 23 | const formBaseUrl = "https://docs.google.com/forms/d/e/1FAIpQLSfT9o287VqLyhwR8LPdloAQWhuqCgA3NfdhgP5vb9_sVQHL-g/viewform";
|
| 24 |
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 25 | const MIN_HOUR = 8;
|
| 26 | const MAX_HOUR = 23; // Pels altres graus de la Facultat
|
| 27 |
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 28 | var final_JSON = {
|
| 29 | "class": null,
|
| 30 | "number": "",
|
| 31 | "letter": ""
|
| 32 | };
|
| 33 |
|
| 34 | var current_section = "section-1";
|
| 35 |
|
| 36 | var repeated_subjects;
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 37 | var current_time;
|
| 38 |
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 39 |
|
| 40 | function 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;
|
delefme | f651022 | 2020-10-04 20:21:33 +0200 | [diff] [blame] | 46 | document.getElementById('date-final').textContent = formatDate(begins);
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 47 | document.getElementById('time-final').textContent = formatTime(begins) + ' - ' + formatTime(ends);
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 48 | document.getElementById('letter-final').textContent = final_JSON.letter;
|
| 49 | document.getElementById('number-final').textContent = final_JSON.number;
|
| 50 | }
|
| 51 |
|
| 52 | function 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)) {
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 65 | document.getElementById('repeated-subject-warning').classList.remove('is-hidden');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 66 | document.getElementById('repeated-subject-warning-class').textContent = selectedClass.room;
|
| 67 | } else {
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 68 | document.getElementById('repeated-subject-warning').classList.add('is-hidden');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 69 | }
|
| 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 |
|
| 93 | function switchSection(s) {
|
| 94 | setTimeout(function(){
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 95 | document.getElementById(current_section).classList.add('is-hidden');
|
| 96 | document.getElementById(s).classList.remove('is-hidden');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 97 | current_section = s;
|
| 98 | }, 75);
|
| 99 | }
|
| 100 |
|
| 101 | function 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 |
|
| 112 | function buildSubjectContainer(classes, repeated) {
|
| 113 | var duplicateSubjectBoolNext, duplicateSubjectBoolPrev;
|
| 114 | var duplicateSubjectCounter = 0;
|
| 115 |
|
delefme | 381067d | 2020-10-04 20:07:21 +0200 | [diff] [blame] | 116 | // Flush existing classes
|
| 117 | document.querySelectorAll('.message').forEach(function(classe) {
|
| 118 | classe.classList.add('is-hidden');
|
| 119 | });
|
| 120 |
|
Dani Vilardell | d6ce735 | 2020-10-04 22:50:40 +0200 | [diff] [blame] | 121 | for (var [i, classe] of classes.entries()) {
|
| 122 | console.log(classe);
|
delefme | 4f84bd5 | 2020-10-04 20:14:49 +0200 | [diff] [blame] | 123 | var hora_inici = formatTime(new Date(parseInt(classe.begins)*1000));
|
| 124 | var hora_final = formatTime(new Date(parseInt(classe.ends)*1000));
|
delefme | 44594a4 | 2020-10-04 22:23:32 +0200 | [diff] [blame] | 125 | var classeDiv;
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 126 |
|
| 127 | // Check if the subject is repeated
|
Dani Vilardell | e70f23b | 2020-10-04 23:38:47 +0200 | [diff] [blame] | 128 | 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 | }
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 140 |
|
delefme | 44594a4 | 2020-10-04 22:23:32 +0200 | [diff] [blame] | 141 | // Change the previous classeDiv
|
delefme | f42ec32 | 2020-10-04 19:39:13 +0200 | [diff] [blame] | 142 | if(duplicateSubjectBoolNext && duplicateSubjectCounter%2 == 1) {
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 143 | classeDiv.classList.add('message', 'complex-button-full');
|
Dani Vilardell | 00b10e2 | 2020-10-04 23:11:41 +0200 | [diff] [blame] | 144 | duplicateSubjectCounter++;
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 145 | }
|
| 146 |
|
delefme | 44594a4 | 2020-10-04 22:23:32 +0200 | [diff] [blame] | 147 | classeDiv = document.createElement('div');
|
| 148 |
|
delefme | f42ec32 | 2020-10-04 19:39:13 +0200 | [diff] [blame] | 149 | if (duplicateSubjectBoolPrev) {
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 150 | classeDiv.classList.add('message', 'complex-button2Right');
|
delefme | f42ec32 | 2020-10-04 19:39:13 +0200 | [diff] [blame] | 151 | } else if(duplicateSubjectBoolNext) {
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 152 | classeDiv.classList.add('message', 'complex-button2Left');
|
Dani Vilardell | d6ce735 | 2020-10-04 22:50:40 +0200 | [diff] [blame] | 153 | console.log("hola");
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 154 | } 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 |
|
delefme | f42ec32 | 2020-10-04 19:39:13 +0200 | [diff] [blame] | 165 | if (!(duplicateSubjectBoolPrev)) {
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 166 | header.textContent = classe.friendly_name || classe.calendar_name;
|
delefme | 44594a4 | 2020-10-04 22:23:32 +0200 | [diff] [blame] | 167 | header.style.color = "#FFFFFF";
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 168 | } 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 | }
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 197 |
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 198 | 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 |
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 210 | function 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 |
|
| 227 | function 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 |
|
| 233 | function 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 |
|
| 266 | function formatTime(d) {
|
| 267 | return d.toLocaleTimeString("ca", {timeStyle: 'short'});
|
| 268 | /* var str = "";
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 269 | str += d.getHours();
|
| 270 | str += ":";
|
| 271 | if (d.getMinutes() < 10) str += "0";
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 272 | str += d.getMinutes();
|
| 273 | return str; */
|
| 274 | }
|
| 275 |
|
| 276 | function formatDate(d) {
|
| 277 | return d.toLocaleDateString("ca");
|
| 278 | }
|
| 279 |
|
| 280 | function 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 | });
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 301 | }
|
| 302 |
|
| 303 | function onPageLoad() {
|
| 304 |
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 305 | // Check if user is signed in
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 306 | if (localStorage.getItem('devMode') == 'true') {
|
| 307 | var banner = document.getElementById('dev-mode');
|
| 308 | banner.addEventListener('click', _ => {
|
| 309 | localStorage.devMode = 'false';
|
| 310 | location.reload();
|
| 311 | });
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 312 | banner.classList.remove('is-hidden');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 313 | 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 | }
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 317 |
|
| 318 | current_time = getDefaultTime();
|
| 319 | buildTimeSelector(current_time);
|
| 320 |
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 321 | 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 |
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 342 | fetchClasses();
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 343 | }
|
| 344 |
|
| 345 | function 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"));
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 365 | params.append("entry." + idsFormulari.begins, formatTime(begins));
|
| 366 | params.append("entry." + idsFormulari.ends, formatTime(ends));
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 367 | 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 |
|
| 376 | function 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 | });
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 395 |
|
| 396 | addDateEventListeners();
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 397 | }
|
| 398 |
|
| 399 | addEventListeners();
|