From 0fc3d39106e61528feaca47d2bd4e87180991e18 Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 3 Apr 2024 16:56:29 +1100 Subject: [PATCH] Ensure pagination is retained when saving or discarding But we have more work to do. --- .../admin/products_v3_controller.rb | 8 ++--- app/views/admin/products_v3/_table.html.haml | 5 ++- .../system/admin/products_v3/products_spec.rb | 31 +++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 7a00d77144..1750083cea 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -26,8 +26,8 @@ module Admin end end - def index_url - "/admin/products" # todo: fix routing so this can be automatically generated + def index_url(params) + "/admin/products?#{params.to_query}" # todo: fix routing so this can be automaticly generated end private @@ -43,8 +43,8 @@ module Admin def init_pagination_params # prority is given to element dataset (if present) over url params - @page = params[:_page] || 1 - @per_page = params[:_per_page] || 15 + @page = params[:_page].presence || 1 + @per_page = params[:_per_page].presence || 15 end def producers diff --git a/app/views/admin/products_v3/_table.html.haml b/app/views/admin/products_v3/_table.html.haml index 6f9850e778..bf7154de84 100644 --- a/app/views/admin/products_v3/_table.html.haml +++ b/app/views/admin/products_v3/_table.html.haml @@ -4,6 +4,10 @@ 'bulk-form-error-value': defined?(@error_counts), } } do |form| = render(partial: "admin/shared/flashes", locals: { flashes: }) if defined? flashes + + = hidden_field_tag :_page, @page + = hidden_field_tag :_per_page, @per_page + %table.products %colgroup %col{ width:"56" }= # Img (size + padding) @@ -35,7 +39,6 @@ -# 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) } = t('.reset') = form.submit t('.save'), class: "medium" diff --git a/spec/system/admin/products_v3/products_spec.rb b/spec/system/admin/products_v3/products_spec.rb index 0f23adf05c..9622ce4a9d 100644 --- a/spec/system/admin/products_v3/products_spec.rb +++ b/spec/system/admin/products_v3/products_spec.rb @@ -608,6 +608,37 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do expect(page).to have_content "Please review the errors and try again" end end + + context "pagination" do + let!(:product_a) { create(:simple_product, name: "zucchini") } # appears on p2 + + it "retains selected page after saving" do + create_products 15 # in addition to product_a + visit admin_products_url + + within ".pagination" do + click_link "2" + end + + within row_containing_name("zucchini") do + fill_in "Name", with: "zucchinis" + end + + expect { + click_button "Save changes" + + expect(page).to have_content "Changes saved" + product_a.reload + }.to change { product_a.name }.to("zucchinis") + + pending "awaiting pagination to be loaded without SR" + expect(page).to have_content "Showing 16 to 16" # todo: remove unnecessary duplication + expect_page_to_be 2 + expect_per_page_to_be 15 + expect_products_count_to_be 1 + expect(page).to have_css row_containing_name("zucchinis") + end + end end describe "edit image" do