diff --git a/app/reflexes/products_reflex.rb b/app/reflexes/products_reflex.rb index 0ab4035927..3a3047513d 100644 --- a/app/reflexes/products_reflex.rb +++ b/app/reflexes/products_reflex.rb @@ -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 diff --git a/app/views/admin/products_v3/_content.html.haml b/app/views/admin/products_v3/_content.html.haml index 24f0150424..e9fcd26975 100644 --- a/app/views/admin/products_v3/_content.html.haml +++ b/app/views/admin/products_v3/_content.html.haml @@ -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 diff --git a/app/views/admin/products_v3/_filters.html.haml b/app/views/admin/products_v3/_filters.html.haml index 26a1d9e3c6..fca9e951e6 100644 --- a/app/views/admin/products_v3/_filters.html.haml +++ b/app/views/admin/products_v3/_filters.html.haml @@ -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" diff --git a/config/locales/en.yml b/config/locales/en.yml index bca84fb4b2..d9b9113a82 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 diff --git a/spec/system/admin/products_v3/products_spec.rb b/spec/system/admin/products_v3/products_spec.rb index fe809fe6ed..a0f6242bfb 100644 --- a/spec/system/admin/products_v3/products_spec.rb +++ b/spec/system/admin/products_v3/products_spec.rb @@ -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