From 03b608565dcc12c199ce8f2c009cf4743475f207 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 8 Sep 2021 13:58:10 -0700 Subject: [PATCH 01/24] Save status drafts in localstorage --- bookwyrm/static/js/status_cache.js | 40 +++++++++++++++++++ bookwyrm/templates/layout.html | 1 + .../snippets/create_status/content_field.html | 3 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 bookwyrm/static/js/status_cache.js diff --git a/bookwyrm/static/js/status_cache.js b/bookwyrm/static/js/status_cache.js new file mode 100644 index 000000000..9d86c313f --- /dev/null +++ b/bookwyrm/static/js/status_cache.js @@ -0,0 +1,40 @@ +/* exported StatusCache */ +/* globals BookWyrm */ + +let StatusCache = new class { + constructor() { + document.querySelectorAll('[data-cache-draft]') + .forEach(t => t.addEventListener('change', this.updateDraft.bind(this))); + document.querySelectorAll('[data-cache-draft]') + .forEach(t => this.populateDraft(t)); + } + + /** + * Update localStorage copy of drafted status + * + * @param {Event} event + * @return {undefined} + */ + updateDraft(event) { + // Used in set reading goal + let key = event.target.dataset.cacheDraft; + let value = event.target.value; + + window.localStorage.setItem(key, value); + } + + /** + * Toggle display of a DOM node based on its value in the localStorage. + * + * @param {object} node - DOM node to toggle. + * @return {undefined} + */ + populateDraft(node) { + // Used in set reading goal + let key = node.dataset.cacheDraft; + let value = window.localStorage.getItem(key); + + node.value = value; + } +}(); + diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index 43ca81c74..e855bf600 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -251,6 +251,7 @@ + {% block scripts %}{% endblock %} diff --git a/bookwyrm/templates/snippets/create_status/content_field.html b/bookwyrm/templates/snippets/create_status/content_field.html index 907467362..c2b383b9a 100644 --- a/bookwyrm/templates/snippets/create_status/content_field.html +++ b/bookwyrm/templates/snippets/create_status/content_field.html @@ -10,7 +10,8 @@ draft: an existing Status object that is providing default values for input fiel {% endcomment %} @@ -32,7 +33,11 @@ uuid: a unique identifier used to make html "id" attributes unique and clarify j
-
diff --git a/bookwyrm/templates/snippets/create_status/review.html b/bookwyrm/templates/snippets/create_status/review.html index 714055a1c..67214f332 100644 --- a/bookwyrm/templates/snippets/create_status/review.html +++ b/bookwyrm/templates/snippets/create_status/review.html @@ -15,7 +15,17 @@ uuid: a unique identifier used to make html "id" attributes unique and clarify j
- +
From b0e7a5e468355690af93f45efc3a877d4691b528 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 8 Sep 2021 19:30:45 -0700 Subject: [PATCH 04/24] Post statuses asynchronously --- bookwyrm/static/js/bookwyrm.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/bookwyrm/static/js/bookwyrm.js b/bookwyrm/static/js/bookwyrm.js index 894b1fb69..9dab7a23d 100644 --- a/bookwyrm/static/js/bookwyrm.js +++ b/bookwyrm/static/js/bookwyrm.js @@ -22,6 +22,12 @@ let BookWyrm = new class { this.interact.bind(this)) ); + document.querySelectorAll('.submit-status') + .forEach(button => button.addEventListener( + 'submit', + this.submitStatus.bind(this)) + ); + document.querySelectorAll('.hidden-form input') .forEach(button => button.addEventListener( 'change', @@ -292,6 +298,27 @@ let BookWyrm = new class { }); } + /** + * Post a status with ajax + * + * @param {Event} event + * @return {undefined} + */ + submitStatus(event) { + event.preventDefault(); + + const bookwyrm = this; + const form = event.currentTarget; + + this.ajaxPost(form).catch(error => { + // @todo Display a notification in the UI instead. + console.warn('Request failed:', error); + }); + + // Clear form data + form.reset(); + } + /** * Submit a form using POST. * From dfaf08584339bb7eafd9500d5297797b5d547e52 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 9 Sep 2021 06:54:34 -0700 Subject: [PATCH 05/24] Move status code into the new file --- bookwyrm/static/js/bookwyrm.js | 27 --------------------------- bookwyrm/static/js/status_cache.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/bookwyrm/static/js/bookwyrm.js b/bookwyrm/static/js/bookwyrm.js index 9dab7a23d..894b1fb69 100644 --- a/bookwyrm/static/js/bookwyrm.js +++ b/bookwyrm/static/js/bookwyrm.js @@ -22,12 +22,6 @@ let BookWyrm = new class { this.interact.bind(this)) ); - document.querySelectorAll('.submit-status') - .forEach(button => button.addEventListener( - 'submit', - this.submitStatus.bind(this)) - ); - document.querySelectorAll('.hidden-form input') .forEach(button => button.addEventListener( 'change', @@ -298,27 +292,6 @@ let BookWyrm = new class { }); } - /** - * Post a status with ajax - * - * @param {Event} event - * @return {undefined} - */ - submitStatus(event) { - event.preventDefault(); - - const bookwyrm = this; - const form = event.currentTarget; - - this.ajaxPost(form).catch(error => { - // @todo Display a notification in the UI instead. - console.warn('Request failed:', error); - }); - - // Clear form data - form.reset(); - } - /** * Submit a form using POST. * diff --git a/bookwyrm/static/js/status_cache.js b/bookwyrm/static/js/status_cache.js index c2445f7a2..70c10e822 100644 --- a/bookwyrm/static/js/status_cache.js +++ b/bookwyrm/static/js/status_cache.js @@ -4,8 +4,15 @@ let StatusCache = new class { constructor() { document.querySelectorAll('[data-cache-draft]') .forEach(t => t.addEventListener('change', this.updateDraft.bind(this))); + document.querySelectorAll('[data-cache-draft]') .forEach(t => this.populateDraft(t)); + + document.querySelectorAll('.submit-status') + .forEach(button => button.addEventListener( + 'submit', + this.submitStatus.bind(this)) + ); } /** @@ -35,5 +42,26 @@ let StatusCache = new class { node.value = value; } + + /** + * Post a status with ajax + * + * @param {Event} event + * @return {undefined} + */ + submitStatus(event) { + event.preventDefault(); + + const bookwyrm = this; + const form = event.currentTarget; + + this.ajaxPost(form).catch(error => { + // @todo Display a notification in the UI instead. + console.warn('Request failed:', error); + }); + + // Clear form data + form.reset(); + } }(); From 053e2cea0dc6a0c5749dc6c4ca491882d9432009 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 9 Sep 2021 07:39:38 -0700 Subject: [PATCH 06/24] 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 70c10e822..10ab18f9f 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 e855bf600..60480b717 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -249,9 +249,11 @@ + + {% block scripts %}{% endblock %} From 49f1226f3a62871f087ff77b0d7bda64c60033f1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 9 Sep 2021 08:06:36 -0700 Subject: [PATCH 07/24] Close modals after submit --- bookwyrm/static/js/status_cache.js | 6 ++++++ .../snippets/reading_modals/finish_reading_modal.html | 2 +- .../snippets/reading_modals/progress_update_modal.html | 2 +- .../snippets/reading_modals/start_reading_modal.html | 2 +- .../snippets/reading_modals/want_to_read_modal.html | 2 +- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/bookwyrm/static/js/status_cache.js b/bookwyrm/static/js/status_cache.js index 10ab18f9f..ee563a153 100644 --- a/bookwyrm/static/js/status_cache.js +++ b/bookwyrm/static/js/status_cache.js @@ -74,6 +74,12 @@ let StatusCache = new class { .forEach(node => window.localStorage.removeItem(node.dataset.cacheDraft)); // Close modals + let modal = form.closest(".modal.is-active"); + if (!!modal) { + modal.getElementsByClassName("modal-close")[0].click(); + } + + // Update buttons } }(); diff --git a/bookwyrm/templates/snippets/reading_modals/finish_reading_modal.html b/bookwyrm/templates/snippets/reading_modals/finish_reading_modal.html index 3a0346931..b041b70aa 100644 --- a/bookwyrm/templates/snippets/reading_modals/finish_reading_modal.html +++ b/bookwyrm/templates/snippets/reading_modals/finish_reading_modal.html @@ -9,7 +9,7 @@ Finish "{{ book_title }}" {% endblock %} {% block modal-form-open %} -
+ {% csrf_token %} {% endblock %} diff --git a/bookwyrm/templates/snippets/reading_modals/progress_update_modal.html b/bookwyrm/templates/snippets/reading_modals/progress_update_modal.html index c766153af..b4ed8e0c6 100644 --- a/bookwyrm/templates/snippets/reading_modals/progress_update_modal.html +++ b/bookwyrm/templates/snippets/reading_modals/progress_update_modal.html @@ -6,7 +6,7 @@ {% endblock %} {% block modal-form-open %} - + {% endblock %} {% block modal-body %} diff --git a/bookwyrm/templates/snippets/reading_modals/start_reading_modal.html b/bookwyrm/templates/snippets/reading_modals/start_reading_modal.html index 099fd9153..827c0a86d 100644 --- a/bookwyrm/templates/snippets/reading_modals/start_reading_modal.html +++ b/bookwyrm/templates/snippets/reading_modals/start_reading_modal.html @@ -9,7 +9,7 @@ Start "{{ book_title }}" {% endblock %} {% block modal-form-open %} - + {% csrf_token %} {% endblock %} diff --git a/bookwyrm/templates/snippets/reading_modals/want_to_read_modal.html b/bookwyrm/templates/snippets/reading_modals/want_to_read_modal.html index 1213b18e9..5dec637be 100644 --- a/bookwyrm/templates/snippets/reading_modals/want_to_read_modal.html +++ b/bookwyrm/templates/snippets/reading_modals/want_to_read_modal.html @@ -9,7 +9,7 @@ Want to Read "{{ book_title }}" {% endblock %} {% block modal-form-open %} - + {% csrf_token %} {% endblock %} From 055cced75b620f77b0bc7c1031f573a4d55191df Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 9 Sep 2021 08:20:55 -0700 Subject: [PATCH 08/24] Close reply panel --- bookwyrm/static/js/status_cache.js | 8 +++++++- bookwyrm/templates/snippets/status/layout.html | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bookwyrm/static/js/status_cache.js b/bookwyrm/static/js/status_cache.js index ee563a153..26b308ac0 100644 --- a/bookwyrm/static/js/status_cache.js +++ b/bookwyrm/static/js/status_cache.js @@ -79,7 +79,13 @@ let StatusCache = new class { modal.getElementsByClassName("modal-close")[0].click(); } - // Update buttons + // Close reply panel + let reply = form.closest(".reply-panel"); + if (!!reply) { + document.querySelector("[data-controls=" + reply.id + "]").click(); + } + + // Update shelve buttons } }(); diff --git a/bookwyrm/templates/snippets/status/layout.html b/bookwyrm/templates/snippets/status/layout.html index 6ffdd0f72..39445d9c1 100644 --- a/bookwyrm/templates/snippets/status/layout.html +++ b/bookwyrm/templates/snippets/status/layout.html @@ -66,7 +66,7 @@ {% block card-bonus %} {% if request.user.is_authenticated and not moderation_mode %} {% with status.id|uuid as uuid %} -
+
+ + +
+