main.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import './styles/counter.css'
  2. import { Countdown } from './counter.js'
  3. import { fetchObject } from './fetch.js';
  4. import { entrypoint } from './entrypoint.local.js';
  5. // select elements
  6. const main = document.querySelector('#app');
  7. const heading = document.createElement('h1');
  8. const message = document.createElement('div');
  9. message.classList.add('message');
  10. const app = document.createElement('div');
  11. app.classList.add('countdown-timer');
  12. main.appendChild(heading);
  13. main.appendChild(app);
  14. main.appendChild(message);
  15. const checkCounter = () => {
  16. const intervalId = setInterval(async () => {
  17. const counter = await api.getCounter();
  18. if (counter.error) {
  19. clearInterval(intervalId);
  20. showErrorMessage(counter.error);
  21. return;
  22. }
  23. heading.innerText = counter.name;
  24. if (counter.state === 'ready') {
  25. hideMessage();
  26. const period = new Date(counter.minutes * 1000 * 60);
  27. render(Countdown.getTime(period));
  28. } else if (counter.state === 'completed') {
  29. const zero = new Date(0);
  30. showMessage();
  31. render(Countdown.getTime(zero));
  32. } else {
  33. clearInterval(intervalId);
  34. const countdownTimer = new Countdown(
  35. getEndTime(counter),
  36. render,
  37. complete
  38. )
  39. }
  40. }, 1000)
  41. }
  42. const api = new fetchObject(entrypoint, main.dataset.id);
  43. checkCounter();
  44. const getEndTime = (counter) => {
  45. return new Date(Date.parse(counter.endTime));
  46. };
  47. const format = (t) => {
  48. return t < 10 ? '0' + t : t;
  49. };
  50. const render = (time) => {
  51. app.innerHTML = `
  52. <div class="count-down">
  53. <div class="timer hidden">
  54. <h2 class="days">${format(time.days)}</h2>
  55. <small>Days</small>
  56. </div>
  57. <div class="timer">
  58. <h2 class="hours">${format(time.hours)}</h2>
  59. <small>Hours</small>
  60. </div>
  61. <div class="timer">
  62. <h2 class="minutes">${format(time.minutes)}</h2>
  63. <small>Minutes</small>
  64. </div>
  65. <div class="timer">
  66. <h2 class="seconds">${format(time.seconds)}</h2>
  67. <small>Seconds</small>
  68. </div>
  69. </div>
  70. `;
  71. };
  72. const showMessage = () => {
  73. message.innerHTML = "Le compteur est terminé";
  74. //app.innerHTML = '';
  75. //heading.style.display = 'none';
  76. };
  77. const hideMessage = () => {
  78. message.innerHTML = '';
  79. heading.style.display = 'block';
  80. };
  81. const complete = () => {
  82. showMessage();
  83. checkCounter();
  84. };
  85. const showErrorMessage = (error) => {
  86. message.innerHTML = 'Erreur ' + error.code + ' - ' + error.message;
  87. const buttonRestart = document.createElement('button');
  88. buttonRestart.innerText = "Réessayer";
  89. app.appendChild(buttonRestart);
  90. buttonRestart.addEventListener('click', (e) => {
  91. buttonRestart.remove();
  92. checkCounter();
  93. })
  94. }