diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 7535bd142..f89325b41 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -17,6 +17,7 @@ class LocationsController < ApplicationController @pagy, @locations = pagy(filter_manager.filtered_locations(@scheme.locations, search_term, session_filters)) @total_count = @scheme.locations.size @searched = search_term.presence + @filter_type = "scheme_locations" end def create @@ -301,7 +302,7 @@ private helper_method :return_to_check_your_answers? def filter_manager - FilterManager.new(current_user:, session:, params:, filter_type: "locations") + FilterManager.new(current_user:, session:, params:, filter_type: "scheme_locations") end def session_filters diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index cdcd05c9f..59537bac2 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -2,7 +2,7 @@ class SessionsController < ApplicationController def clear_filters session[session_name_for(params[:filter_type])] = "{}" - redirect_to send("#{params[:filter_type]}_path") + redirect_to send("#{params[:filter_type]}_path", scheme_id: params[:path_params][:scheme_id]) end private diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index 33e109565..e2da5b0ed 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -56,6 +56,17 @@ module FiltersHelper }.freeze end + def location_status_filters + { + "incomplete" => "Incomplete", + "active" => "Active", + "deactivating_soon" => "Deactivating soon", + "activating_soon" => "Activating soon", + "reactivating_soon" => "Reactivating soon", + "deactivated" => "Deactivated", + }.freeze + end + def selected_option(filter, filter_type) return false unless session[session_name_for(filter_type)] @@ -80,9 +91,9 @@ module FiltersHelper applied_filters_count(filter_type).zero? ? "No filters applied" : "#{pluralize(applied_filters_count(filter_type), 'filter')} applied" end - def reset_filters_link(filter_type) + def reset_filters_link(filter_type, path_params = {}) if applied_filters_count(filter_type).positive? - govuk_link_to "Clear", clear_filters_path(filter_type:) + govuk_link_to "Clear", clear_filters_path(filter_type:, path_params:) end end @@ -104,6 +115,8 @@ private end def applied_filters(filter_type) + return {} unless session[session_name_for(filter_type)] + JSON.parse(session[session_name_for(filter_type)]) end diff --git a/app/services/filter_manager.rb b/app/services/filter_manager.rb index 6da12ed56..c8665752d 100644 --- a/app/services/filter_manager.rb +++ b/app/services/filter_manager.rb @@ -97,7 +97,7 @@ class FilterManager new_filters["user"] = current_user.id.to_s if params["assigned_to"] == "you" end - if (filter_type.include?("schemes") || filter_type.include?("users") || filter_type.include?("locations")) && params["status"].present? + if (filter_type.include?("schemes") || filter_type.include?("users") || filter_type.include?("scheme_locations")) && params["status"].present? new_filters["status"] = params["status"] end diff --git a/app/views/locations/_location_filters.html.erb b/app/views/locations/_location_filters.html.erb new file mode 100644 index 000000000..1854d731b --- /dev/null +++ b/app/views/locations/_location_filters.html.erb @@ -0,0 +1,30 @@ +
+
+
+

Filters

+
+ +
+ <%= form_with html: { method: :get } do |f| %> +
+

+ <%= filters_applied_text(@filter_type) %> +

+

+ <%= reset_filters_link(@filter_type, { scheme_id: @scheme.id }) %> +

+
+ + <%= render partial: "filters/checkbox_filter", + locals: { + f:, + options: location_status_filters, + label: "Status", + category: "status", + } %> + + <%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %> + <% end %> +
+
+
diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb index 7641bbd48..e5d3dbab8 100644 --- a/app/views/locations/index.html.erb +++ b/app/views/locations/index.html.erb @@ -23,6 +23,7 @@
+ <%= render partial: "locations/location_filters" %>
<%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index c88b831ec..73972c4c3 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -284,6 +284,43 @@ RSpec.describe "Schemes scheme Features" do end end + context "when filtering locations" do + before do + click_link("Locations") + end + + context "when no filters are selected" do + it "displays the filters component with no clear button" do + expect(page).to have_content("No filters applied") + expect(page).not_to have_content("Clear") + end + end + + context "when I have selected filters" do + before do + check("Active") + check("Incomplete") + click_button("Apply filters") + end + + it "displays the filters component with a correct count and clear button" do + expect(page).to have_content("2 filters applied") + expect(page).to have_content("Clear") + end + + context "when clearing the filters" do + before do + click_link("Clear") + end + + it "clears the filters and displays the filter component as before" do + expect(page).to have_content("No filters applied") + expect(page).not_to have_content("Clear") + end + end + end + end + context "when the user clicks add location" do before do click_link("Locations")