Skip to content

Commit

Permalink
Implement FiltersPresenter#reset_url
Browse files Browse the repository at this point in the history
This returns the URL used for the "Clear all" link on both the filter
summary and filter panel in the new "all content" finder UI.

- Add real implementation for `FiltersPresenter#reset_url`, merging the
  query parameters for every applied facet and then removing them from
  the current URL
- Refactor duplication into a `#applied_filters` private method
  • Loading branch information
csutter committed Oct 4, 2024
1 parent f5bedae commit 12f76c1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
22 changes: 19 additions & 3 deletions app/presenters/filters_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ def initialize(facets, finder_url_builder)
end

def any_filters?
facets.any?(&:has_filters?)
applied_filters.any?
end

def summary_items
facets.flat_map(&:applied_filters).map do |filter|
applied_filters.map do |filter|
{
label: filter[:name],
value: filter[:label],
Expand All @@ -20,10 +20,26 @@ def summary_items
end

def reset_url
"#"
return nil unless any_filters?

all_query_params = applied_filters.map { _1[:query_params] }
merged_facet_params = all_query_params.reduce do |acc, query_params|
acc.deep_merge(query_params) do |_key, old_val, new_val|
if old_val.is_a?(Array)
old_val + new_val
else
new_val
end
end
end
finder_url_builder.url_except(merged_facet_params)
end

private

attr_reader :facets, :finder_url_builder

def applied_filters
@applied_filters ||= facets.flat_map(&:applied_filters)
end
end
54 changes: 49 additions & 5 deletions spec/presenters/filters_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,27 @@
let(:finder_url_builder) { instance_double(UrlBuilder) }

let(:facet_without_applied_filters) { double("Facet", has_filters?: false, applied_filters: []) }
let(:facet_with_applied_filters) { double("Facet", has_filters?: true, applied_filters:) }

let(:applied_filters) { [{ name: "name", label: "label", query_params: { key: %w[value] } }] }
let(:facet_with_applied_filters) do
double(
"Facet",
has_filters?: true,
applied_filters: [{ name: "name", label: "label", query_params: { key: %w[value] } }],
)
end
let(:another_facet_with_applied_filters) do
double(
"Facet",
has_filters?: true,
applied_filters: [
{ name: "name1", label: "label1", query_params: { key1: %w[value1] } },
{ name: "name2",
label: "label2",
query_params: {
key1: %w[anothervalue1], key2: { value2: "subvalue" }
} },
],
)
end

describe "#any_filters" do
context "when there are no facets" do
Expand All @@ -32,8 +50,34 @@
end

describe "#reset_url" do
it "returns a static anchor link" do
expect(subject.reset_url).to eq("#")
subject(:reset_url) { filters_presenter.reset_url }

context "when there are no facets" do
let(:facets) { [] }

it { is_expected.to be_nil }
end

context "when there are only facets without applied filters" do
let(:facets) { [facet_without_applied_filters, facet_without_applied_filters] }

it { is_expected.to be_nil }
end

context "when there are facets with applied filters" do
let(:facets) { [facet_with_applied_filters, another_facet_with_applied_filters] }

before do
allow(finder_url_builder).to receive(:url_except).with({
key: %w[value],
key1: %w[value1 anothervalue1],
key2: { value2: "subvalue" },
}).and_return("/search/foo")
end

it "returns the expected reset URL" do
expect(reset_url).to eq("/search/foo")
end
end
end

Expand Down

0 comments on commit 12f76c1

Please sign in to comment.