Adding interactions to clap count

This means that people appreciating my posts on other platforms get a single clap per interaction type (eg. comment or star or repost etc.)
This commit is contained in:
JP Hastings-Spital 2024-06-20 14:25:09 +01:00
parent 2ab15519b2
commit aa3c92b9db
8 changed files with 29 additions and 14 deletions

View file

@ -33,9 +33,10 @@ const performClap = (e) => {
return data;
})
.then(data => {
localStorage.setItem(clapKey(btn.parentElement.action), data.claps);
const clapCount = addInteractions(btn, data.claps);
localStorage.setItem(clapKey(btn.parentElement.action), clapCount);
forEveryClapButton((btn) => {
setClapCount(btn, data.claps);
setClapCount(btn, clapCount);
}, btn.parentElement.action)
btn.parentElement.classList.add('clapped')
btn.disabled = false;
@ -54,6 +55,8 @@ const performClap = (e) => {
const clapKey = (action) => `clap:${(new URL(action)).pathname}`;
const addInteractions = (btn, claps) => (claps + (btn.dataset.interactions || 0));
const setClapCount = (btn, clapCount) => {
if (clapCount === 0) {
return
@ -100,8 +103,9 @@ document.addEventListener("DOMContentLoaded", () => {
return res.json()
})
.then(data => {
const clapCount = addInteractions(btn, data.claps);
forEveryClapButton((btn) => {
setClapCount(btn, data.claps);
setClapCount(btn, clapCount);
}, action)
})
.catch(console.error)

View file

@ -65,12 +65,12 @@ date = ["date", "publishDate", "lastmod", ":git"]
[params.defaultSVG]
post = "document"
bookmarks = "bookmark"
bookmark = "bookmark"
article-bookmark = "bookmark"
game-bookmark = "game"
video-bookmark = "video"
product-bookmark = "package"
notes = "message"
note = "message"
photo = "camera"
calendar = "calendar"
review = "review"
@ -78,8 +78,7 @@ date = ["date", "publishDate", "lastmod", ":git"]
contact = "email"
curiosities = "star"
memex = "brain"
photos = "camera"
reading-lists = "book"
reading-list = "book"
[params.defaultEmoji]
posts = "📔"

View file

@ -1,7 +1,8 @@
{{- $relperma := .url -}}
{{- $style := .style | default "clap" -}}
{{- $interactionCount := partial "interaction-count.obj" page -}}
<form class="claps" action="https://{{ site.Params.clapsHost }}{{ $relperma }}" method="post">
<button type="submit" title="Show appreciation for the post on this page">
<button type="submit" title="Show appreciation for the post on this page" data-interactions="{{ $interactionCount }}">
{{- partial "svg.html" (dict "name" $style) -}}
{{- $url := printf "https://%s" site.Params.clapsHost -}}
{{- $cacheKey := print $url (now.Format "-2006-01-02-15") -}}
@ -10,7 +11,8 @@
{{- errorf "%s" . -}}
{{- else -}}
{{- $data := .Content | transform.Unmarshal -}}
{{- with (index $data $relperma) }}<span>{{ . }}</span>{{ end -}}
{{- $clapCount := add $interactionCount ((index $data $relperma) | default 0) -}}
{{- with $clapCount }}<span>{{ . }}</span>{{ end -}}
{{- end -}}
{{- else -}}
{{- errorf "Unable to get all clap counts" -}}

View file

@ -0,0 +1,8 @@
{{- $interactionIndex := (split .Path "/") | after 1 -}}
{{- $interactionCount := 0 -}}
{{- if gt (len $interactionIndex) 1 -}}
{{- with (index site.Data.interactions $interactionIndex) -}}
{{- $interactionCount = len .interactions -}}
{{- end -}}
{{- end -}}
{{- return $interactionCount -}}

View file

@ -2,7 +2,7 @@
<ul class="menu__inner">
{{- $page := .Page -}}
{{ range .Site.Menus.main -}}
<li class="micro{{ if hasPrefix $page.RelPermalink .URL }} active{{ end }}"><a href="{{ .URL }}" title="{{ .Name }}">{{ partial "svg.html" (dict "name" (index site.Params.defaultSVG .Identifier)) }}<span> {{ .Name }}</span></a></li>
<li class="micro{{ if hasPrefix $page.RelPermalink .URL }} active{{ end }}"><a href="{{ .URL }}" title="{{ .Name }}">{{ partial "svg.html" (dict "name" (index site.Params.defaultSVG (.Identifier | singularize))) }}<span> {{ .Name }}</span></a></li>
{{- end }}
</ul>
</nav>

View file

@ -5,7 +5,7 @@
{{- $posts := where .Site.RegularPages "Section" "in" (slice "posts" "bookmarks" "poetry" "reviews") -}}
{{- range $posts.ByDate.Reverse | first 6 -}}
{{- $postType := partial "post-type.txt" . -}}
{{- $typeSVG := index .Site.Params.defaultSVG ( partial "kebab.txt" $postType) -}}
{{- $typeSVG := index .Site.Params.defaultSVG ( partial "kebab.txt" ($postType | singularize)) -}}
<li>
{{ partial "svg.html" (dict "name" $typeSVG) }}
<a href="{{.RelPermalink}}">

View file

@ -8,7 +8,8 @@
{{- $data := .Content | transform.Unmarshal -}}
{{- $claps := slice -}}
{{- range $path, $count := $data -}}
{{ $claps = $claps | append (dict "path" $path "count" $count) }}
{{- $interactionCount := partial "interaction-count.obj" (site.GetPage $path) -}}
{{ $claps = $claps | append (dict "path" $path "count" (add $interactionCount $count)) }}
{{- end -}}
{{- range (sort $claps "count" "desc" | first $count) -}}
{{- $count := .count -}}

View file

@ -8,14 +8,15 @@
{{- $data := .Content | transform.Unmarshal -}}
{{- $claps := slice -}}
{{- range $path, $count := $data -}}
{{ $claps = $claps | append (dict "path" $path "count" $count) }}
{{- $interactionCount := partial "interaction-count.obj" (site.GetPage $path) -}}
{{ $claps = $claps | append (dict "path" $path "count" (add $interactionCount $count)) }}
{{- end -}}
<ol class="small-post-list">
{{- range (sort $claps "count" "desc" | first $count) -}}
{{- $count := .count -}}
{{- with site.GetPage .path -}}
{{- $postType := partial "post-type.txt" . -}}
{{- $typeSVG := index site.Params.defaultSVG ( partial "kebab.txt" $postType) -}}
{{- $typeSVG := index site.Params.defaultSVG (partial "kebab.txt" ($postType | singularize)) -}}
<li>
{{ partial "svg.html" (dict "name" $typeSVG) }}
<a href="{{.RelPermalink}}">