From 08ec80d6daec0395d6132176e04a4b808eb3a61d Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 4 Apr 2022 15:16:36 +0100 Subject: [PATCH] Add a filter for status --- app/controllers/case_logs_controller.rb | 15 ++- app/helpers/filters_helper.rb | 8 ++ app/models/case_log.rb | 1 + app/views/case_logs/_log_list.html.erb | 147 ++++++++++++--------- config/routes.rb | 2 + spec/helpers/filters_helper_spec.rb | 26 ++++ spec/requests/case_logs_controller_spec.rb | 47 +++++++ 7 files changed, 185 insertions(+), 61 deletions(-) create mode 100644 app/helpers/filters_helper.rb create mode 100644 spec/helpers/filters_helper_spec.rb diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 03000dfca..19d12b774 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -7,7 +7,7 @@ class CaseLogsController < ApplicationController before_action :find_resource, except: %i[create index edit] def index - @pagy, @case_logs = pagy(current_user.case_logs) + @pagy, @case_logs = pagy(filtered_case_logs) respond_to do |format| format.html @@ -78,6 +78,11 @@ class CaseLogsController < ApplicationController end end + def filter + cookies[:case_logs_filters] = { status: params[:status] }.to_json + redirect_back(fallback_location: root_path) + end + private API_ACTIONS = %w[create show update destroy].freeze @@ -117,4 +122,12 @@ private def find_resource @case_log = CaseLog.find_by(id: params[:id]) end + + def filtered_case_logs + user_case_logs = current_user.case_logs + status_filter = JSON.parse(cookies[:case_logs_filters])["status"] if cookies[:case_logs_filters].present? + return user_case_logs unless status_filter + + user_case_logs.filter_by_status(status_filter) + end end diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb new file mode 100644 index 000000000..aaebeba57 --- /dev/null +++ b/app/helpers/filters_helper.rb @@ -0,0 +1,8 @@ +module FiltersHelper + def filter_selected?(filter) + return true unless cookies[:case_logs_filters] + + selected_filters = JSON.parse(cookies[:case_logs_filters]) + selected_filters["status"].present? && selected_filters["status"].include?(filter.to_s) + end +end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 6b602362f..aa926d5b4 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -34,6 +34,7 @@ class CaseLog < ApplicationRecord belongs_to :managing_organisation, class_name: "Organisation" scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) } + scope :filter_by_status, ->(status) { where status: status } AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze OPTIONAL_FIELDS = %w[postcode_known la_known first_time_property_let_as_social_housing tenant_code propcode].freeze diff --git a/app/views/case_logs/_log_list.html.erb b/app/views/case_logs/_log_list.html.erb index 3cb30289f..bf171cf51 100644 --- a/app/views/case_logs/_log_list.html.erb +++ b/app/views/case_logs/_log_list.html.erb @@ -1,61 +1,88 @@ -
-
- - <%= pagy.count %> total <%= title.downcase %> - - Download (CSV) -
-
- - - - - - - - - - <% if current_user.support? %> - - +
+
+
+
+

Filters

+
+
+
+ <%= form_with url: "/logs/filter", html: { method: :post }, builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= f.govuk_check_boxes_fieldset :status do %> + <% statuses = {in_progress: "In progress", submitted: "Completed", archived: "Not started"} %> + <% statuses.map do |key, option| %> + <%= f.govuk_check_box "status", "#{key}", + label: { text: option }, + checked: filter_selected?(key), + class: "govuk-checkboxes govuk-checkboxes--small" %> + <% end %> + <% end %> + <%= f.govuk_submit "Apply filters"%> <% end %> -
- - - <% case_logs.map do |log| %> - - - - - - - - <% if current_user.support? %> - - - <% end %> - - <% end %> - -
LogTenantPropertyTenancy startsLog createdCompletedOwning organisationManaging organisation
- <%= govuk_link_to log.id, case_log_path(log) %> - - <%= log.tenant_code? ? log.tenant_code : '–' %> - - <%= log.propcode? ? log.propcode : '–' %> - - <%= log.startdate.present? ? log.startdate.to_formatted_s(:govuk_date) : '–' %> - - <%= log.created_at.to_formatted_s(:govuk_date) %> - - <%= govuk_tag( - colour: log.status == 'completed' ? 'blue' : 'grey', - text: log.status.humanize - ) %> - - <%= log.owning_organisation.name %> - - <%= log.managing_organisation.name %> -
-
-
+ + + + +
+
+
+ + <%= pagy.count %> total <%= title.downcase %> + + Download (CSV) +
+
+ + + + + + + + + + <% if current_user.support? %> + + + <% end %> + + + + <% case_logs.map do |log| %> + + + + + + + + <% if current_user.support? %> + + + <% end %> + + <% end %> + +
LogTenantPropertyTenancy startsLog createdCompletedOwning organisationManaging organisation
+ <%= govuk_link_to log.id, case_log_path(log) %> + + <%= log.tenant_code? ? log.tenant_code : '–' %> + + <%= log.propcode? ? log.propcode : '–' %> + + <%= log.startdate.present? ? log.startdate.to_formatted_s(:govuk_date) : '–' %> + + <%= log.created_at.to_formatted_s(:govuk_date) %> + + <%= govuk_tag( + colour: log.status == 'completed' ? 'blue' : 'grey', + text: log.status.humanize + ) %> + + <%= log.owning_organisation.name %> + + <%= log.managing_organisation.name %> +
+
+
+
+ diff --git a/config/routes.rb b/config/routes.rb index 4cb364878..36169aabf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -90,6 +90,8 @@ Rails.application.routes.draw do end end + post "/logs/filter", to: "case_logs#filter" + scope via: :all do match "/404", to: "errors#not_found" match "/429", to: "errors#too_many_requests", status: 429 diff --git a/spec/helpers/filters_helper_spec.rb b/spec/helpers/filters_helper_spec.rb new file mode 100644 index 000000000..1ef323ab0 --- /dev/null +++ b/spec/helpers/filters_helper_spec.rb @@ -0,0 +1,26 @@ +require "rails_helper" + +RSpec.describe FiltersHelper do + describe "#filter_selected?" do + context "when no filters are selected" do + it "returns true for all filters" do + expect(filter_selected?("completed")).to be_truthy + expect(filter_selected?("in_progress")).to be_truthy + end + end + + context "one filter is selected" do + before do + cookies[:case_logs_filters] = { "status": "in_progress" }.to_json + end + + it "returns false for non selected filters" do + expect(filter_selected?("completed")).to be_falsey + end + + it "returns true for selected filter" do + expect(filter_selected?("in_progress")).to be_truthy + end + end + end +end diff --git a/spec/requests/case_logs_controller_spec.rb b/spec/requests/case_logs_controller_spec.rb index 0526ee7ed..5b751fdf4 100644 --- a/spec/requests/case_logs_controller_spec.rb +++ b/spec/requests/case_logs_controller_spec.rb @@ -172,6 +172,34 @@ RSpec.describe CaseLogsController, type: :request do expect(page).to have_content("Owning organisation") expect(page).to have_content("Managing organisation") end + + context "when filtering" do + let!(:in_progress_case_log) do + FactoryBot.create(:case_log, :in_progress, + owning_organisation: organisation, + managing_organisation: organisation) + end + let!(:completed_case_log) do + FactoryBot.create(:case_log, :completed, + owning_organisation: organisation, + managing_organisation: organisation) + end + + it "only shows case logs for selected status" do + in_progress_case_row_log = "#{in_progress_case_log.id}" + completed_case_row_log = "#{completed_case_log.id}" + + cookies[:case_logs_filters] = { status: %w[in_progress completed] }.to_json + get "/logs", headers: headers, params: {} + expect(CGI.unescape_html(response.body)).to include(in_progress_case_row_log) + expect(CGI.unescape_html(response.body)).to include(completed_case_row_log) + + cookies[:case_logs_filters] = { status: %w[in_progress] }.to_json + get "/logs", headers: headers, params: {} + expect(CGI.unescape_html(response.body)).to include(in_progress_case_row_log) + expect(CGI.unescape_html(response.body)).not_to include(completed_case_row_log) + end + end end context "when the user is not a customer support user" do @@ -626,4 +654,23 @@ RSpec.describe CaseLogsController, type: :request do end end end + + describe "POST #filter" do + let(:user) { FactoryBot.create(:user) } + let(:params) do + { + "status": ["In progress"], + } + end + + before do + sign_in user + end + + it "sets status filter in the cookies" do + expect(cookies[:case_logs_filters]).to be_nil + post "/logs/filter", headers: headers, params: params.to_json + expect(JSON.parse(cookies[:case_logs_filters])["status"]).to eq(["In progress"]) + end + end end