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