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