From 87e23ab9e473ae7cfe67a335e23428b3220f33fb Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Wed, 27 Apr 2022 13:59:38 +0100 Subject: [PATCH] UI init --- app/controllers/case_logs_controller.rb | 2 +- app/helpers/filters_helper.rb | 6 ++++++ app/views/case_logs/_log_filters.erb | 10 +++++++++- app/views/filters/_radio_filter.html.erb | 19 ++++++++++++++----- app/views/filters/_select_filter.html.erb | 7 +++++++ spec/requests/case_logs_controller_spec.rb | 9 ++++++++- 6 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 app/views/filters/_select_filter.html.erb diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index ef53784a1..24ecb1e06 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -126,7 +126,7 @@ private if session[:case_logs_filters].present? filters = JSON.parse(session[:case_logs_filters]) filters.each do |category, values| - next if values.reject(&:empty?).blank? + next if Array(values).reject(&:empty?).blank? query = query.public_send("filter_by_#{category}", values, current_user) end diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index c142b6e6d..1d6e773e6 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -14,4 +14,10 @@ module FiltersHelper CaseLog.statuses.keys.map { |status| statuses[status] = status.humanize } statuses end + + def selected_option(filter) + return false unless session[:case_logs_filters] + + JSON.parse(session[:case_logs_filters])[filter] + end end diff --git a/app/views/case_logs/_log_filters.erb b/app/views/case_logs/_log_filters.erb index 5f69e97a7..ded8bc9dc 100644 --- a/app/views/case_logs/_log_filters.erb +++ b/app/views/case_logs/_log_filters.erb @@ -11,7 +11,15 @@ <%= render partial: "filters/checkbox_filter", locals: { f: f, options: status_filters, label: "Status", category: "status" } %> <%= render partial: "filters/radio_filter", locals: { f: f, options: all_or_yours, label: "Logs", category: "user", } %> <% if @current_user.support? %> - <%= render partial: "filters/checkbox_filter", locals: { f: f, options: {"all": "All", "yours": "Yours"}, label: "Organisation", category: "organisation" } %> + <%= render partial: "filters/radio_filter", locals: { + f: f, + options: { + "all": { label: "All" }, + "specific_org": { label: "Specific organisation", conditional_filter: { type: "select", label: "Organisation", category: "organisation" } } + }, + label: "Organisation", + category: "organisation" + } %> <% end %> <%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %> <% end %> diff --git a/app/views/filters/_radio_filter.html.erb b/app/views/filters/_radio_filter.html.erb index aff0f6ed6..e6f03e796 100644 --- a/app/views/filters/_radio_filter.html.erb +++ b/app/views/filters/_radio_filter.html.erb @@ -1,8 +1,17 @@ <%= f.govuk_radio_buttons_fieldset category.to_sym, legend: { text: label, size: "s" }, small: true, form_group: { classes: "app-filter__group" } do %> - <% options.map do |key, option| %> - <%= f.govuk_radio_button category, key.to_s, - label: { text: option }, - checked: filter_selected?(category, key), - size: "s" %> + <% options.map do |key, option| %> + <%= f.govuk_radio_button category, key.to_s, + label: { text: option }, + checked: filter_selected?(category, key), + size: "s" + %> + <% if option[:conditional_filter] %> + <%= render partial: "filters/#{option[:conditional_filter][:type]}_filter", locals: { + f: f, + collection: Organisation.all, + category: "organisation", + secondary: true + } %> <% end %> + <% end %> <% end %> diff --git a/app/views/filters/_select_filter.html.erb b/app/views/filters/_select_filter.html.erb new file mode 100644 index 000000000..e2c71988e --- /dev/null +++ b/app/views/filters/_select_filter.html.erb @@ -0,0 +1,7 @@ +<%= f.govuk_collection_select category.to_sym, + collection, + :id, + :name, + label: { hidden: secondary }, + options: { disabled: [""], selected: selected_option(category) }, + "data-controller": "accessible-autocomplete" %> diff --git a/spec/requests/case_logs_controller_spec.rb b/spec/requests/case_logs_controller_spec.rb index 9a9dd60e1..a5ebfba73 100644 --- a/spec/requests/case_logs_controller_spec.rb +++ b/spec/requests/case_logs_controller_spec.rb @@ -186,6 +186,7 @@ RSpec.describe CaseLogsController, type: :request do context "when filtering" do context "with status filter" do + let(:organisation_2) { FactoryBot.create(:organisation) } let!(:in_progress_case_log) do FactoryBot.create(:case_log, :in_progress, owning_organisation: organisation, @@ -193,7 +194,7 @@ RSpec.describe CaseLogsController, type: :request do end let!(:completed_case_log) do FactoryBot.create(:case_log, :completed, - owning_organisation: organisation, + owning_organisation: organisation_2, managing_organisation: organisation) end @@ -209,6 +210,12 @@ RSpec.describe CaseLogsController, type: :request do expect(page).not_to have_link(completed_case_log.id.to_s) end + it "filters on organisation" do + get "/logs?organisation[]=#{organisation_2.id}", headers: headers, params: {} + expect(page).to have_link(completed_case_log.id.to_s) + expect(page).not_to have_link(in_progress_case_log.id.to_s) + end + it "does not reset the filters" do get "/logs?status[]=in_progress", headers: headers, params: {} expect(page).to have_link(in_progress_case_log.id.to_s)