Skip to content

Commit 66ea9a8

Browse files
committed
Use localStorage to check if a user has already voted for a particular poll
1 parent 2babb66 commit 66ea9a8

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

index.html

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@
208208
</div>
209209

210210
<script>
211+
const DEFAULT_POLLY_URL = 'https://app.apptivegrid.de/api/users/62398e23b8b578b35b971df0/spaces/66bcc1d7f549cad6d6dbf2be/grids/66bcc1dcf549ca6236dbf2c4/entities/BqWFtLCV6KM?layout=key';
212+
211213
const thumb = document.getElementById('thumb');
212214
const needle = document.getElementById('needle');
213215
const valueDisplay = document.getElementById('valueDisplay');
@@ -306,6 +308,7 @@
306308
hasSubmitted = true; // Mark as submitted
307309
drawCircles();
308310
calculatePercentages(entity);
311+
setVoteSubmitted();
309312
})
310313
.catch(error => {
311314
console.error('Failed to post data:', error);
@@ -423,7 +426,7 @@
423426
});
424427
document.addEventListener("DOMContentLoaded", function() {
425428
const urlParams = new URLSearchParams(window.location.search);
426-
const url = urlParams.get('url') || 'https://app.apptivegrid.de/api/users/62398e23b8b578b35b971df0/spaces/66bcc1d7f549cad6d6dbf2be/grids/66bcc1dcf549ca6236dbf2c4/entities/BqWFtLCV6KM?layout=key';
429+
const url = urlParams.get('url') || DEFAULT_POLLY_URL;
427430
const username = urlParams.get('username') || '23bd064465e583233eab7807507a715c';
428431
const password = urlParams.get('password') || 'a7hkqxt2qj8rfverccyzn4agt';
429432

@@ -448,7 +451,9 @@
448451
document.getElementById('question').textContent = data['question'] || 'No question found in response.';
449452
document.getElementById('answer1').textContent = data['answer_1'] || 'No answer_1 found in response.';
450453
document.getElementById('answer2').textContent = data['answer_2'] || 'No answer_2 found in response.';
451-
document.getElementById('numberOfResultsDisplay').textContent = data['numberOfResults']['value'] || 'No numberOfResults found in response.';
454+
if (previouslySubmitted()) {
455+
showResults();
456+
}
452457
})
453458
.catch(error => {
454459
document.getElementById('valueDisplay').textContent = 'Fehler: ' + error.message;
@@ -458,6 +463,39 @@
458463
}
459464
});
460465

466+
function getPollyEntity() {
467+
const urlParams = new URLSearchParams(window.location.search);
468+
return urlParams.get('url') || DEFAULT_POLLY_URL;
469+
}
470+
471+
function getVotedUrlArray() {
472+
const stringArray = localStorage.getItem('voteUrl')
473+
if (!stringArray) {
474+
return []
475+
}
476+
return JSON.parse(stringArray)
477+
}
478+
479+
function setVoteSubmitted() {
480+
const array = getVotedUrlArray();
481+
array.push(getPollyEntity());
482+
localStorage.setItem('voteUrl', JSON.stringify(array));
483+
}
484+
485+
function previouslySubmitted() {
486+
const array = getVotedUrlArray();
487+
return array.includes(getPollyEntity());
488+
}
489+
490+
function showResults() {
491+
hasSubmitted = true; // Mark as submitted
492+
document.getElementById('needle').setAttribute('style', 'display: none')
493+
document.getElementById('thumb').setAttribute('style', 'display: none')
494+
document.getElementById('arrows').setAttribute('style', 'display: none')
495+
drawCircles();
496+
calculatePercentages(entity);
497+
}
498+
461499
async function postDataToApptiveGrid(data) {
462500
const url = entity['flow_uri'];
463501

0 commit comments

Comments
 (0)