From 2c3f75fdc0bce08f378f6d4ff90a8cb4139cfa5e Mon Sep 17 00:00:00 2001 From: rebeccadumazert <rebeccadumazert@hotmail.fr> Date: Thu, 19 Dec 2024 02:00:32 +0100 Subject: [PATCH] refacto: update disabled script for fix display bug --- assets/js/disabled-with-countdown.js | 45 +++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/assets/js/disabled-with-countdown.js b/assets/js/disabled-with-countdown.js index 3076b320..750a497e 100644 --- a/assets/js/disabled-with-countdown.js +++ b/assets/js/disabled-with-countdown.js @@ -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 }