diff --git a/.eleventy.js b/.eleventy.js index 83461dc..26bf3f8 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -18,6 +18,8 @@ module.exports = function (eleventyConfig) { eleventyConfig.addPassthroughCopy("src/js/**/*.js") eleventyConfig.addPassthroughCopy({ "static": "/" }) + eleventyConfig.addFilter("formatNumber", number => number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ")); + return { htmlTemplateEngine: "njk", dir: { diff --git a/src/index.11tydata.js b/src/index.11tydata.js index 0e610f2..f5e32a1 100644 --- a/src/index.11tydata.js +++ b/src/index.11tydata.js @@ -1,3 +1,5 @@ -module.exports = function() { - return Promise.resolve({count: 69420}) +module.exports = async function() { + const response = await fetch("https://c.compteurdegreve.fr/val") + const data = await response.json() + return {count: data.value} } diff --git a/src/index.njk b/src/index.njk index 635cb09..34d9f16 100644 --- a/src/index.njk +++ b/src/index.njk @@ -13,7 +13,7 @@ permalink: Contre la réforme des retraites,
À l’appel de l’ensemble des organisations syndicales, le 7 mars - {{count}} + {{count | formatNumber}} personnes seront en grève ! diff --git a/src/js/script.js b/src/js/script.js index 1842ccf..88ae287 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -1,5 +1,3 @@ -let internalCount = 0 - document.addEventListener('alpine:init', () => { Alpine.data('counter', () => ({ count: null, @@ -57,7 +55,8 @@ document.addEventListener('alpine:init', () => { this.loading = true this.errored = false - incrementCount().then(() => { + incrementCount().then((newCount) => { + this.count = newCount this.participating = true }).catch((error) => { this.errored = true @@ -105,7 +104,7 @@ function subscribeCount(onCount) { function execute() { fetchCount().then(newCount => { if (!unsubscribed) { - timeoutId = setTimeout(execute, 2000) + timeoutId = setTimeout(execute, 5000) onCount(newCount) } }) @@ -122,35 +121,14 @@ function subscribeCount(onCount) { } } - - -// fake api function fetchCount() { - return wait(1000).then(() => { - internalCount += randomInt(0, 9) - return internalCount - }) - + return fetch("https://c.compteurdegreve.fr/val") + .then(res => res.json()) + .then(data => data.value) } function incrementCount() { - return wait(1000).then(() => { - if (!randomInt(0, 1)) { - throw new Error() - } - - internalCount += 1 + randomInt(0, 9) - return internalCount - }) -} - - -function wait(ms) { - return new Promise(res => setTimeout(res, ms)) -} - -function randomInt(min, max) { - min = Math.ceil(min) - max = Math.floor(max) - return Math.floor(Math.random() * (max - min + 1)) + min + return fetch("https://c.compteurdegreve.fr/incr", { method: "POST", mode: "cors", credentials: "include"}) + .then(res => res.json()) + .then(data => data.value) }