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'));
|
avm99963 | a83f32a | 2020-10-09 00:39:17 +0200 | [diff] [blame^] | 59 | $("#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');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 62 | // Canvi JSON
|
| 63 | final_JSON["class"] = selectedClass;
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 64 | // 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 |
|
| 87 | function switchSection(s) {
|
| 88 | setTimeout(function(){
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 89 | document.getElementById(current_section).classList.add('is-hidden');
|
| 90 | document.getElementById(s).classList.remove('is-hidden');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 91 | current_section = s;
|
| 92 | }, 75);
|
| 93 | }
|
| 94 |
|
| 95 | function 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 |
|
avm99963 | a83f32a | 2020-10-09 00:39:17 +0200 | [diff] [blame^] | 106 | function 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 | }
|
delefme | 381067d | 2020-10-04 20:07:21 +0200 | [diff] [blame] | 121 | });
|
avm99963 | a83f32a | 2020-10-09 00:39:17 +0200 | [diff] [blame^] | 122 |
|
| 123 | return classes;
|
| 124 | }
|
| 125 |
|
| 126 | function 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');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 138 | classeDiv.classList.add('message', 'complex-button');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 139 |
|
| 140 | var header = document.createElement('div');
|
| 141 | header.classList.add('message-header');
|
avm99963 | a83f32a | 2020-10-09 00:39:17 +0200 | [diff] [blame^] | 142 | header.textContent = firstClass.friendly_name || firstClass.calendar_name;
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 143 |
|
avm99963 | a83f32a | 2020-10-09 00:39:17 +0200 | [diff] [blame^] | 144 | var roomsDiv = document.createElement('div');
|
| 145 | roomsDiv.classList.add('rooms-container');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 146 |
|
| 147 | classeDiv.appendChild(header);
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 148 |
|
avm99963 | a83f32a | 2020-10-09 00:39:17 +0200 | [diff] [blame^] | 149 | 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);
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 178 | document.getElementById("subject-container").appendChild(classeDiv);
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 179 | }
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 180 | }
|
| 181 |
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 182 | function 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 |
|
| 199 | function 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 |
|
| 205 | function 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 |
|
| 238 | function formatTime(d) {
|
| 239 | return d.toLocaleTimeString("ca", {timeStyle: 'short'});
|
| 240 | /* var str = "";
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 241 | str += d.getHours();
|
| 242 | str += ":";
|
| 243 | if (d.getMinutes() < 10) str += "0";
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 244 | str += d.getMinutes();
|
| 245 | return str; */
|
| 246 | }
|
| 247 |
|
| 248 | function formatDate(d) {
|
| 249 | return d.toLocaleDateString("ca");
|
| 250 | }
|
| 251 |
|
| 252 | function fetchClasses() {
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 253 | 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 {
|
avm99963 | a83f32a | 2020-10-09 00:39:17 +0200 | [diff] [blame^] | 264 | var transformedClasses = transformClasses(data.payload.classes);
|
| 265 | buildSubjectContainer(transformedClasses);
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 266 | 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 | });
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 272 | }
|
| 273 |
|
| 274 | function onPageLoad() {
|
| 275 |
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 276 | // Check if user is signed in
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 277 | if (localStorage.getItem('devMode') == 'true') {
|
| 278 | var banner = document.getElementById('dev-mode');
|
| 279 | banner.addEventListener('click', _ => {
|
| 280 | localStorage.devMode = 'false';
|
| 281 | location.reload();
|
| 282 | });
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 283 | banner.classList.remove('is-hidden');
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 284 | api_url = localStorage.getItem('apiUrl') || 'https://covid-tracability-backend-dev.sandbox.avm99963.com/api/v1/'
|
| 285 | } else {
|
| 286 | api_url = "https://covid-tracability-backend-prod.sandbox.avm99963.com/api/v1/";
|
| 287 | }
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 288 |
|
| 289 | current_time = getDefaultTime();
|
| 290 | buildTimeSelector(current_time);
|
| 291 |
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 292 | fetch(api_url + "isSignedIn", {
|
| 293 | "mode": "cors",
|
| 294 | "credentials": "include"
|
| 295 | })
|
| 296 | .then(response => response.json())
|
| 297 | .then(data => {
|
| 298 | if (!data.payload.signedIn) {
|
| 299 | console.log("Not signed in!");
|
| 300 | fetch(api_url + "getAuthUrl", {
|
| 301 | "mode": "cors",
|
| 302 | "credentials": "include"
|
| 303 | })
|
| 304 | .then(response => response.json())
|
| 305 | .then(data => {
|
| 306 | // TODO: redirect here
|
| 307 | // location.href = data.payload.url;
|
| 308 | console.warn('Log in here: ', data.payload.url);
|
| 309 | });
|
| 310 | }
|
| 311 | });
|
| 312 |
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 313 | fetchClasses();
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 314 | }
|
| 315 |
|
| 316 | function sendForm() {
|
| 317 | // Add subject to user
|
| 318 | fetch(api_url + "addUserSubject", {
|
| 319 | "method": "POST",
|
| 320 | "body": JSON.stringify({
|
| 321 | subject: final_JSON.class.subject_id
|
| 322 | }),
|
| 323 | "mode": "cors",
|
| 324 | "credentials": "include"
|
| 325 | })
|
| 326 | .then(res => res.json())
|
| 327 | .then(json => {
|
| 328 | console.log("Subject added to user: ", json);
|
| 329 |
|
| 330 | var begins = new Date(parseInt(final_JSON.class.begins)*1000);
|
| 331 | var ends = new Date(parseInt(final_JSON.class.ends)*1000);
|
| 332 |
|
| 333 | var params = new URLSearchParams();
|
| 334 | params.append("entry." + idsFormulari.room, final_JSON.class.room); // class, number, letter
|
| 335 | 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] | 336 | params.append("entry." + idsFormulari.begins, formatTime(begins));
|
| 337 | params.append("entry." + idsFormulari.ends, formatTime(ends));
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 338 | params.append("entry." + idsFormulari.rows[final_JSON.letter], 'Columna ' + final_JSON.number);
|
| 339 | // params.append("entry." + idsFormulari.notes, '[Autogenerat per delefme/covid-tracability -- Assignatura seleccionada: ' + (final_JSON.class.friendly_name || final_JSON.class.calendar_name) + ']');
|
| 340 |
|
| 341 | var formulari_link = formBaseUrl + '?' + params.toString() + '#i1';
|
| 342 | window.location.href = formulari_link;
|
| 343 | });
|
| 344 | }
|
| 345 |
|
| 346 |
|
| 347 | function addEventListeners() {
|
| 348 | window.addEventListener('load', onPageLoad);
|
| 349 |
|
| 350 | var elements = document.getElementsByClassName("button");
|
| 351 | Array.from(elements).forEach(function(element) {
|
| 352 | element.addEventListener('click', clickButton);
|
| 353 | element.parent = element.parentNode.id;
|
| 354 | });
|
| 355 |
|
| 356 | var elements = document.getElementsByClassName("complex-button");
|
| 357 | Array.from(elements).forEach(function(element) {
|
| 358 | element.addEventListener('click', clickButton);
|
| 359 | element.parent = element.parentNode.id;
|
| 360 | });
|
| 361 |
|
| 362 | document.getElementById("send-button").addEventListener('click', function (el) {
|
| 363 | document.getElementById("send-button").classList.add('is-loading');
|
| 364 | sendForm();
|
| 365 | });
|
delefme | b550603 | 2020-10-04 19:19:27 +0200 | [diff] [blame] | 366 |
|
| 367 | addDateEventListeners();
|
delefme | eb0a470 | 2020-10-04 18:15:55 +0200 | [diff] [blame] | 368 | }
|
| 369 |
|
| 370 | addEventListeners();
|