index.js refactor

This commit is contained in:
2024-09-20 14:33:37 +02:00
parent 6122b21d90
commit 027aff84b9
3 changed files with 72 additions and 43 deletions

View File

@@ -89,16 +89,20 @@ body {
grid-column-start: 2; grid-column-start: 2;
grid-column-end: 3; grid-column-end: 3;
} }
.confirm { .notification {
position: absolute;
top: 0;
margin: auto; margin: auto;
width: fit-content; width: 100%;
height: 100dvh;
text-align: center; text-align: center;
background-color: #11111b;
} }
.confirm > h1 { .notification > h1 {
color: yellow; color: yellow;
} }
.confirm > h3 { .notification > h3 {
color: white; color: white;
} }

View File

@@ -26,7 +26,8 @@
</div> </div>
<div id="notification" class="notification" hidden="hidden"> <div id="notification" class="notification" hidden="hidden">
<h1 id="title"></h1> <h1 id="title"></h1>
<h2 id="detail"></h2> <h3 id="detail"></h3>
<button id="close">Ok</button>
</div> </div>
<div id="footer" class="footer"> <div id="footer" class="footer">
<a href="/">Voter</a> <a href="/">Voter</a>

View File

@@ -1,10 +1,10 @@
let vote = { let vote = {
plusId: null, plus_player_id: null,
plusNickname: "", plus_nickname: "",
plusReason: "", plus_reason: "",
minusId: null, minus_player_id: null,
minusNickname: "", minus_nickname: "",
minusReason: "" minus_reason: ""
} }
let current_page = 0; let current_page = 0;
@@ -21,35 +21,46 @@ async function main() {
select.append(option); select.append(option);
}) })
select.value = null; select.value = null;
select.addEventListener("change", () => {
if (current_page) {
vote.minus_player_id = parseInt(select.value);
} else {
vote.plus_player_id = parseInt(select.value);
}
})
nickname.addEventListener("change", () => {
if (current_page) {
vote.minus_nickname = nickname.value;
} else {
vote.plus_nickname = nickname.value;
}
})
reason.addEventListener("change", () => {
if (current_page) {
vote.minus_reason = reason.value;
} else {
vote.plus_reason = reason.value;
}
})
let rightButton = document.getElementById("next"); let rightButton = document.getElementById("next");
let leftButton = document.getElementById("prev"); let leftButton = document.getElementById("prev");
let title = document.getElementById("app_title"); let title = document.getElementById("app_title");
// better to do event listeners onchange with condition currentpage rightButton.addEventListener("click", async () => {
// such as currentpage gets changed before reassigning values
// should work
rightButton.addEventListener("click", () => {
if (current_page) { if (current_page) {
vote.minusId = parseInt(select.value); if (await fetch("/post", {
select.value = null; method:"post", body: JSON.stringify(vote)})
vote.minusNickname = nickname.value; .then(r => r.status) === 200) {
nickname.value = ""; showMessage("Merci pour ton vote!", "Ton vote a bien été prit en compte.", false);
vote.minusReason = reason.value; }
reason.value = "";
console.log(vote); console.log(vote);
} else { } else {
rightButton.textContent = "À voté!"; rightButton.textContent = "À voté!";
title.textContent = "Vote -"; title.textContent = "Vote -";
current_page = 1; current_page = 1;
vote.plusId = parseInt(select.value);
select.value = null;
vote.plusNickname = nickname.value;
nickname.value = "";
vote.plusReason = reason.value;
reason.value = "";
leftButton.hidden = false; leftButton.hidden = false;
select.value = vote.minusId; select.value = vote.minus_player_id;
nickname.value = vote.minusNickname; nickname.value = vote.minus_nickname;
reason.value = vote.minusReason; reason.value = vote.minus_reason;
} }
}) })
leftButton.addEventListener("click", () => { leftButton.addEventListener("click", () => {
@@ -57,15 +68,28 @@ async function main() {
current_page = 0; current_page = 0;
title.textContent = "Vote +"; title.textContent = "Vote +";
rightButton.textContent = "Suivant"; rightButton.textContent = "Suivant";
vote.minusId = parseInt(select.value);
vote.minusNickname = nickname.value;
vote.minusReason = reason.value;
leftButton.hidden = true; leftButton.hidden = true;
select.value = vote.plusId; select.value = vote.plus_player_id;
nickname.value = vote.plusNickname; nickname.value = vote.plus_nickname;
reason.value = vote.plusReason; reason.value = vote.plus_reason;
} }
}) })
} }
function showMessage(title, description, canBeDismissed) {
let notification = document.getElementById("notification");
notification.hidden = false;
let notificationTitle = document.getElementById("title");
notificationTitle.textContent = title;
let detail = document.getElementById("detail");
detail.textContent = description;
let close = document.getElementById("close");
close.hidden = !canBeDismissed;
close.addEventListener("click", () => {
notification.hidden = true;
})
}
function cookie() {}
let _ = main(); let _ = main();