|
@@ -1,19 +1,25 @@
|
|
|
import '../styles/counter.css'
|
|
|
import { Countdown } from './counter.js'
|
|
|
+import { fetchObject } from './fetch.js';
|
|
|
+
|
|
|
+// select elements
|
|
|
+const app = document.querySelector('.countdown-timer');
|
|
|
+const message = document.querySelector('.message');
|
|
|
+const heading = document.querySelector('h1');
|
|
|
+
|
|
|
+const api = new fetchObject("http://localhost:8000/counter", app.dataset.id);
|
|
|
+const counter = await api.getCounter();
|
|
|
+
|
|
|
+heading.innerText = counter.name;
|
|
|
|
|
|
const buttonInit = document.querySelector('#init');
|
|
|
const buttonStart = document.querySelector('#start');
|
|
|
|
|
|
// Get the new year
|
|
|
-const getNewYear = () => {
|
|
|
- const currentYear = new Date().getFullYear();
|
|
|
- return new Date(`January 01 ${currentYear + 1} 00:00:00`);
|
|
|
+const getEndTime = () => {
|
|
|
+ return new Date(Date.parse(counter.endTime));
|
|
|
};
|
|
|
|
|
|
-// select elements
|
|
|
-const app = document.querySelector('.countdown-timer');
|
|
|
-const message = document.querySelector('.message');
|
|
|
-const heading = document.querySelector('h1');
|
|
|
|
|
|
const format = (t) => {
|
|
|
return t < 10 ? '0' + t : t;
|
|
@@ -22,7 +28,7 @@ const format = (t) => {
|
|
|
const render = (time) => {
|
|
|
app.innerHTML = `
|
|
|
<div class="count-down">
|
|
|
- <div class="timer">
|
|
|
+ <div class="timer hidden">
|
|
|
<h2 class="days">${format(time.days)}</h2>
|
|
|
<small>Days</small>
|
|
|
</div>
|
|
@@ -44,9 +50,9 @@ const render = (time) => {
|
|
|
|
|
|
|
|
|
const showMessage = () => {
|
|
|
- message.innerHTML = `Terminé !`;
|
|
|
+ message.innerHTML = "Le compteur est terminé";
|
|
|
app.innerHTML = '';
|
|
|
- heading.style.display = 'none';
|
|
|
+ //heading.style.display = 'none';
|
|
|
};
|
|
|
|
|
|
const hideMessage = () => {
|
|
@@ -55,18 +61,12 @@ const hideMessage = () => {
|
|
|
};
|
|
|
|
|
|
const complete = () => {
|
|
|
+ console.log("Fonction complete() du main")
|
|
|
showMessage();
|
|
|
-
|
|
|
- // restart the countdown after showing the
|
|
|
- // greeting message for a day ()
|
|
|
- setTimeout(() => {
|
|
|
- hideMessage();
|
|
|
- countdownTimer.setExpiredDate(getNewYear());
|
|
|
- }, 1000 * 60 * 60 * 24);
|
|
|
};
|
|
|
|
|
|
const countdownTimer = new Countdown(
|
|
|
- getNewYear(),
|
|
|
+ getEndTime(),
|
|
|
render,
|
|
|
complete
|
|
|
);
|