import { MONTHS } from '../config.js'; export class Countdown { constructor(container) { this.container = container; this.nextEvent = null; this.intervalId = null; } setNextEvent(event) { this.nextEvent = event; this.render(); this.startTicking(); } startTicking() { if (this.intervalId) { clearInterval(this.intervalId); } this.intervalId = setInterval(() => this.updateTime(), 1000); } getTimeUntil(date) { const now = new Date(); const target = new Date(date); target.setHours(14, 0, 0, 0); // Assume 14:00 start time const diff = target - now; if (diff <= 0) { return { days: 0, hours: 0, minutes: 0, seconds: 0, passed: true }; } const days = Math.floor(diff / (1000 * 60 * 60 * 24)); const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((diff % (1000 * 60)) / 1000); return { days, hours, minutes, seconds, passed: false }; } render() { if (!this.nextEvent) { this.container.innerHTML = ''; return; } const date = new Date(this.nextEvent.date); const formattedDate = `${date.getDate()} ${MONTHS[date.getMonth()]}`; this.container.innerHTML = `
Prochain meeting ${this.nextEvent.name} ${this.nextEvent.location.city} ยท ${formattedDate}
-- jours
:
-- heures
:
-- min
:
-- sec
`; this.updateTime(); } updateTime() { if (!this.nextEvent) return; const time = this.getTimeUntil(this.nextEvent.date); const daysEl = this.container.querySelector('[data-unit="days"]'); const hoursEl = this.container.querySelector('[data-unit="hours"]'); const minutesEl = this.container.querySelector('[data-unit="minutes"]'); const secondsEl = this.container.querySelector('[data-unit="seconds"]'); if (daysEl) daysEl.textContent = String(time.days).padStart(2, '0'); if (hoursEl) hoursEl.textContent = String(time.hours).padStart(2, '0'); if (minutesEl) minutesEl.textContent = String(time.minutes).padStart(2, '0'); if (secondsEl) secondsEl.textContent = String(time.seconds).padStart(2, '0'); } destroy() { if (this.intervalId) { clearInterval(this.intervalId); } } }