Skip to content

Commit

Permalink
Remove jQuery; make voting save on radio click
Browse files Browse the repository at this point in the history
  • Loading branch information
rixx committed Nov 14, 2023
1 parent 05d8a52 commit fa1fcb2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 41 deletions.
61 changes: 22 additions & 39 deletions pretalx_public_voting/static/pretalx_public_voting/vote.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
'use strict'
document.addEventListener('DOMContentLoaded', () => {
const saveIndicator = document.querySelector("#js-save")
const saving = saveIndicator.querySelector(".badge-primary")
const saved = saveIndicator.querySelector(".badge-success")
const savingSpinner = document.querySelector(".fa-spin")
const form = document.querySelector("form")

let voteDirty = false;

$(() => {
const csrftoken = getCookie("pretalx_csrftoken")
const saveIndicator = $("#js-save")
const saving = saveIndicator.children(".badge-primary")
const saved = saveIndicator.children(".badge-success")

$('label').on('click', (event) => {
$(".fa-spin").removeClass("d-none")
saved.addClass("invisible")
saving.removeClass("invisible")
window.setTimeout(() => {
const form = $("form")
$.post(form.attr('action'), form.serialize(), function(res){
$(".fa-spin").addClass("d-none")
saved.removeClass("invisible")
saving.addClass("invisible")
})
}, 5)
return true
document.querySelectorAll('input[type="radio"]').forEach((input) => {
input.addEventListener('change', (event) => {
savingSpinner.classList.remove("d-none")
saved.classList.add("d-none")
saving.classList.remove("d-none")
window.setTimeout(() => {
fetch(form.action, {
method: 'POST',
body: new FormData(form),
}).then((res) => {
savingSpinner.classList.add("d-none")
saved.classList.remove("d-none")
saving.classList.add("d-none")
})
}, 5)
})
})

})

const getCookie = (name) => {
let cookieValue = null
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';')
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim()
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1))
break
}
}
}
return cookieValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ <h3 class="card-title">{{ submission.title }}</h3>
<div id="save-bar">
<i class="fa fa-spinner fa-spin d-none"></i>
<div id="js-save" class="m-2">
<span class="badge badge-primary invisible">Saving…</span>
<span class="badge badge-success invisible">Saved!</span>
<span class="badge badge-primary d-none">Saving…</span>
<span class="badge badge-success d-none">Saved!</span>
</div>
<noscript>
<button class="btn btn-lg btn-info" name="action" value="manual">{% trans "Save!" %}</button>
Expand Down

0 comments on commit fa1fcb2

Please sign in to comment.