Can filter by categories

This commit is contained in:
Jean-Baptiste Bellet
2023-07-03 13:37:35 +02:00
parent ef1702188f
commit bfe1884ab5
5 changed files with 37 additions and 2 deletions

View File

@@ -25,6 +25,7 @@ class ProductsReflex < ApplicationReflex
@page = 1
@search_term = params[:search_term]
@producer_id = params[:producer_id]
@category_id = params[:category_id]
fetch_products
render_products
@@ -37,7 +38,8 @@ class ProductsReflex < ApplicationReflex
selector: "#products-content",
html: render(partial: "admin/products_v3/content",
locals: { products: @products, pagy: @pagy, search_term: @search_term,
producer_options: producers, producer_id: @producer_id })
producer_options: producers, producer_id: @producer_id,
category_options: categories, category_id: @category_id })
).broadcast
cable_ready.replace_state(
@@ -56,6 +58,10 @@ class ProductsReflex < ApplicationReflex
producers.map { |p| [p.name, p.id] }
end
def categories
Spree::Taxon.order(:name).map { |c| [c.name, c.id] }
end
# copied from ProductsTableComponent
def fetch_products
product_query = OpenFoodNetwork::Permissions.new(current_user)
@@ -77,6 +83,7 @@ class ProductsReflex < ApplicationReflex
query = { s: "name desc" }
query = query.merge({ supplier_id_in: @producer_id }) if @producer_id.present?
query = query.merge({ name_cont: @search_term }) if @search_term.present?
query = query.merge({ primary_taxon_id_in: @category_id }) if @category_id.present?
query
end

View File

@@ -3,7 +3,9 @@
.sixteen.columns
= render partial: 'filters', locals: { search_term: search_term,
producer_id: producer_id,
producer_options: producer_options }
producer_options: producer_options,
category_options: category_options,
category_id: category_id }
- if products.any?
.container
.sixteen.columns

View File

@@ -4,6 +4,8 @@
= text_field_tag :search_term, search_term, placeholder: t('.search_products')
.producers
= select_tag :producer_id, options_for_select(producer_options, producer_id), include_blank: t('.all_producers')
.categories
= select_tag :category_id, options_for_select(category_options, category_id), include_blank: t('.all_categories')
.submit
.search-button
= submit_tag t(".search"), class: "secondary"

View File

@@ -776,6 +776,7 @@ en:
filters:
search_products: Search for products
all_producers: All producers
all_categories: All categories
search: Search
content:
no_products_found: No products found

View File

@@ -16,6 +16,10 @@ describe 'As an admin, I can see the new product page' do
create(:simple_product,
supplier: create(:enterprise, name: "Producer 1"))
}
# create a product with a category that can be searched
let!(:product_by_category) {
create(:simple_product, taxons: [create(:taxon, name: "Category 1")])
}
before do
# activate feature toggle admin_style_v3 to use new admin interface
@@ -99,6 +103,20 @@ describe 'As an admin, I can see the new product page' do
expect_products_count_to_be 1
end
end
context "search by category" do
it "has a category select" do
expect(page).to have_selector "select#category_id"
end
it "can search for a product" do
search_by_category "Category 1"
expect(page).to have_select "category_id", selected: "Category 1"
expect_page_to_be 1
expect_products_count_to_be 1
end
end
end
def expect_page_to_be(page)
@@ -123,4 +141,9 @@ describe 'As an admin, I can see the new product page' do
select producer, from: "producer_id"
click_button "Search"
end
def search_by_category(category)
select category, from: "category_id"
click_button "Search"
end
end