Browse Source

CLDC-2241 Add filter count and clear button (#1768)

* feat: initial commit

* feat: update designs

* feat: actually clear filters

* refactor: don't use underscore in route

* refactor: make filters_count more readable

* feat: add session controller tests

* feat: add more tests

* refactor: simplification

* feat: add bulk upload id to filter count, make nil safe

* feat: add tests (and add other missing ones to sales log controller spec)

* Update flaky test (#1773)

* feat: revert db changes

* refactor: lint

---------

Co-authored-by: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com>
pull/1779/head v0.3.39
natdeanlewissoftwire 1 year ago committed by GitHub
parent
commit
2570c08fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      app/controllers/sessions_controller.rb
  2. 2
      app/frontend/styles/_filter-layout.scss
  3. 30
      app/helpers/filters_helper.rb
  4. 8
      app/views/logs/_log_filters.html.erb
  5. 2
      config/routes.rb
  6. 33
      spec/controllers/sessions_controller_spec.rb
  7. 45
      spec/features/lettings_log_spec.rb
  8. 45
      spec/features/sales_log_spec.rb
  9. 43
      spec/helpers/filters_helper_spec.rb
  10. 6
      spec/requests/lettings_logs_controller_spec.rb
  11. 109
      spec/requests/sales_logs_controller_spec.rb

13
app/controllers/sessions_controller.rb

@ -0,0 +1,13 @@
class SessionsController < ApplicationController
def clear_filters
session[session_name_for(params[:filter_type])] = "{}"
redirect_to send("#{params[:filter_type]}_path")
end
private
def session_name_for(filter_type)
"#{filter_type}_filters"
end
end

2
app/frontend/styles/_filter-layout.scss

@ -5,7 +5,7 @@
.app-filter-layout__filter { .app-filter-layout__filter {
@include govuk-media-query(wide) { @include govuk-media-query(wide) {
float: left; float: left;
min-width: govuk-grid-width("one-quarter"); width: govuk-grid-width("one-quarter");
} }
} }

30
app/helpers/filters_helper.rb

@ -46,9 +46,39 @@ module FiltersHelper
{ "2023": "2023/24", "2022": "2022/23", "2021": "2021/22" } { "2023": "2023/24", "2022": "2022/23", "2021": "2021/22" }
end end
def filters_applied_text(filter_type)
applied_filters_count(filter_type).zero? ? "No filters applied" : "#{pluralize(applied_filters_count(filter_type), 'filter')} applied"
end
def reset_filters_link(filter_type)
if applied_filters_count(filter_type).positive?
govuk_link_to "Clear", clear_filters_path(filter_type:)
end
end
private private
def applied_filters_count(filter_type)
filters_count(applied_filters(filter_type))
end
def applied_filters(filter_type)
JSON.parse(session[session_name_for(filter_type)])
end
def session_name_for(filter_type) def session_name_for(filter_type)
"#{filter_type}_filters" "#{filter_type}_filters"
end end
def filters_count(filters)
filters.each.sum do |category, category_filters|
if %w[status years bulk_upload_id].include?(category)
category_filters.count(&:present?)
elsif %w[user organisation].include?(category)
category_filters != "all" ? 1 : 0
else
0
end
end
end
end end

8
app/views/logs/_log_filters.html.erb

@ -8,6 +8,14 @@
<%= form_with html: { method: :get } do |f| %> <%= form_with html: { method: :get } do |f| %>
<% all_or_yours = { "all": { label: "All" }, "yours": { label: "Yours" } } %> <% all_or_yours = { "all": { label: "All" }, "yours": { label: "Yours" } } %>
<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) %>
</p>
</div>
<% if bulk_upload_options(@bulk_upload).present? %> <% if bulk_upload_options(@bulk_upload).present? %>
<%= render partial: "filters/checkbox_filter", <%= render partial: "filters/checkbox_filter",
locals: { locals: {

2
config/routes.rb

@ -54,6 +54,8 @@ Rails.application.routes.draw do
get "/download-22-23-sales-bulk-upload-template", to: "start#download_22_23_sales_bulk_upload_template" get "/download-22-23-sales-bulk-upload-template", to: "start#download_22_23_sales_bulk_upload_template"
get "/download-22-23-sales-bulk-upload-specification", to: "start#download_22_23_sales_bulk_upload_specification" get "/download-22-23-sales-bulk-upload-specification", to: "start#download_22_23_sales_bulk_upload_specification"
get "clear-filters", to: "sessions#clear_filters"
resource :account, only: %i[show edit], controller: "users" do resource :account, only: %i[show edit], controller: "users" do
get "edit/password", to: "users#edit_password" get "edit/password", to: "users#edit_password"
end end

33
spec/controllers/sessions_controller_spec.rb

@ -0,0 +1,33 @@
require "rails_helper"
RSpec.describe SessionsController do
describe "#clear_filters" do
context "when filter_type is lettings_logs" do
let(:filter_type) { "lettings_logs" }
it "clears only lettings filters" do
session[:lettings_logs_filters] = "{'some_category':'some_filter'}"
session[:sales_logs_filters] = "{'some_other_category':'some_other_filter'}"
get :clear_filters, params: { filter_type: }
expect(session[:lettings_logs_filters]).to eq("{}")
expect(session[:sales_logs_filters]).to eq("{'some_other_category':'some_other_filter'}")
end
end
context "when filter_type is sales_logs" do
let(:filter_type) { "sales_logs" }
it "clears only sales filters" do
session[:lettings_logs_filters] = "{'some_category':'some_filter'}"
session[:sales_logs_filters] = "{'some_other_category':'some_other_filter'}"
get :clear_filters, params: { filter_type: }
expect(session[:lettings_logs_filters]).to eq("{'some_category':'some_filter'}")
expect(session[:sales_logs_filters]).to eq("{}")
end
end
end
end

45
spec/features/lettings_log_spec.rb

@ -57,6 +57,51 @@ RSpec.describe "Lettings Log Features" do
end end
end end
context "when filtering logs" do
let(:user) { create(:user, last_sign_in_at: Time.zone.now) }
context "when I am signed in" do
before do
visit("/lettings-logs")
fill_in("user[email]", with: user.email)
fill_in("user[password]", with: user.password)
click_button("Sign in")
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("Not started")
check("In progress")
choose("Yours")
click_button("Apply filters")
end
it "displays the filters component with a correct count and clear button" do
expect(page).to have_content("3 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
end
context "when the signed is user is a Support user" do context "when the signed is user is a Support user" do
let(:organisation) { create(:organisation, name: "User org") } let(:organisation) { create(:organisation, name: "User org") }
let(:support_user) { create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) } let(:support_user) { create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) }

45
spec/features/sales_log_spec.rb

@ -94,6 +94,51 @@ RSpec.describe "Sales Log Features" do
end end
end end
context "when filtering logs" do
let(:user) { create(:user, last_sign_in_at: Time.zone.now) }
context "when I am signed in" do
before do
visit("/sales-logs")
fill_in("user[email]", with: user.email)
fill_in("user[password]", with: user.password)
click_button("Sign in")
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("Not started")
check("In progress")
choose("Yours")
click_button("Apply filters")
end
it "displays the filters component with a correct count and clear button" do
expect(page).to have_content("3 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
end
context "when signed in as a support user" do context "when signed in as a support user" do
let(:devise_notify_mailer) { DeviseNotifyMailer.new } let(:devise_notify_mailer) { DeviseNotifyMailer.new }
let(:notify_client) { instance_double(Notifications::Client) } let(:notify_client) { instance_double(Notifications::Client) }

43
spec/helpers/filters_helper_spec.rb

@ -132,7 +132,7 @@ RSpec.describe FiltersHelper do
end end
end end
context "when a range of filters are applied" do context "when a range of filters is applied" do
let(:filters) do let(:filters) do
{ {
"user" => "all", "user" => "all",
@ -204,4 +204,45 @@ RSpec.describe FiltersHelper do
) )
end end
end end
describe "#filters_applied_text" do
let(:filter_type) { "lettings_logs" }
let(:result) { filters_applied_text(filter_type) }
let(:serialised_filters) { filters&.to_json }
let(:filters) { nil }
before do
session[:lettings_logs_filters] = serialised_filters if serialised_filters
end
context "when no filters are applied" do
let(:filters) do
{
"user" => "all",
"status" => [""],
"years" => [""],
"organisation" => "all",
}
end
it "returns the correct filters count" do
expect(result).to eq "No filters applied"
end
end
context "when a range of filters is applied" do
let(:filters) do
{
"user" => "all",
"status" => %w[in_progress completed],
"years" => [""],
"organisation" => 2,
}
end
it "returns the correct filters count" do
expect(result).to eq "3 filters applied"
end
end
end
end end

6
spec/requests/lettings_logs_controller_spec.rb

@ -498,6 +498,12 @@ RSpec.describe LettingsLogsController, type: :request do
expect(page).not_to have_content("Status") expect(page).not_to have_content("Status")
end end
it "has correct filter count and clear button" do
get "/lettings-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).to have_content("1 filter applied")
expect(page).to have_content("Clear")
end
it "hides button to create a new log" do it "hides button to create a new log" do
get "/lettings-logs?bulk_upload_id[]=#{bulk_upload.id}" get "/lettings-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).not_to have_content("Create a new lettings log") expect(page).not_to have_content("Create a new lettings log")

109
spec/requests/sales_logs_controller_spec.rb

@ -350,6 +350,115 @@ RSpec.describe SalesLogsController, type: :request do
expect(page).to have_link(sales_log_2023.id.to_s) expect(page).to have_link(sales_log_2023.id.to_s)
end end
end end
context "with bulk_upload_id filter" do
context "with bulk upload that belongs to current user" do
let(:organisation) { create(:organisation) }
let(:user) { create(:user, organisation:) }
let(:bulk_upload) { create(:bulk_upload, :sales, user:) }
let!(:included_log) { create(:sales_log, :in_progress, bulk_upload:, owning_organisation: organisation) }
let!(:excluded_log) { create(:sales_log, :in_progress, owning_organisation: organisation) }
it "returns logs only associated with the bulk upload" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).to have_content(included_log.id)
expect(page).not_to have_content(excluded_log.id)
end
it "dislays how many logs remaining to fix" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).to have_content("You need to fix 1 log")
end
it "displays filter" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).to have_content("With logs from bulk upload")
end
it "hides collection year filter" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).not_to have_content("Collection year")
end
it "hides status filter" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).not_to have_content("Status")
end
it "has correct filter count and clear button" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).to have_content("1 filter applied")
expect(page).to have_content("Clear")
end
it "hides button to create a new log" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).not_to have_content("Create a new sales log")
end
it "displays card with help info" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).to have_content("The following logs are from your recent bulk upload")
end
it "displays meta info about the bulk upload" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).to have_content(bulk_upload.filename)
expect(page).to have_content(bulk_upload.created_at.to_fs(:govuk_date_and_time))
end
end
context "with bulk upload that belongs to another user" do
let(:organisation) { create(:organisation) }
let(:user) { create(:user, organisation:) }
let(:other_user) { create(:user, organisation:) }
let(:bulk_upload) { create(:bulk_upload, :sales, user: other_user) }
let!(:excluded_log) { create(:sales_log, bulk_upload:, owning_organisation: organisation) }
let!(:also_excluded_log) { create(:sales_log, owning_organisation: organisation) }
it "does not return any logs" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).not_to have_content(excluded_log.id)
expect(page).not_to have_content(also_excluded_log.id)
end
end
context "when bulk upload has been resolved" do
let(:organisation) { create(:organisation) }
let(:user) { create(:user, organisation:) }
let(:bulk_upload) { create(:bulk_upload, :sales, user:) }
it "redirects to resume the bulk upload" do
get "/sales-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(response).to redirect_to(resume_bulk_upload_sales_result_path(bulk_upload))
end
end
end
context "without bulk_upload_id" do
it "does not display filter" do
get "/sales-logs"
expect(page).not_to have_content("With logs from bulk upload")
end
it "displays button to create a new log" do
get "/sales-logs"
expect(page).to have_content("Create a new sales log")
end
it "does not display card with help info" do
get "/sales-logs"
expect(page).not_to have_content("The following logs are from your recent bulk upload")
end
end
end end
end end

Loading…
Cancel
Save