Skip to content

Commit

Permalink
Merge pull request #1 from zweidenker/Vote-Once
Browse files Browse the repository at this point in the history
Use localStorage to check if a user has already voted for a particula…
  • Loading branch information
christian2denker authored Oct 8, 2024
2 parents 1ce42c1 + 66ea9a8 commit 2cb3649
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@
</div>

<script>
const DEFAULT_POLLY_URL = 'https://app.apptivegrid.de/api/users/62398e23b8b578b35b971df0/spaces/66bcc1d7f549cad6d6dbf2be/grids/66bcc1dcf549ca6236dbf2c4/entities/BqWFtLCV6KM?layout=key';

const thumb = document.getElementById('thumb');
const needle = document.getElementById('needle');
const valueDisplay = document.getElementById('valueDisplay');
Expand Down Expand Up @@ -310,6 +312,7 @@
hasSubmitted = true; // Mark as submitted
drawCircles();
calculatePercentages(entity);
setVoteSubmitted();
})
.catch(error => {
console.error('Failed to post data:', error);
Expand Down Expand Up @@ -427,7 +430,7 @@
});
document.addEventListener("DOMContentLoaded", function() {
const urlParams = new URLSearchParams(window.location.search);
const url = urlParams.get('url') || 'https://app.apptivegrid.de/api/users/62398e23b8b578b35b971df0/spaces/66bcc1d7f549cad6d6dbf2be/grids/66bcc1dcf549ca6236dbf2c4/entities/BqWFtLCV6KM?layout=key';
const url = urlParams.get('url') || DEFAULT_POLLY_URL;
const username = urlParams.get('username') || '23bd064465e583233eab7807507a715c';
const password = urlParams.get('password') || 'a7hkqxt2qj8rfverccyzn4agt';

Expand All @@ -452,7 +455,9 @@
document.getElementById('question').textContent = data['question'] || 'No question found in response.';
document.getElementById('answer1').textContent = data['answer_1'] || 'No answer_1 found in response.';
document.getElementById('answer2').textContent = data['answer_2'] || 'No answer_2 found in response.';
document.getElementById('numberOfResultsDisplay').textContent = data['numberOfResults']['value'] || 'No numberOfResults found in response.';
if (previouslySubmitted()) {
showResults();
}
})
.catch(error => {
document.getElementById('valueDisplay').textContent = 'Fehler: ' + error.message;
Expand All @@ -462,6 +467,39 @@
}
});

function getPollyEntity() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('url') || DEFAULT_POLLY_URL;
}

function getVotedUrlArray() {
const stringArray = localStorage.getItem('voteUrl')
if (!stringArray) {
return []
}
return JSON.parse(stringArray)
}

function setVoteSubmitted() {
const array = getVotedUrlArray();
array.push(getPollyEntity());
localStorage.setItem('voteUrl', JSON.stringify(array));
}

function previouslySubmitted() {
const array = getVotedUrlArray();
return array.includes(getPollyEntity());
}

function showResults() {
hasSubmitted = true; // Mark as submitted
document.getElementById('needle').setAttribute('style', 'display: none')
document.getElementById('thumb').setAttribute('style', 'display: none')
document.getElementById('arrows').setAttribute('style', 'display: none')
drawCircles();
calculatePercentages(entity);
}

async function postDataToApptiveGrid(data) {
const url = entity['flow_uri'];

Expand Down

0 comments on commit 2cb3649

Please sign in to comment.