From 5e31ea808e93e1eeef7e87a70551cf70cc0ce12e Mon Sep 17 00:00:00 2001 From: Ted Date: Thu, 16 Jun 2022 15:19:48 +0100 Subject: [PATCH] Added repeated code to view component for table in orgs, schemes, users and logs --- app/components/view_result_component.html.erb | 7 +++++++ app/components/view_result_component.rb | 13 +++++++++++++ app/views/case_logs/_log_list.html.erb | 8 +------- app/views/organisations/_organisation_list.html.erb | 6 +----- app/views/schemes/_scheme_list.html.erb | 8 +------- app/views/users/_user_list.html.erb | 8 +------- 6 files changed, 24 insertions(+), 26 deletions(-) create mode 100644 app/components/view_result_component.html.erb create mode 100644 app/components/view_result_component.rb diff --git a/app/components/view_result_component.html.erb b/app/components/view_result_component.html.erb new file mode 100644 index 000000000..ed1c4cf29 --- /dev/null +++ b/app/components/view_result_component.html.erb @@ -0,0 +1,7 @@ + + <% if searched.present? %> + <%= count %> <%= item_label %> found matching ‘<%= searched %>’ of <%= total_count %> total <%= item %>. <%= govuk_link_to("Clear search", request.path) %> + <% else %> + <%= count %> total <%= item %>. + <% end %> + \ No newline at end of file diff --git a/app/components/view_result_component.rb b/app/components/view_result_component.rb new file mode 100644 index 000000000..cc415adde --- /dev/null +++ b/app/components/view_result_component.rb @@ -0,0 +1,13 @@ +class ViewResultComponent < ViewComponent::Base + attr_reader :searched, :count, :item_label, :total_count, :item, :request + + def initialize(searched:, count:, item_label:, total_count:, item:, request:) + @searched = searched + @count = count + @item_label = item_label + @total_count = total_count + @item = item + @request = request + super + end +end diff --git a/app/views/case_logs/_log_list.html.erb b/app/views/case_logs/_log_list.html.erb index 8041f5cc5..0e6bfda9e 100644 --- a/app/views/case_logs/_log_list.html.erb +++ b/app/views/case_logs/_log_list.html.erb @@ -1,13 +1,7 @@
<%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular], id: title.dasherize) do |caption| %> - - <% if defined?(searched) && searched.present? %> - <%= pagy.count %> <%= item_label %> found matching ‘<%= searched %>’ of <%= total_count %> total <%= title.downcase %>. <%= govuk_link_to("Clear search", request.path) %> - <% else %> - <%= pagy.count %> total <%= title.downcase %> - <% end %> - + <%= render(ViewResultComponent.new(searched: searched, count: pagy.count, item_label: item_label, total_count: total_count, item: "logs", request: request)) %> <%= govuk_link_to "Download (CSV)", "/logs.csv", type: "text/csv" %> <% end %> <%= table.head do |head| %> diff --git a/app/views/organisations/_organisation_list.html.erb b/app/views/organisations/_organisation_list.html.erb index 20e35b40c..fdc997b38 100644 --- a/app/views/organisations/_organisation_list.html.erb +++ b/app/views/organisations/_organisation_list.html.erb @@ -1,11 +1,7 @@
<%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> - <% if searched.present? %> - <%= pagy.count %> <%= item_label %> found matching ‘<%= searched %>’ of <%= total_count %> total organisations. <%= govuk_link_to("Clear search", request.path) %> - <% else %> - <%= pagy.count %> total organisations. - <% end %> + <%= render(ViewResultComponent.new(searched: searched, count: pagy.count, item_label: item_label, total_count: total_count, item: "organisations", request: request)) %> <% end %> <%= table.head do |head| %> <%= head.row do |row| %> diff --git a/app/views/schemes/_scheme_list.html.erb b/app/views/schemes/_scheme_list.html.erb index c610610e2..4103f122e 100644 --- a/app/views/schemes/_scheme_list.html.erb +++ b/app/views/schemes/_scheme_list.html.erb @@ -1,13 +1,7 @@
<%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> - - <% if searched.present? %> - <%= pagy.count %> <%= item_label %> found matching ‘<%= searched %>’ of <%= total_count %> total schemes. <%= govuk_link_to("Clear search", request.path) %> - <% else %> - <%= pagy.count %> total schemes. - <% end %> - + <%= render(ViewResultComponent.new(searched: searched, count: pagy.count, item_label: item_label, total_count: total_count, item: "schemes", request: request)) %> <% end %> <%= table.head do |head| %> <%= head.row do |row| %> diff --git a/app/views/users/_user_list.html.erb b/app/views/users/_user_list.html.erb index fb370c914..34d7b12ed 100644 --- a/app/views/users/_user_list.html.erb +++ b/app/views/users/_user_list.html.erb @@ -1,13 +1,7 @@
<%= govuk_table do |table| %> <%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %> - - <% if searched.present? %> - <%= pagy.count %> <%= item_label %> found matching ‘<%= searched %>’ of <%= total_count %> total users. <%= govuk_link_to("Clear search", request.path) %> - <% else %> - <%= pagy.count %> total users. - <% end %> - + <%= render(ViewResultComponent.new(searched: searched, count: pagy.count, item_label: item_label, total_count: total_count, item: "users", request: request)) %> <% if current_user.support? %> <% query = searched.present? ? "?search=#{searched}" : nil %> <%= govuk_link_to "Download (CSV)", "/users.csv#{query}", type: "text/csv" %>