Poll for notifications

This commit is contained in:
Mouse Reeve
2021-01-18 16:32:02 -08:00
parent dda4698a84
commit 394666357f
5 changed files with 45 additions and 5 deletions

View File

@ -31,8 +31,29 @@ window.onload = function() {
// hidden submit button in a form
document.querySelectorAll('.hidden-form input')
.forEach(t => t.onchange = revealForm);
// polling
document.querySelectorAll('[data-poll]')
.forEach(t => setInterval(function () { polling(t) }, 10000));
};
function polling(el) {
// poll the endpoint
fetch('/api/updates/' + el.getAttribute('data-poll'), {
method : "GET",
}).then(response => response.json())
.then(data => updateCountElement(el, data));
}
function updateCountElement(el, data) {
const count = data[el.getAttribute('data-poll')];
if (count) {
removeClass(el, 'hidden');
el.innerHTML = count;
}
}
function revealForm(e) {
var hidden = e.currentTarget.closest('.hidden-form').getElementsByClassName('hidden')[0];
if (hidden) {