From 3e30eae0d3f95a25780058a1c2c35c8dbd7d3f59 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Wed, 18 May 2022 12:15:00 +0100 Subject: [PATCH] Remove filter value if hidden --- .../conditional_filter_controller.js | 13 ++++++++++ app/frontend/controllers/index.js | 3 +++ app/views/filters/_select_filter.html.erb | 2 +- spec/features/user_spec.rb | 24 +++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 app/frontend/controllers/conditional_filter_controller.js diff --git a/app/frontend/controllers/conditional_filter_controller.js b/app/frontend/controllers/conditional_filter_controller.js new file mode 100644 index 000000000..f4fee7eb9 --- /dev/null +++ b/app/frontend/controllers/conditional_filter_controller.js @@ -0,0 +1,13 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + initialize() { + this.clearIfHidden() + } + + clearIfHidden() { + if(this.element.style["display"] == "none") { + this.element.value = "" + } + } +} diff --git a/app/frontend/controllers/index.js b/app/frontend/controllers/index.js index 7c597baa9..d182110a0 100644 --- a/app/frontend/controllers/index.js +++ b/app/frontend/controllers/index.js @@ -6,6 +6,9 @@ import { application } from "./application" import AccessibleAutocompleteController from "./accessible_autocomplete_controller.js" application.register("accessible-autocomplete", AccessibleAutocompleteController) +import ConditionalFilterController from "./conditional_filter_controller.js" +application.register("conditional-filter", ConditionalFilterController) + import ConditionalQuestionController from "./conditional_question_controller.js" application.register("conditional-question", ConditionalQuestionController) diff --git a/app/views/filters/_select_filter.html.erb b/app/views/filters/_select_filter.html.erb index e2c71988e..fa2dbf539 100644 --- a/app/views/filters/_select_filter.html.erb +++ b/app/views/filters/_select_filter.html.erb @@ -4,4 +4,4 @@ :name, label: { hidden: secondary }, options: { disabled: [""], selected: selected_option(category) }, - "data-controller": "accessible-autocomplete" %> + "data-controller": ["accessible-autocomplete", "conditional-filter"] %> diff --git a/spec/features/user_spec.rb b/spec/features/user_spec.rb index dc4a24591..e4dae7490 100644 --- a/spec/features/user_spec.rb +++ b/spec/features/user_spec.rb @@ -524,5 +524,29 @@ RSpec.describe "User Features" do click_button("Send email") end end + + context "when viewing logs" do + context "when filtering by organisation and then switching back to all organisations", js: true do + let!(:organisation) { FactoryBot.create(:organisation, name: "Filtered Org") } + + before do + allow(SecureRandom).to receive(:random_number).and_return(otp) + click_button("Sign in") + fill_in("code", with: otp) + click_button("Submit") + end + + it "clears the previously selected organisation value" do + visit("/logs") + choose("organisation-select-specific-org-field", allow_label_click: true) + find("#organisation-field").click.native.send_keys("F", "i", "l", "t", :down, :enter) + click_button("Apply filters") + expect(page).to have_current_path("/logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&organisation_select=specific_org&organisation=#{organisation.id}") + choose("organisation-select-all-field", allow_label_click: true) + click_button("Apply filters") + expect(page).to have_current_path("/logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&organisation_select=all") + end + end + end end end