From 60f37ece676f2164a87955a10704465037969b8f Mon Sep 17 00:00:00 2001 From: maddiebaka Date: Fri, 27 Oct 2023 17:27:21 -0400 Subject: [PATCH] Add progressive enhancement JS for active_language --- app/assets/stylesheets/application.scss | 4 ++++ app/helpers/active_language_helper.rb | 2 +- app/javascript/application.js | 7 +++++++ app/views/layouts/application.html.erb | 12 ++++++++++-- spec/helpers/active_language_helper_spec.rb | 5 ++++- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index fdce93e..5000ced 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -47,3 +47,7 @@ a:hover { display: inline; } +#active_language_submit_tag { + display: none; +} + diff --git a/app/helpers/active_language_helper.rb b/app/helpers/active_language_helper.rb index 6602109..ee7afd9 100644 --- a/app/helpers/active_language_helper.rb +++ b/app/helpers/active_language_helper.rb @@ -3,6 +3,6 @@ module ActiveLanguageHelper def active_language_select_tag languages = Language.all.to_a.delete_if {|language| language.name == "English" } options = options_from_collection_for_select(languages, "id", "name", cookies[:active_language_id] || 1) - select_tag "active_language_id", options + select_tag "active_language_id", options, { id: "active_language_select_tag" } end end diff --git a/app/javascript/application.js b/app/javascript/application.js index 0d7b494..f4c4e40 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -1,3 +1,10 @@ // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails import "@hotwired/turbo-rails" import "controllers" + +let switchLanguageForm = document.querySelector("#active_language_form"); +let switchLanguageSelectTag = document.querySelector("#active_language_select_tag"); + +switchLanguageSelectTag.addEventListener("change", (event) => { + switchLanguageForm.submit(); +}); diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4bdc0e1..3a510a8 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,6 +8,14 @@ <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> <%= javascript_importmap_tags %> + + @@ -19,9 +27,9 @@
|
<%= link_to "Languages", admin_languages_path %>
|
- <%= form_tag '/set_active_language', class: "d-inline" do %> + <%= form_tag '/set_active_language', class: "d-inline", id: "active_language_form" do %> <%= active_language_select_tag %> - <%= submit_tag "Switch Language" %> + <%= submit_tag "Switch Language", id: "active_language_submit_tag" %> <% end %> <% end %> diff --git a/spec/helpers/active_language_helper_spec.rb b/spec/helpers/active_language_helper_spec.rb index a4f36ce..ddc53c3 100644 --- a/spec/helpers/active_language_helper_spec.rb +++ b/spec/helpers/active_language_helper_spec.rb @@ -25,7 +25,10 @@ RSpec.describe ActiveLanguageHelper, type: :helper do it "should not contain an English option" do expect(@tag).to_not include("English") - + end + + it "should include css id #active_language_select_tag" do + expect(@tag).to have_selector(%(select[id="active_language_select_tag"])) end end end