File size: 3,459 Bytes
b8952af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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 = `
      <div class="countdown">
        <div class="countdown-header">
          <span class="countdown-label">Prochain meeting</span>
          <span class="countdown-event">${this.nextEvent.name}</span>
          <span class="countdown-location">${this.nextEvent.location.city} · ${formattedDate}</span>
        </div>
        <div class="countdown-timer">
          <div class="countdown-unit">
            <span class="countdown-value" data-unit="days">--</span>
            <span class="countdown-unit-label">jours</span>
          </div>
          <div class="countdown-separator">:</div>
          <div class="countdown-unit">
            <span class="countdown-value" data-unit="hours">--</span>
            <span class="countdown-unit-label">heures</span>
          </div>
          <div class="countdown-separator">:</div>
          <div class="countdown-unit">
            <span class="countdown-value" data-unit="minutes">--</span>
            <span class="countdown-unit-label">min</span>
          </div>
          <div class="countdown-separator">:</div>
          <div class="countdown-unit">
            <span class="countdown-value" data-unit="seconds">--</span>
            <span class="countdown-unit-label">sec</span>
          </div>
        </div>
      </div>
    `;

    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);
    }
  }
}