mirror of
https://github.com/GuerillaStudio/compteur-de-greve.git
synced 2024-12-18 05:41:56 +00:00
bind real api
This commit is contained in:
parent
f8c0110223
commit
ea849aacc6
4 changed files with 16 additions and 34 deletions
|
@ -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: {
|
||||
|
|
|
@ -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}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ permalink:
|
|||
<span class="color-pink-2 pb05 inline-block">Contre la réforme des retraites,</span><br>
|
||||
À l’appel de l’ensemble des organisations syndicales, <span class="color-purple-1">le 7 mars</span>
|
||||
<span aria-live="polite">
|
||||
<strong class="counter-number" x-bind="counter">{{count}}</strong>
|
||||
<strong class="counter-number" x-bind="counter">{{count | formatNumber}}</strong>
|
||||
<span class="separator">personnes seront</span>
|
||||
<em class="counter-strikeeeee-lol highlighter-zigzag">en grève<span class="visually-hidden"> !</span></em>
|
||||
</span>
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue