blob: d281c58462fbfab0e5a6e581f725a42799b93159 [file] [log] [blame]
Ferran Lópeza514bea2020-10-03 23:02:10 +02001
Ferran Lópezf8443fd2020-10-03 23:26:49 +02002
3const idsFormulari = {
4 room: "1063142948",
5 day: "2115504093",
6 begins: "1749141911",
7 ends: "1827359679",
8 rows: {
9 A: "208184485",
10 B: "1077148310",
11 C: "642851281",
12 D: "1686039024",
13 E: "697835787",
14 F: "1511799646",
15 G: "809853432",
16 H: "182597499",
17 I: "1890539481",
18 J: "529159478",
19 K: "1615241874",
20 L: "1334263875"
21 },
22 notes: "1600275159"
23};
24
25const formBaseUrl = "https://docs.google.com/forms/d/e/1FAIpQLSfT9o287VqLyhwR8LPdloAQWhuqCgA3NfdhgP5vb9_sVQHL-g/viewform";
Ferran Lópeza514bea2020-10-03 23:02:10 +020026
27var final_JSON = {
28 "class": null,
29 "number": "",
30 "letter": ""
31};
32
Ferran Lópezf8443fd2020-10-03 23:26:49 +020033var current_section = "section-1";
34
Ferran López4138be82020-10-03 23:48:54 +020035var repeated_subjects;
Ferran Lópezf8443fd2020-10-03 23:26:49 +020036
Ferran Lópeza514bea2020-10-03 23:02:10 +020037function fillInSummary() {
Ferran López697111d2020-10-03 23:08:26 +020038 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
39 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
Ferran Lópeza514bea2020-10-03 23:02:10 +020040
Ferran López697111d2020-10-03 23:08:26 +020041 document.getElementById('subject-final').textContent = final_JSON.class.friendly_name || final_JSON.class.calendar_name;
42 document.getElementById('classroom-final').textContent = final_JSON.class.room;
43 document.getElementById('date-final').textContent = begins.toLocaleDateString();
44 document.getElementById('time-final').textContent = formatDate(begins) + ' - ' + formatDate(ends);
45 document.getElementById('letter-final').textContent = final_JSON.letter;
46 document.getElementById('number-final').textContent = final_JSON.number;
Ferran Lópeza514bea2020-10-03 23:02:10 +020047}
48
49function clickButton(element) {
50 var btn = element.currentTarget;
51 var parent = btn.parent;
52
53 if (parent == "subject-container") {
54 // Canvi de background del button
55 var selectedClass = JSON.parse(btn.getAttribute('data-class'));
56 $("#subject-container .complex-button").removeClass("is-link")
57 btn.classList.add("is-link");
58 // Canvi JSON
59 final_JSON["class"] = selectedClass;
Ferran López4138be82020-10-03 23:48:54 +020060 // Missatge advertència classe repetida
61 if (repeated_subjects.has(selectedClass.id)) {
62 document.getElementById('repeated-subject-warning').classList.remove('hidden');
63 document.getElementById('repeated-subject-warning-class').textContent = selectedClass.room;
64 } else {
65 document.getElementById('repeated-subject-warning').classList.add('hidden');
66 }
Ferran Lópeza514bea2020-10-03 23:02:10 +020067 // Anchor següent pregunta
68 switchSection("section-2");
69 } else if (parent == "number-container") {
70 // Canvi de background del button
71 $("#number-container .button").removeClass("is-link is-light is-active")
72 btn.classList.add("is-link", "is-light", "is-active");
73 // Canvi JSON
74 final_JSON["number"] = btn.getAttribute('data-number');
75 // Introducció de totes les dades al resum final
76 fillInSummary();
77 // Anchor següent pregunta
78 switchSection("section-send");
79 } else if (parent == "letter-container") {
80 // Canvi de background del button
81 $("#letter-container .button").removeClass("is-link is-light is-active")
82 btn.classList.add("is-link", "is-light", "is-active");
83 // Canvi JSON
84 final_JSON["letter"] = btn.getAttribute('data-letter');
85 // Anchor següent pregunta
86 switchSection("section-3");
87 }
88}
89
Ferran Lópezf8443fd2020-10-03 23:26:49 +020090function switchSection(s) {
91 setTimeout(function(){
92 document.getElementById(current_section).classList.add('hidden');
93 document.getElementById(s).classList.remove('hidden');
94 current_section = s;
95 }, 75);
96}
97
Ferran López4138be82020-10-03 23:48:54 +020098function findRepeatedSubjects(classes) {
99 var rep = new Set();
Ferran Lópezf8443fd2020-10-03 23:26:49 +0200100 for (var [i, classe] of classes.entries()) {
Ferran López4138be82020-10-03 23:48:54 +0200101 if (i > 0 && classes[i-1].calendar_name == classe.calendar_name) {
102 rep.add(classe.id);
103 rep.add(classes[i-1].id);
104 }
105 }
106 return rep;
107}
108
109function buildSubjectContainer(classes, repeated) {
110 for (var classe of classes) {
Ferran Lópezf8443fd2020-10-03 23:26:49 +0200111 var hora_inici = formatDate(new Date(parseInt(classe.begins)*1000));
112 var hora_final = formatDate(new Date(parseInt(classe.ends)*1000));
113
114 var classeDiv = document.createElement('div');
115 classeDiv.classList.add('message', 'complex-button');
116 classeDiv.id = 'subject-' + classe.subject_id + '-' + classe.room;
117 classeDiv.setAttribute('data-class', JSON.stringify(classe));
118
119 var header = document.createElement('div');
120 header.classList.add('message-header');
121 header.textContent = classe.friendly_name || classe.calendar_name;
122
123 var body = document.createElement('div');
124 body.classList.add('message-body');
125
126 var div1 = document.createElement('div');
127 var span = document.createElement('span');
128 span.textContent = classe.room;
129
Ferran López4138be82020-10-03 23:48:54 +0200130 if (repeated.has(classe.id)) {
Ferran Lópezf8443fd2020-10-03 23:26:49 +0200131 div1.classList.add('has-text-danger', 'has-text-weight-bold');
132 }
133
134 div1.textContent = 'Aula ';
135 div1.appendChild(span);
136
137 var div2 = document.createElement('div');
138 div2.textContent = hora_inici + ' - ' + hora_final;
139
140 body.appendChild(div1);
141 body.appendChild(div2);
142
143 classeDiv.appendChild(header);
144 classeDiv.appendChild(body);
145
146 document.getElementById("subject-container").appendChild(classeDiv);
147 }
148
149 var elements = document.getElementsByClassName("button");
150 Array.from(elements).forEach(function(element) {
151 element.addEventListener('click', clickButton);
152 element.parent = element.parentNode.id;
153 });
154 var elements = document.getElementsByClassName("complex-button");
155 Array.from(elements).forEach(function(element) {
156 element.addEventListener('click', clickButton);
157 element.parent = element.parentNode.id;
158 });
159}
160
Ferran Lópeza514bea2020-10-03 23:02:10 +0200161function formatDate(d) {
162 var str = "";
163 str += d.getHours();
164 str += ":";
165 if (d.getMinutes() < 10) str += "0";
166 str += d.getMinutes();
167 return str;
168}
169
Ferran Lópezf8443fd2020-10-03 23:26:49 +0200170function onPageLoad() {
Ferran Lópeza514bea2020-10-03 23:02:10 +0200171
Ferran López697111d2020-10-03 23:08:26 +0200172 // Check if user is signed in
173 if (localStorage.getItem('devMode') == 'true') {
174 var banner = document.getElementById('dev-mode');
175 banner.addEventListener('click', _ => {
176 localStorage.devMode = 'false';
177 location.reload();
178 });
179 banner.classList.remove('hidden');
180 api_url = localStorage.getItem('apiUrl') || 'https://covid-tracability-backend-dev.sandbox.avm99963.com/api/v1/'
181 } else {
182 api_url = "https://covid-tracability-backend-prod.sandbox.avm99963.com/api/v1/";
183 }
184 fetch(api_url + "isSignedIn", {
185 "mode": "cors",
186 "credentials": "include"
187 })
188 .then(response => response.json())
189 .then(data => {
190 if (!data.payload.signedIn) {
191 console.log("Not signed in!");
192 fetch(api_url + "getAuthUrl", {
193 "mode": "cors",
194 "credentials": "include"
195 })
196 .then(response => response.json())
197 .then(data => {
198 // TODO: redirect here
199 // location.href = data.payload.url;
200 console.warn('Log in here: ', data.payload.url);
201 });
202 }
203 });
Ferran Lópeza514bea2020-10-03 23:02:10 +0200204
Ferran López697111d2020-10-03 23:08:26 +0200205 fetch(api_url + "getCurrentClasses", {
206 "mode": "cors",
207 "credentials": "include"
208 })
209 .then(response => response.json())
210 .then(data => {
211 if (data.payload.classes.length == 0) {
212 document.getElementById('no-subjects').classList.remove('hidden');
213 } else {
Ferran López4138be82020-10-03 23:48:54 +0200214 repeated_subjects = findRepeatedSubjects(data.payload.classes);
215 buildSubjectContainer(data.payload.classes, repeated_subjects);
Ferran López697111d2020-10-03 23:08:26 +0200216 document.getElementById('fme-maps-container').classList.remove('hidden');
Ferran Lópeza514bea2020-10-03 23:02:10 +0200217 }
218
Ferran Lópeza514bea2020-10-03 23:02:10 +0200219 });
Ferran Lópezf8443fd2020-10-03 23:26:49 +0200220}
Ferran Lópeza514bea2020-10-03 23:02:10 +0200221
Ferran Lópezf8443fd2020-10-03 23:26:49 +0200222function sendForm() {
223 // Add subject to user
224 fetch(api_url + "addUserSubject", {
225 "method": "POST",
226 "body": JSON.stringify({
227 subject: final_JSON.class.subject_id
228 }),
229 "mode": "cors",
230 "credentials": "include"
231 })
232 .then(res => res.json())
233 .then(json => {
234 console.log("Subject added to user: ", json);
235
236 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
237 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
238
239 var params = new URLSearchParams();
240 params.append("entry." + idsFormulari.room, final_JSON.class.room); // class, number, letter
241 params.append("entry." + idsFormulari.day, begins.getFullYear().toString() + '-' + (begins.getMonth() + 1).toString().padStart(2, "0") + '-' + begins.getDate().toString().padStart(2, "0"));
242 params.append("entry." + idsFormulari.begins, formatDate(begins));
243 params.append("entry." + idsFormulari.ends, formatDate(ends));
244 params.append("entry." + idsFormulari.rows[final_JSON.letter], 'Columna ' + final_JSON.number);
245 // params.append("entry." + idsFormulari.notes, '[Autogenerat per delefme/covid-tracability -- Assignatura seleccionada: ' + (final_JSON.class.friendly_name || final_JSON.class.calendar_name) + ']');
246
247 var formulari_link = formBaseUrl + '?' + params.toString() + '#i1';
248 window.location.href = formulari_link;
249 });
250}
251
252
253function addEventListeners() {
254 window.addEventListener('load', onPageLoad);
255
256 var elements = document.getElementsByClassName("button");
257 Array.from(elements).forEach(function(element) {
258 element.addEventListener('click', clickButton);
259 element.parent = element.parentNode.id;
260 });
261
262 var elements = document.getElementsByClassName("complex-button");
263 Array.from(elements).forEach(function(element) {
264 element.addEventListener('click', clickButton);
265 element.parent = element.parentNode.id;
266 });
267
268 document.getElementById("send-button").addEventListener('click', function (el) {
Ferran Lópezbdd3acd2020-10-04 00:02:32 +0200269 document.getElementById("send-button").classList.add('is-loading');
Ferran Lópezf8443fd2020-10-03 23:26:49 +0200270 sendForm();
271 });
272}
273
274addEventListeners();