Add voting system

This commit is contained in:
Joshua Goins 2023-03-28 12:53:02 -04:00
parent ed404744fe
commit 0e7bd2d0ca
3 changed files with 36 additions and 1 deletions

View file

@ -0,0 +1,18 @@
function refresh(slug) {
fetch('https://voting.redstrate.com/votes/view/' + slug)
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data)
document.getElementById("num-votes").innerHTML = data["votes"] + " votes";
});
}
function addVote(slug) {
const request = new Request("https://voting.redstrate.com/votes/submit/" + slug, {
method: "POST"
});
fetch(request)
.then((response) => { refresh(slug) });
}

View file

@ -4,7 +4,7 @@
<hr> <hr>
{{ with resources.Get .Params.filename }} {{ with resources.Get .Params.filename }}
<img class="article-img" alt="{{ $.Params.alt_text }}" title="{{ $.Params.alt_text }}" src="{{ .RelPermalink }}"/> <img class="article-img" style="display: block; max-height: 1000px; margin-left: auto; margin-right: auto;" alt="{{ $.Params.alt_text }}" title="{{ $.Params.alt_text }}" src="{{ .RelPermalink }}"/>
{{ end }} {{ end }}
<table> <table>
@ -78,6 +78,7 @@
</tr> </tr>
{{ end }} {{ end }}
{{ partial "voting" . }}
</table> </table>

View file

@ -0,0 +1,16 @@
<div style="width: 100px; margin-top: 20px; margin-bottom: 20px; margin-left: auto; margin-right: auto">
<button style="display: block; margin-left: auto; margin-right: auto" onClick="addVote({{ .Slug }})">Cool!</button>
<p style="text-align: center" id="num-votes">...</p>
</div>
{{ $site := resources.Get "js/voting.js" }}
{{ if hugo.IsProduction }}
{{ $site = $site | minify | fingerprint | resources.PostProcess }}
{{ end }}
<script src="{{ $site.RelPermalink }}" integrity="{{ $site.Data.Integrity }}"></script>
<script type="text/javascript">
refresh({{ .Slug }})
</script>