diff --git a/app/components/search_component.html.erb b/app/components/search_component.html.erb new file mode 100644 index 000000000..cf5da1898 --- /dev/null +++ b/app/components/search_component.html.erb @@ -0,0 +1,16 @@ +<%= form_with model: @user, url: path(current_user), method: "get", local: true do |f| %> + +<% end %> diff --git a/app/components/search_component.rb b/app/components/search_component.rb new file mode 100644 index 000000000..dbab17245 --- /dev/null +++ b/app/components/search_component.rb @@ -0,0 +1,13 @@ +class SearchComponent < ViewComponent::Base + attr_reader :current_user, :label + + def initialize(current_user:, label:) + @current_user = current_user + @label = label + super + end + + def path(current_user) + current_user.support? ? users_path : users_organisation_path(current_user.organisation) + end +end diff --git a/app/controllers/modules/users_filter.rb b/app/controllers/modules/users_filter.rb index 732c08d43..1b6ba4c53 100644 --- a/app/controllers/modules/users_filter.rb +++ b/app/controllers/modules/users_filter.rb @@ -1,6 +1,6 @@ module Modules::UsersFilter def filtered_users(base_collection) - search_param = params["user-search-field"] + search_param = params["search-field"] if search_param.present? base_collection.search_by(search_param) else diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 75a483cad..17d3b8a3f 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -19,7 +19,7 @@ class OrganisationsController < ApplicationController def users @pagy, @users = pagy(filtered_users(@organisation.users)) - @searched = params["user-search-field"].present? + @searched = params["search-field"].present? render "users/index" end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d0493a739..8e58f7396 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -11,7 +11,7 @@ class UsersController < ApplicationController redirect_to users_organisation_path(current_user.organisation) unless current_user.support? @pagy, @users = pagy(filtered_users(User.all)) - @searched = params["user-search-field"].present? + @searched = params["search-field"].present? respond_to do |format| format.html diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index ab160f49c..44f401515 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -8,23 +8,7 @@ <%= govuk_button_link_to "Invite user", new_user_path, html: { method: :get } %> <% end %> -<% path = current_user.support? ? users_path : users_organisation_path(current_user.organisation) %> -<%= form_with model: @user, url: path, method: "get", local: true do |f| %> - -<% end %> +<%= render SearchComponent.new(current_user:, label: "Search by name or email address") %> <%= govuk_table do |table| %> <%= table.caption(size: "s", classes: %w[govuk-!-text-align-left govuk-!-margin-top-4 govuk-!-margin-bottom-4]) do |caption| %> diff --git a/spec/components/search_component_spec.rb b/spec/components/search_component_spec.rb new file mode 100644 index 000000000..237ff05d6 --- /dev/null +++ b/spec/components/search_component_spec.rb @@ -0,0 +1,19 @@ +require "rails_helper" + +RSpec.describe SearchComponent, type: :component do + let(:current_user) { FactoryBot.create(:user, :support) } + let(:label) { "Search by name or email address" } + let(:page) { Capybara::Node::Simple.new(rendered_component) } + + before do + render_inline(described_class.new(current_user:, label:)) + end + + it "renders a search bar" do + expect(page).to have_field("search-field", type: "search") + end + + it "renders the given label" do + expect(page).to have_content(label) + end +end diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 31448b102..beb81a993 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -359,7 +359,7 @@ RSpec.describe UsersController, type: :request do it "shows a search bar" do follow_redirect! - expect(page).to have_field("user-search-field", type: "search") + expect(page).to have_field("search-field", type: "search") end end @@ -369,7 +369,7 @@ RSpec.describe UsersController, type: :request do let!(:other_org_user) { FactoryBot.create(:user, name: "User 4", email: "joe@other_example.com") } before do - get "/organisations/#{user.organisation.id}/users?user-search-field=#{search_param}" + get "/organisations/#{user.organisation.id}/users?search-field=#{search_param}" end context "when our search string matches case" do @@ -822,12 +822,12 @@ RSpec.describe UsersController, type: :request do end it "shows a search bar" do - expect(page).to have_field("user-search-field", type: "search") + expect(page).to have_field("search-field", type: "search") end context "when a search parameter is passed" do before do - get "/users?user-search-field=#{search_param}" + get "/users?search-field=#{search_param}" end context "when our search term matches a name" do