From: Paul Hänsch Date: Tue, 14 Jan 2020 22:27:12 +0000 (+0100) Subject: auto submit therapy form data X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=6d2d03223a03794d2cef013b2b204b1a4eb983a0;p=lobster auto submit therapy form data --- diff --git a/therapies/autosave.js b/therapies/autosave.js new file mode 100644 index 0000000..bf67274 --- /dev/null +++ b/therapies/autosave.js @@ -0,0 +1,48 @@ +var button = document.querySelector('#savebutton'); +var formdata_old = ''; +var formdata = ''; + +function postsubmit(){ + if ( this.status == 200 ) { + console.log('successful auto submit of form data'); + button.setAttribute('style', 'display: none;'); + } else { + console.log('!!! Error response while auto submitting form data'); + button.setAttribute('style', 'display: block;'); + } +} +function failsubmit(){ + console.log('!!! Timeout while auto submitting form data'); + button.setAttribute('style', 'display: block;'); +} + +function formencode(fd){ + var send; + send='autosubmit=false'; + for (var tup of fd.entries()){ + send += '&' + encodeURIComponent(tup[0]) + '=' + encodeURIComponent(tup[1]); + } + return send; +} + +function formsend(){ + var request = new XMLHttpRequest(); + request.open('post', '/therapies/update_therapy.sh'); + request.timeout = 5000; + request.onload = postsubmit; + request.ontimeout = failsubmit; + request.onerror = failsubmit; + request.onabort = failsubmit; + + formdata = formencode(new FormData(document.querySelector('#report'))); + + if ( ! (formdata == formdata_old) ) { + console.log( 'send' ); + request.send( formdata ); + formdata_old = formdata; + } +} + +formdata_old = formencode(new FormData(document.querySelector('#report'))); +button.setAttribute('style', 'display: none;'); +setInterval(formsend, 500); diff --git a/therapies/page.sh b/therapies/page.sh index 2f0136b..c3d8078 100755 --- a/therapies/page.sh +++ b/therapies/page.sh @@ -211,10 +211,11 @@ cat <"$tempfile" +done >"$tempfile" 2>&- if [ "$(POST delete_session)" ]; then n="$(POST delete_session)" @@ -100,4 +100,8 @@ fi mv "$tempfile" "$tpyfile" rm -f "${_DATA}/cache/${tpy%%.*}.vcf.cache" -REDIRECT "/therapies/${tpy%.*}/${tpy#*.}" +if [ "$(POST autosubmit)" = "true" ]; then + printf 'HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n' +else + REDIRECT "/therapies/${tpy%.*}/${tpy#*.}" +fi