mirror of
https://github.com/by-jp/www.byjp.me.git
synced 2025-08-10 02:26:08 +01:00
Update claps on page load
This commit is contained in:
parent
48c5b6f9f6
commit
9f7d009abb
3 changed files with 22 additions and 3 deletions
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -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" . -}}
|
||||
|
|
|
@ -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" . -}}
|
||||
|
|
Loading…
Reference in a new issue