From 6d2d03223a03794d2cef013b2b204b1a4eb983a0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Tue, 14 Jan 2020 23:27:12 +0100 Subject: [PATCH] auto submit therapy form data --- therapies/autosave.js | 48 +++++++++++++++++++++++++++++++++++++ therapies/page.sh | 3 ++- therapies/therapy_draw.js | 2 +- therapies/update_therapy.sh | 8 +++++-- 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 therapies/autosave.js 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 -- 2.39.2