From 4856bf9dc903a4f212067dc622c47b3bf78336e0 Mon Sep 17 00:00:00 2001 From: Jennifer Vendetti Date: Wed, 13 Dec 2023 14:07:23 -0800 Subject: [PATCH] Fix Uncaught TypeError on notes emails subscribe See #298 --- app/assets/javascripts/bp_notes.js | 15 +++++++++++---- app/helpers/notes_helper.rb | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/bp_notes.js b/app/assets/javascripts/bp_notes.js index 7862380f3..29c0a1248 100644 --- a/app/assets/javascripts/bp_notes.js +++ b/app/assets/javascripts/bp_notes.js @@ -357,10 +357,17 @@ function subscribeToNotes(button) { var subbedVal = (isSubbed == "true") ? "false" : "true"; jQuery("a.subscribe_to_notes").attr("data-bp_is_subbed", subbedVal); - // Change button text - var txt = jQuery("a.subscribe_to_notes span.ui-button-text").html(); - var newButtonText = txt.match("Unsubscribe") ? txt.replace("Unsubscribe", "Subscribe") : txt.replace("Subscribe", "Unsubscribe"); - jQuery("a.subscribe_to_notes span.ui-button-text").html(newButtonText); + /* + * Update link text. + * Note that there are two links that allow users to subscribe/unsubscribe from notes emails. Given any ontology, + * one link is located on the top-level Notes tab, and the other link is located on the Notes sub-tab on the + * right-hand side of the Classes tab. Both links are handled by the subscriptions controller, and the text of + * both should be updated on success. + */ + const matches = document.querySelectorAll('a.subscribe_to_notes'); + matches.forEach(match => { + match.textContent = (subbedVal === 'true') ? 'Unsubscribe from notes emails' : 'Subscribe to notes emails'; + }); }, error: function(data) { jQuery(".notes_subscribe_spinner").hide(); diff --git a/app/helpers/notes_helper.rb b/app/helpers/notes_helper.rb index 7313a4f03..f1f89cedb 100644 --- a/app/helpers/notes_helper.rb +++ b/app/helpers/notes_helper.rb @@ -148,14 +148,14 @@ def subscribe_button(ontology_id) ont = LinkedData::Client::Models::Ontology.find_by_acronym(ontology_id).first end subscribed = subscribed_to_ontology?(ont.acronym, user) # application_helper - sub_text = subscribed ? "Unsubscribe" : "Subscribe" + sub_text = subscribed ? "Unsubscribe from" : "Subscribe to" params = "data-bp_ontology_id='#{ont.acronym}' data-bp_is_subbed='#{subscribed}' data-bp_user_id='#{user.id}'" rescue # pass, fallback init done above begin block to scope parameters beyond the begin/rescue block end spinner = '' error = "" - return "#{sub_text} to notes emails #{spinner} #{error}".html_safe + return "#{sub_text} notes emails #{spinner} #{error}".html_safe end def delete_button