Skip to content

Commit

Permalink
refacto: update disabled script for fix display bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccadumazert committed Dec 19, 2024
1 parent 715934d commit 2c3f75f
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions assets/js/disabled-with-countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,44 @@ document.addEventListener(

elements.forEach((element) => {
const rawEndDate = element.getAttribute("data-countdown-end-date");

try {
const endDateInSeconds = new Date(rawEndDate).getTime() / 1000;
const nowInSeconds = new Date().getTime() / 1000;
let secondsToEndDate = Math.round(endDateInSeconds - nowInSeconds);
let intervalId;

element.disabled = true;

function updateButtonText() {
const minutes = Math.floor(secondsToEndDate / 60);
const seconds = String(secondsToEndDate % 60).padStart(2, "0");
element.textContent = `Recevoir un nouvel email (disponible dans ${minutes}:${seconds})`;
if (isNaN(endDateInSeconds)) {
console.error("Invalid date provided in data-countdown-end-date.");
return;
}

updateButtonText();
if (secondsToEndDate > 0) {
element.disabled = true;

intervalId = setInterval(function () {
secondsToEndDate--;

if (secondsToEndDate > 0) {
updateButtonText();
} else {
element.disabled = false;
element.textContent = "Recevoir un nouvel email";
clearInterval(intervalId);
function updateButtonText() {
const minutes = Math.floor(secondsToEndDate / 60);
const seconds = String(secondsToEndDate % 60).padStart(2, "0");
element.textContent = `Recevoir un nouvel email (disponible dans ${minutes}:${seconds})`;
}
}, 1000);

updateButtonText();

intervalId = setInterval(function () {
secondsToEndDate--;

if (secondsToEndDate > 0) {
updateButtonText();
} else {
clearInterval(intervalId);
element.disabled = false;
element.textContent = "Recevoir un nouvel email";
}
}, 1000);
} else {
element.disabled = false;
element.textContent = "Recevoir un nouvel email";
}
} catch (error) {
// silently fails
}
Expand Down

0 comments on commit 2c3f75f

Please sign in to comment.