42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
|
async function vote(button) {
|
||
|
console.log(button);
|
||
|
const name = document.getElementById('nameInput').value;
|
||
|
const data = new FormData();
|
||
|
data.append("name", name);
|
||
|
data.append("vote", button.value);
|
||
|
data.append("uid", button.dataset.uid);
|
||
|
const response = await fetch('vote.php', { method: "POST", body: data });
|
||
|
button.disabled = true;
|
||
|
button.parentElement
|
||
|
.querySelectorAll('.voteButton')
|
||
|
.forEach(b => {
|
||
|
b.disabled = b.value === button.value;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
async function loadVotes() {
|
||
|
const name = document.getElementById('nameInput').value;
|
||
|
localStorage.setItem('name', name);
|
||
|
const response = await fetch('vote.php?' + new URLSearchParams({
|
||
|
name: name
|
||
|
}));
|
||
|
const result = await response.json();
|
||
|
console.log(result);
|
||
|
document
|
||
|
.querySelectorAll('.voteButton')
|
||
|
.forEach(b => {
|
||
|
b.disabled = b.dataset.uid in result && b.value == result[b.dataset.uid];
|
||
|
});
|
||
|
}
|
||
|
|
||
|
window.addEventListener('DOMContentLoaded', async (event) => {
|
||
|
console.log('DOMContentLoaded');
|
||
|
const loadedName = localStorage.getItem('name');
|
||
|
console.log(loadedName);
|
||
|
if (loadedName) {
|
||
|
const nameInput = document.getElementById('nameInput');
|
||
|
nameInput.value = loadedName;
|
||
|
await loadVotes();
|
||
|
}
|
||
|
});
|