Browse Source

Display the status filter and clear filters

pull/1785/head
Kat 3 years ago
parent
commit
1234ca4516
  1. 3
      app/controllers/locations_controller.rb
  2. 2
      app/controllers/sessions_controller.rb
  3. 17
      app/helpers/filters_helper.rb
  4. 2
      app/services/filter_manager.rb
  5. 30
      app/views/locations/_location_filters.html.erb
  6. 1
      app/views/locations/index.html.erb
  7. 37
      spec/features/schemes_spec.rb

3
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)) @pagy, @locations = pagy(filter_manager.filtered_locations(@scheme.locations, search_term, session_filters))
@total_count = @scheme.locations.size @total_count = @scheme.locations.size
@searched = search_term.presence @searched = search_term.presence
@filter_type = "scheme_locations"
end end
def create def create
@ -301,7 +302,7 @@ private
helper_method :return_to_check_your_answers? helper_method :return_to_check_your_answers?
def filter_manager def filter_manager
FilterManager.new(current_user:, session:, params:, filter_type: "locations") FilterManager.new(current_user:, session:, params:, filter_type: "scheme_locations")
end end
def session_filters def session_filters

2
app/controllers/sessions_controller.rb

@ -2,7 +2,7 @@ class SessionsController < ApplicationController
def clear_filters def clear_filters
session[session_name_for(params[:filter_type])] = "{}" 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 end
private private

17
app/helpers/filters_helper.rb

@ -56,6 +56,17 @@ module FiltersHelper
}.freeze }.freeze
end 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) def selected_option(filter, filter_type)
return false unless session[session_name_for(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" applied_filters_count(filter_type).zero? ? "No filters applied" : "#{pluralize(applied_filters_count(filter_type), 'filter')} applied"
end end
def reset_filters_link(filter_type) def reset_filters_link(filter_type, path_params = {})
if applied_filters_count(filter_type).positive? 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
end end
@ -104,6 +115,8 @@ private
end end
def applied_filters(filter_type) def applied_filters(filter_type)
return {} unless session[session_name_for(filter_type)]
JSON.parse(session[session_name_for(filter_type)]) JSON.parse(session[session_name_for(filter_type)])
end end

2
app/services/filter_manager.rb

@ -97,7 +97,7 @@ class FilterManager
new_filters["user"] = current_user.id.to_s if params["assigned_to"] == "you" new_filters["user"] = current_user.id.to_s if params["assigned_to"] == "you"
end 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"] new_filters["status"] = params["status"]
end end

30
app/views/locations/_location_filters.html.erb

@ -0,0 +1,30 @@
<div class="app-filter-layout__filter">
<div class="app-filter">
<div class="app-filter__header">
<h2 class="govuk-heading-m">Filters</h2>
</div>
<div class="app-filter__content">
<%= form_with html: { method: :get } do |f| %>
<div class="govuk-grid-row" style="white-space: nowrap">
<p class="govuk-grid-column-one-half">
<%= filters_applied_text(@filter_type) %>
</p>
<p class="govuk-!-text-align-right govuk-grid-column-one-half">
<%= reset_filters_link(@filter_type, { scheme_id: @scheme.id }) %>
</p>
</div>
<%= 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 %>
</div>
</div>
</div>

1
app/views/locations/index.html.erb

@ -23,6 +23,7 @@
</div> </div>
<div class="govuk-grid-row"> <div class="govuk-grid-row">
<%= render partial: "locations/location_filters" %>
<div class="govuk-grid-column-two-thirds-from-desktop"> <div class="govuk-grid-column-two-thirds-from-desktop">
<%= govuk_table do |table| %> <%= govuk_table do |table| %>
<%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %>

37
spec/features/schemes_spec.rb

@ -284,6 +284,43 @@ RSpec.describe "Schemes scheme Features" do
end end
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 context "when the user clicks add location" do
before do before do
click_link("Locations") click_link("Locations")

Loading…
Cancel
Save