Update claps on page load

This commit is contained in:
JP Hastings-Spital 2024-04-07 11:28:45 +01:00
parent 48c5b6f9f6
commit 9f7d009abb
3 changed files with 22 additions and 3 deletions

View file

@ -73,12 +73,31 @@ const forEveryClapButton = (fn, sameAction = '') => {
}
document.addEventListener("DOMContentLoaded", () => {
let toCheck = []
forEveryClapButton((btn) => {
const lastClappedTo = localStorage.getItem(clapKey(btn.parentElement.action));
const action = btn.parentElement.action
const lastClappedTo = localStorage.getItem(clapKey(action));
toCheck.push(action)
if (lastClappedTo) {
btn.parentElement.classList.add('clapped')
setClapCount(btn, lastClappedTo)
}
btn.addEventListener("click", performClap)
})
toCheck.forEach((action) => {
fetch(action, { method: 'GET', headers: new Headers({ 'Accept': 'application/json' }) })
.then(res => {
if (res.status !== 200) {
throw new Error(`Got HTTP ${res.status} while trying to retrieve claps`)
}
return res.json()
})
.then(data => {
forEveryClapButton((btn) => {
setClapCount(btn, data.claps);
}, action)
})
.catch(console.error)
})
})

View file

@ -4,7 +4,7 @@
<button type="submit" title="Show appreciation for the post on this page">
{{- partial "svg.html" (dict "name" $style) -}}
{{- $url := printf "https://%s" site.Params.clapsHost -}}
{{- $cacheKey := print $url (now.Format "-2006-01-02") -}}
{{- $cacheKey := print $url (now.Format "-2006-01-02-15") -}}
{{- with resources.GetRemote $url (dict "key" $cacheKey) -}}
{{- with .Err -}}
{{- errorf "%s" . -}}

View file

@ -1,6 +1,6 @@
{{- $count := .Get 0 | default 5 -}}
{{- $url := printf "https://%s" site.Params.clapsHost -}}
{{- $cacheKey := print $url (now.Format "-2006-01-02") -}}
{{- $cacheKey := print $url (now.Format "-2006-01-02-15") -}}
{{- with resources.GetRemote $url (dict "key" $cacheKey) -}}
{{- with .Err -}}
{{- errorf "%s" . -}}