-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
361 lines (316 loc) · 13.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Um por todos!</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 10px;
background-color: #121212; /* Dark background */
color: #ffffff; /* Light text */
}
h1 {
text-align: center;
color: #ffffff; /* Light text */
}
form {
width: 90%;
max-width: 600px;
background-color: #1f1f1f; /* Darker form background */
padding: 20px;
border-radius: 10px;
margin: auto;
}
.email-input, .noun-input {
display: flex;
margin-bottom: 10px;
width:90%;
}
label {
padding-right: 20px;
color: #ffffff; /* Light text */
}
input {
flex: 3;
padding: 5px;
background-color: #2b2b2b; /* Dark input background */
color: #ffffff; /* Light text */
border: 1px solid #4a4a4a; /* Dark border */
}
.options {
padding: 10px;
background-color: #2b2b2b; /* Dark background */
border-radius: 5px;
margin-bottom: 20px;
}
.form-label {
color: #ffffff; /* Light text */
font-size: large;
margin-bottom: 10px;
display: block;
}
input[type="checkbox"] {
width: 25px;
height: 25px;
background-color: #2b2b2b;
color: #4CAF50;
border: 1px solid #4a4a4a;
transform: scale(1); /* Makes the checkbox 1.5 times larger */
}
input[type="checkbox"]:checked {
background-color: #4CAF50; /* Green when checked */
}
label[for^="option"] {
color: #ffffff; /* Light text */
font-size: large;
margin-bottom: 10px;
display: inline;
}
.centered-button {
display: flex;
justify-content: center;
align-items: center;
margin-top: 20px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 60px;
border-radius: 5px;
border: none;
cursor: pointer;
}
button:hover {
background-color: #1b8921;
}
.progress-bar-container {
background-color: #1f1f1f; /* Darker container background */
padding: 20px;
border-radius: 10px;
margin-bottom: 20px;
max-width: 600px;
margin: auto;
display: flex;
align-items: center;
}
.progress-bar {
width: 100%;
max-width: 600px;
background-color: #2b2b2b; /* Darker progress bar background */
border-radius: 5px;
padding: 3px;
margin-bottom: 10px;
margin: auto;
flex-grow: 1;
margin-right: 10px;
}
.progress-bar-fill {
height: 20px;
background-color: #4CAF50;
border-radius: 5px;
transition: width 0.5s;
}
.vote-info {
display: inline-block;
margin-left: 5px;
white-space: nowrap;
color: #ffffff; /* Light text */
}
@media (min-width: 768px) {
form, .progress-bar {
width: 80%;
}
}
</style>
</head>
<body>
<h1>Um por todos!</h1>
<form id="checkForm" action="/check" method="POST">
<div class="email-input">
<label for="userEmail">Email: </label>
<input type="email" id="userEmail" name="userEmail">
</div>
<div class="noun-input">
<label for="userChoice">Matricula:</label>
<input type="text" id="userChoice" name="userChoice">
</div>
<div class="options">
<label class="form-label" for="options">Datas:</label>
<br><br><br>
<input type="checkbox"
id="option1"
name="options"
value=1
>
<label for="option1">Segunda (26/06)</label>
<br><br><br>
<input type="checkbox"
id="option2"
name="options"
value=2
>
<label for="option2">Quarta (28/06)</label>
<br><br><br>
<input type="checkbox"
id="option3"
name="options"
value=3
>
<label for="option3">Segunda (03/07)</label>
<br><br><br>
<input type="checkbox"
id="option4"
name="options"
value=4
>
<label for="option4">Quarta (05/07)</label>
<br><br><br>
<input type="checkbox"
id="option5"
name="options"
value=5
>
<label for="option5">Segunda (10/07)</label>
<br><br><br>
<input type="checkbox"
id="option6"
name="options"
value=6
>
<label for="option6">Indiferente</label>
</div>
<div class="centered-button">
<button type="submit">Votar</button>
</div>
</form>
<br>
<div class="progress-bar-container">
<div id="result"></div><br>
</div>
<div class="progress-bar-container">
<label for="option1">Segunda (26/06)</label>
<div class="progress-bar">
<div id="progress-bar-fill-1" class="progress-bar-fill"></div>
</div>
<span class="vote-info" id="vote-info-1"></span>
</div>
<div class="progress-bar-container">
<label for="option2">Quarta (28/06)</label>
<div class="progress-bar">
<div id="progress-bar-fill-2" class="progress-bar-fill"></div>
</div>
<span class="vote-info" id="vote-info-2"></span>
</div>
<div class="progress-bar-container">
<label for="option3">Segunda (03/07)</label>
<div class="progress-bar">
<div id="progress-bar-fill-3" class="progress-bar-fill"></div>
</div>
<span class="vote-info" id="vote-info-3"></span>
</div>
<div class="progress-bar-container">
<label for="option4">Quarta (05/07)</label>
<div class="progress-bar">
<div id="progress-bar-fill-4" class="progress-bar-fill"></div>
</div>
<span class="vote-info" id="vote-info-4"></span>
</div>
<div class="progress-bar-container">
<label for="option5">Segunda (10/07)</label>
<div class="progress-bar">
<div id="progress-bar-fill-5" class="progress-bar-fill"></div>
</div>
<span class="vote-info" id="vote-info-5"></span>
</div>
<div class="progress-bar-container">
<label for="option6">Indiferente</label>
<div class="progress-bar">
<div id="progress-bar-fill-6" class="progress-bar-fill"></div>
</div>
<span class="vote-info" id="vote-info-6"></span>
</div>
<script>
function count() {
// First, get the total votes
var xhrTotal = new XMLHttpRequest();
xhrTotal.open("GET", "/votes", true);
xhrTotal.setRequestHeader("Content-Type", "application/json");
xhrTotal.onload = function() {
if (xhrTotal.status === 200) {
var responseTotal = JSON.parse(xhrTotal.responseText);
console.log(responseTotal);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = "Total de votos: " + responseTotal.result;
// Extract the total votes from the response
var totalVotes = parseInt(responseTotal.result.split('/')[1]);
// Then, get the vote counts for each option
var xhrCounts = new XMLHttpRequest();
xhrCounts.open("GET", "/eachVoted", true);
xhrCounts.setRequestHeader("Content-Type", "application/json");
xhrCounts.onload = function() {
if (xhrCounts.status === 200) {
var responseCounts = JSON.parse(xhrCounts.responseText);
console.log(responseCounts);
// Update each progress bar
for (var i = 0; i < responseCounts.voteCounts.length; i++) {
var progressBarFill = document.getElementById("progress-bar-fill-" + (i + 1));
var voteInfo = document.getElementById("vote-info-" + (i + 1));
var votePercentage = (responseCounts.voteCounts[i] / totalVotes) * 100;
progressBarFill.style.width = votePercentage + "%";
voteInfo.textContent = responseCounts.voteCounts[i] + " Votos (" + votePercentage.toFixed(2) + "%)";
}
} else {
console.error("Request failed. Status: " + xhrCounts.status);
}
};
xhrCounts.send();
} else {
console.error("Request failed. Status: " + xhrTotal.status);
}
};
xhrTotal.send();
}
window.addEventListener("load", count);
// Form submission functionality
document.getElementById("checkForm").addEventListener("submit", function(event) {
event.preventDefault();
var noun = document.getElementById("noun").value;
var email = document.getElementById("email").value;
var xhr = new XMLHttpRequest();
xhr.open("POST", "/check", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function() {
if (xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.exists === true) {
// Get the checked votes
var checkedVotes = [];
var checkboxes = document.querySelectorAll('input[name="options"]:checked');
checkboxes.forEach(function(checkbox) {
checkedVotes.push(checkbox.value);
});
// Send the checked votes to /vote endpoint
var voteXhr = new XMLHttpRequest();
voteXhr.open("POST", "/vote", true);
voteXhr.setRequestHeader("Content-Type", "application/json");
voteXhr.onload = function() {
if (voteXhr.status === 200) {
console.log("Votes submitted successfully");
count();
} else {
console.error("Request failed. Status: " + voteXhr.status);
}
};
voteXhr.send(JSON.stringify({ email: email, noun: noun, votes: checkedVotes, time: new Date().toISOString() }));
}
} else {
console.error("Request failed. Status: " + xhr.status);
}
};
xhr.send(JSON.stringify({ noun: noun, email: email }));
});
</script>
</body>
</html>