From 053e2cea0dc6a0c5749dc6c4ca491882d9432009 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 9 Sep 2021 07:39:38 -0700 Subject: [PATCH] Clear localstorage items --- bookwyrm/static/js/status_cache.js | 18 +++++++++++++++--- bookwyrm/templates/layout.html | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/bookwyrm/static/js/status_cache.js b/bookwyrm/static/js/status_cache.js index 70c10e82..10ab18f9 100644 --- a/bookwyrm/static/js/status_cache.js +++ b/bookwyrm/static/js/status_cache.js @@ -1,4 +1,5 @@ /* exported StatusCache */ +/* globals BookWyrm */ let StatusCache = new class { constructor() { @@ -25,6 +26,10 @@ let StatusCache = new class { // Used in set reading goal let key = event.target.dataset.cacheDraft; let value = event.target.value; + if (!value) { + window.localStorage.removeItem(key); + return; + } window.localStorage.setItem(key, value); } @@ -39,6 +44,9 @@ let StatusCache = new class { // Used in set reading goal let key = node.dataset.cacheDraft; let value = window.localStorage.getItem(key); + if (!value) { + return; + } node.value = value; } @@ -51,17 +59,21 @@ let StatusCache = new class { */ submitStatus(event) { event.preventDefault(); - - const bookwyrm = this; const form = event.currentTarget; - this.ajaxPost(form).catch(error => { + BookWyrm.ajaxPost(form).catch(error => { // @todo Display a notification in the UI instead. console.warn('Request failed:', error); }); // Clear form data form.reset(); + + // Clear localstorage + form.querySelectorAll('[data-cache-draft]') + .forEach(node => window.localStorage.removeItem(node.dataset.cacheDraft)); + + // Close modals } }(); diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index e855bf60..60480b71 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -249,9 +249,11 @@ + + {% block scripts %}{% endblock %}