acoeur/themes/acoeur/assets/js/choice.js

19 lines
803 B
JavaScript

document.querySelectorAll('body.users .ac_choice input[type=radio]')
.forEach(elt =>
elt.onchange = () => fetch(document.body.dataset.editUrl + '.choice', {
method: 'PUT',
body: new URLSearchParams(new FormData(elt.form))
})
.then(launchNotes)
.then(() => sleep(1))
.then(() => fetchResult())
.catch(error => console.error('Error sending answer', error)))
const fetchResult = () =>
fetch(document.body.dataset.editUrl + '.result')
.then(response => response.ok && response.text())
.then(result => result && window.location.assign('/users/' + window.location.pathname.split('/')[2] + result + '/'))
.then(() => sleep(1))
.then(() => fetchResult()) // Re-tries fetching result...
.catch(error => console.error('Error receiving result', error))