From 72ce3a01a9619878e2e7bec60be85e0d5d63a48a Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 11 Apr 2024 10:00:40 +1000 Subject: [PATCH] Ensure search terms and filters are retained when saving --- .../admin/products_v3_controller.rb | 4 ++- app/views/admin/products_v3/_table.html.haml | 5 +++- .../system/admin/products_v3/products_spec.rb | 25 +++++++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index fb5efe5d1a..819031652a 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -18,7 +18,9 @@ module Admin if product_set.save flash[:success] = I18n.t('admin.products_v3.bulk_update.success') - redirect_to [:index, { page: @page, per_page: @per_page }] + redirect_to [:index, + { page: @page, per_page: @per_page, search_term: @search_term, + producer_id: @producer_id, category_id: @category_id }] elsif product_set.errors.present? @error_counts = { saved: product_set.saved_count, invalid: product_set.invalid.count } diff --git a/app/views/admin/products_v3/_table.html.haml b/app/views/admin/products_v3/_table.html.haml index 4a34b81258..1cf0f78e2d 100644 --- a/app/views/admin/products_v3/_table.html.haml +++ b/app/views/admin/products_v3/_table.html.haml @@ -7,6 +7,9 @@ = hidden_field_tag :page, @page = hidden_field_tag :per_page, @per_page + = hidden_field_tag :search_term, @search_term + = hidden_field_tag :producer_id, @producer_id + = hidden_field_tag :category_id, @category_id %table.products %colgroup @@ -39,7 +42,7 @@ -# Y products could not be saved correctly. Please review errors and try again = t('.error_summary.invalid', count: @error_counts[:invalid]) .form-buttons - %a.button.reset.medium{ href: admin_products_path(page: @page, per_page: @per_page) } + %a.button.reset.medium{ href: admin_products_path(page: @page, per_page: @per_page, search_term: @search_term, producer_id: @producer_id, category_id: @category_id) } = t('.reset') = form.submit t('.save'), class: "medium" %tr diff --git a/spec/system/admin/products_v3/products_spec.rb b/spec/system/admin/products_v3/products_spec.rb index 74dcc2d051..2afde20c1f 100644 --- a/spec/system/admin/products_v3/products_spec.rb +++ b/spec/system/admin/products_v3/products_spec.rb @@ -130,9 +130,9 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do # create a product with a different supplier let!(:producer) { create(:supplier_enterprise, name: "Producer 1") } - let!(:product_by_supplier) { create(:simple_product, supplier: producer) } + let!(:product_by_supplier) { create(:simple_product, name: "Apples", supplier: producer) } - it "can search for a product" do + it "can search for and update a product" do visit admin_products_url search_by_producer "Producer 1" @@ -140,6 +140,22 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do # expect(page).to have_content "1 product found for your search criteria." expect(page).to have_select "producer_id", selected: "Producer 1" expect_products_count_to_be 1 + + within row_containing_name("Apples") do + fill_in "Name", with: "Pommes" + end + + expect { + click_button "Save changes" + + expect(page).to have_content "Changes saved" + product_by_supplier.reload + }.to change { product_by_supplier.name }.to("Pommes") + + # Search is still applied + # expect(page).to have_content "1 product found for your search criteria." + expect(page).to have_select "producer_id", selected: "Producer 1" + expect_products_count_to_be 1 end end @@ -976,13 +992,12 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do end def search_by_producer(producer) - # TODO: use a helper to more reliably select the tom-select component - select producer, from: "producer_id" + tomselect_select producer, from: "producer_id" click_button "Search" end def search_by_category(category) - select category, from: "category_id" + tomselect_select category, from: "category_id" click_button "Search" end