From 4390edde03ad2af6da977890c81712d9f885bcf5 Mon Sep 17 00:00:00 2001 From: Anna Sidwell Date: Mon, 16 Apr 2018 10:48:36 +1000 Subject: [PATCH] Don't display draft restore prompt after a failed submit attempt (#52) --- apps/map/templates/map/form.html | 33 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/apps/map/templates/map/form.html b/apps/map/templates/map/form.html index 10cfd4d..ad655c9 100644 --- a/apps/map/templates/map/form.html +++ b/apps/map/templates/map/form.html @@ -56,6 +56,8 @@
+ + {% crispy form %} {% endblock %} @@ -479,26 +481,31 @@ function showDraftPrompt(formSaver, serialisedForm) { } function initDrafts() { - var formSaver = new FormSaver({ + const formSaver = new FormSaver({ formId: 'case-study-form', except: [ 'csrfmiddlewaretoken' ] }) + // Use whether the form has errors as a proxy for whether the server has + // returned us data in the form. In this case, don't show a draft. + const formHasErrors = document.getElementById('form_errors').value === 'true' ? true : false + if (!formHasErrors) { // Get the previous draft, if any - apiGetDraft() - .then(response => response.json()) - .then(json => { - // Handle the case where we didn't get a response - if (!json) return + apiGetDraft() + .then(response => response.json()) + .then(json => { + // Handle the case where we didn't get a response + if (!json) return - if (json.version !== 1) { - throw new Error("Bad JSON response version") - } + if (json.version !== 1) { + throw new Error("Bad JSON response version") + } - showDraftPrompt(formSaver, json.data.form) - }).catch(err => { - console.error(err); - }) + showDraftPrompt(formSaver, json.data.form) + }).catch(err => { + console.error(err); + }) + } // Init the button controller var button = new SaveButton(document.querySelector('.savebutton'))