diff --git a/app/assets/stylesheets/admin/poll/questions/filter.scss b/app/assets/stylesheets/admin/poll/questions/filter.scss new file mode 100644 index 000000000..78cb4fb4f --- /dev/null +++ b/app/assets/stylesheets/admin/poll/questions/filter.scss @@ -0,0 +1,16 @@ +.admin .poll-questions-filter { + $gap: 0.5em; + align-items: flex-end; + display: flex; + flex-wrap: wrap; + margin-left: -$gap; + + > * { + margin-left: $gap; + } + + [type="submit"] { + @include regular-button; + margin-left: $gap; + } +} diff --git a/app/components/admin/poll/questions/filter_component.html.erb b/app/components/admin/poll/questions/filter_component.html.erb new file mode 100644 index 000000000..ee1f443ff --- /dev/null +++ b/app/components/admin/poll/questions/filter_component.html.erb @@ -0,0 +1,7 @@ +<%= form_tag "", method: :get, class: "poll-questions-filter" do %> +
+ <%= label_tag :poll_id, t("admin.questions.index.filter_poll") %> + <%= select_tag "poll_id", poll_select_options, prompt: t("polls.all") %> +
+ <%= submit_tag t("shared.filter") %> +<% end %> diff --git a/app/components/admin/poll/questions/filter_component.rb b/app/components/admin/poll/questions/filter_component.rb new file mode 100644 index 000000000..b8b7d7412 --- /dev/null +++ b/app/components/admin/poll/questions/filter_component.rb @@ -0,0 +1,14 @@ +class Admin::Poll::Questions::FilterComponent < ApplicationComponent + attr_reader :polls + delegate :current_path_with_query_params, to: :helpers + + def initialize(polls) + @polls = polls + end + + private + + def poll_select_options + options_from_collection_for_select(polls, :id, :name, params[:poll_id]) + end +end diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index b69a9368f..08a2d3ab2 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -1,16 +1,4 @@ module PollsHelper - def poll_select_options(include_all = nil) - options = @polls.map do |poll| - [poll.name, current_path_with_query_params(poll_id: poll.id)] - end - options << all_polls if include_all - options_for_select(options, request.fullpath) - end - - def all_polls - [I18n.t("polls.all"), admin_questions_path] - end - def poll_dates(poll) if poll.starts_at.blank? || poll.ends_at.blank? I18n.t("polls.no_dates") diff --git a/app/views/admin/poll/questions/_filter.html.erb b/app/views/admin/poll/questions/_filter.html.erb deleted file mode 100644 index b3fca61c5..000000000 --- a/app/views/admin/poll/questions/_filter.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -<%= form_tag "", method: :get do %> - <%= label_tag :poll_id, t("admin.questions.index.filter_poll") %> - <%= select_tag "poll_id", - poll_select_options(true), - prompt: t("admin.questions.index.select_poll"), - class: "js-location-changer" %> -<% end %> diff --git a/app/views/admin/poll/questions/_questions.html.erb b/app/views/admin/poll/questions/_questions.html.erb index d83428879..e8ce0402e 100644 --- a/app/views/admin/poll/questions/_questions.html.erb +++ b/app/views/admin/poll/questions/_questions.html.erb @@ -1,6 +1,4 @@ -
- <%= render "filter" %> -
+<%= render Admin::Poll::Questions::FilterComponent.new(@polls) %> <% if @questions.count == 0 %>
diff --git a/spec/components/admin/poll/questions/filter_component_spec.rb b/spec/components/admin/poll/questions/filter_component_spec.rb new file mode 100644 index 000000000..8861ad354 --- /dev/null +++ b/spec/components/admin/poll/questions/filter_component_spec.rb @@ -0,0 +1,9 @@ +require "rails_helper" + +describe Admin::Poll::Questions::FilterComponent, type: :component do + it "renders a button to submit the form" do + render_inline Admin::Poll::Questions::FilterComponent.new([]) + + expect(page).to have_button "Filter" + end +end