Can filter by producer

Not sur the request nor the `producers` in the reflex should be like this. This is a proof of concept, and should probably be reviewed

Fix linter issues
This commit is contained in:
Jean-Baptiste Bellet
2023-07-03 12:31:36 +02:00
parent ddfc60c85e
commit f58cf2d3b2
6 changed files with 43 additions and 4 deletions

View File

@@ -24,6 +24,7 @@ class ProductsReflex < ApplicationReflex
@per_page = params[:per_page]
@page = 1
@search_term = params[:search_term]
@producer_id = params[:producer_id]
fetch_products
render_products
@@ -35,7 +36,8 @@ class ProductsReflex < ApplicationReflex
cable_ready.replace(
selector: "#products-content",
html: render(partial: "admin/products_v3/content",
locals: { products: @products, pagy: @pagy, search_term: @search_term })
locals: { products: @products, pagy: @pagy, search_term: @search_term,
producer_options: producers, producer_id: @producer_id })
).broadcast
cable_ready.replace_state(
@@ -45,6 +47,15 @@ class ProductsReflex < ApplicationReflex
morph :nothing
end
def producers
producers = if current_user.has_spree_role?("admin")
Enterprise.all
else
current_user.enterprises
end
producers.map { |p| [p.name, p.id] }
end
# copied from ProductsTableComponent
def fetch_products
product_query = OpenFoodNetwork::Permissions.new(current_user)
@@ -64,7 +75,9 @@ class ProductsReflex < ApplicationReflex
def ransack_query
query = { s: "name desc" }
query.merge({ name_cont: @search_term }) if @search_term.present?
query = query.merge({ supplier_id_in: @producer_id }) if @producer_id.present?
query = query.merge({ name_cont: @search_term }) if @search_term.present?
query
end
# Optimise by pre-loading required columns

View File

@@ -2,7 +2,7 @@
- if products.any?
.container
.sixteen.columns
= render partial: 'filters', locals: { search_term: search_term }
= render partial: 'filters', locals: { search_term: search_term, producer_id: producer_id, producer_options: producer_options }
.container
.sixteen.columns
= render partial: 'sort', locals: { pagy: pagy }

View File

@@ -2,6 +2,8 @@
.query
.search-input
= 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')
.submit
.search-button
= submit_tag t(".search"), class: "secondary"

View File

@@ -99,6 +99,7 @@
}
#filters {
gap: 20px;
.query {
flex-grow: 2;
.search-input {

View File

@@ -775,6 +775,7 @@ en:
per_page: "%{num} per page"
filters:
search_products: Search for products
all_producers: All producers
search: Search
content:
no_products_found: No products found

View File

@@ -11,6 +11,12 @@ describe 'As an admin, I can see the new product page' do
let!(:products) { create_list(:simple_product, 70) }
# create a product with a name that can be searched
let!(:product_by_name) { create(:simple_product, name: "searchable product") }
# create a product with a supplier that can be searched
let!(:product_by_supplier) {
create(:simple_product,
supplier: create(:enterprise, name: "Producer 1"))
}
before do
# activate feature toggle admin_style_v3 to use new admin interface
Flipper.enable(:admin_style_v3)
@@ -80,6 +86,17 @@ describe 'As an admin, I can see the new product page' do
end
end
context "search by producer" do
it "has a producer select" do
expect(page).to have_selector "select#producer_id"
end
it "can search for a product" do
search_by_producer "Producer 1"
expect(page).to have_select "producer_id", selected: "Producer 1"
expect_page_to_be 1
expect_products_count_to_be 1
end
end
end
@@ -94,11 +111,16 @@ describe 'As an admin, I can see the new product page' do
end
def expect_products_count_to_be(count)
expect(page).to have_selector "table.products tbody", count: count
expect(page).to have_selector("table.products tbody", count:)
end
def search_for(term)
fill_in "search_term", with: term
click_button "Search"
end
def search_by_producer(producer)
select producer, from: "producer_id"
click_button "Search"
end
end