main.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import './styles/counter.css'
  2. import { Countdown } from './counter.js'
  3. import { fetchObject } from './fetch.js';
  4. // select elements
  5. const app = document.querySelector('.countdown-timer');
  6. const message = document.querySelector('.message');
  7. const heading = document.querySelector('h1');
  8. const api = new fetchObject("http://localhost:8000/counter", app.dataset.id);
  9. const counter = await api.getCounter();
  10. heading.innerText = counter.name;
  11. // Get the new year
  12. const getEndTime = () => {
  13. return new Date(Date.parse(counter.endTime));
  14. };
  15. const format = (t) => {
  16. return t < 10 ? '0' + t : t;
  17. };
  18. const render = (time) => {
  19. app.innerHTML = `
  20. <div class="count-down">
  21. <div class="timer hidden">
  22. <h2 class="days">${format(time.days)}</h2>
  23. <small>Days</small>
  24. </div>
  25. <div class="timer">
  26. <h2 class="hours">${format(time.hours)}</h2>
  27. <small>Hours</small>
  28. </div>
  29. <div class="timer">
  30. <h2 class="minutes">${format(time.minutes)}</h2>
  31. <small>Minutes</small>
  32. </div>
  33. <div class="timer">
  34. <h2 class="seconds">${format(time.seconds)}</h2>
  35. <small>Seconds</small>
  36. </div>
  37. </div>
  38. `;
  39. };
  40. const showMessage = () => {
  41. message.innerHTML = "Le compteur est terminé";
  42. app.innerHTML = '';
  43. //heading.style.display = 'none';
  44. };
  45. const hideMessage = () => {
  46. message.innerHTML = '';
  47. heading.style.display = 'block';
  48. };
  49. const complete = () => {
  50. console.log("Fonction complete() du main")
  51. showMessage();
  52. };
  53. const countdownTimer = new Countdown(
  54. getEndTime(),
  55. render,
  56. complete
  57. );