diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 327a4a4be9..24ed032dcf 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -11,6 +11,8 @@ module Admin def index fetch_products render "index", locals: { producers:, categories:, tax_category_options:, flash: } + + session[:products_return_to_url] = request.url end def bulk_update diff --git a/app/controllers/spree/admin/products_controller.rb b/app/controllers/spree/admin/products_controller.rb index 3ae92a38bd..4580a751f0 100644 --- a/app/controllers/spree/admin/products_controller.rb +++ b/app/controllers/spree/admin/products_controller.rb @@ -10,6 +10,7 @@ module Spree include OpenFoodNetwork::SpreeApiKeyLoader include OrderCyclesHelper include EnterprisesHelper + helper ::Admin::ProductsHelper before_action :load_data before_action :load_producers, only: [:index, :new] diff --git a/app/helpers/admin/products_helper.rb b/app/helpers/admin/products_helper.rb index 1edd8d0a28..4686e4720a 100644 --- a/app/helpers/admin/products_helper.rb +++ b/app/helpers/admin/products_helper.rb @@ -29,5 +29,13 @@ module Admin [precised_unit_value, variant.unit_description].compact_blank.join(" ") end + + def products_return_to_url(url_filters) + if feature?(:admin_style_v3, spree_current_user) + return session[:products_return_to_url] || admin_products_url + end + + "#{admin_products_path}#{url_filters.empty? ? '' : "#?#{url_filters.to_query}"}" + end end end diff --git a/app/views/spree/admin/products/edit.html.haml b/app/views/spree/admin/products/edit.html.haml index 6a5ccb4c21..8591ffe211 100644 --- a/app/views/spree/admin/products/edit.html.haml +++ b/app/views/spree/admin/products/edit.html.haml @@ -1,7 +1,7 @@ = admin_inject_available_units - content_for :page_actions do - %li= button_link_to t('admin.products.back_to_products_list'), "#{admin_products_path}#{(@url_filters.empty? ? "" : "#?#{@url_filters.to_query}")}", :icon => 'icon-arrow-left' + %li= button_link_to t('admin.products.back_to_products_list'), products_return_to_url(@url_filters), :icon => 'icon-arrow-left' %li#new_product_link = button_link_to t(:new_product), new_object_url, { :icon => 'icon-plus', :id => 'admin_new_product' } @@ -16,4 +16,4 @@ .form-buttons.filter-actions.actions = button t(:update), 'icon-refresh' - = button_link_to t(:cancel), "#{collection_url}#{(@url_filters.empty? ? "" : "#?#{@url_filters.to_query}")}", icon: 'icon-remove' + = button_link_to t(:cancel), products_return_to_url(@url_filters), icon: 'icon-remove' diff --git a/spec/system/admin/products_spec.rb b/spec/system/admin/products_spec.rb index 9fad202f79..2c87085043 100644 --- a/spec/system/admin/products_spec.rb +++ b/spec/system/admin/products_spec.rb @@ -309,6 +309,43 @@ RSpec.describe ' describe "editing page" do let!(:product) { create(:simple_product, name: 'a product', supplier_id: supplier2.id) } + describe "'Back to products list' and 'Cancel' buttons" do + context "navigates to edit from the bulk product update page with searched results" do + it "should navigate back to the same searched results page" do + # Navigating to a searched URL + visit admin_products_url({ + page: 1, + per_page: 25, + search_term: 'product', + producer_id: supplier2.id + }) + + products_page_url = current_url + within row_containing_name('a product') do + page.find(".vertical-ellipsis-menu").click + click_link('Edit', href: spree.edit_admin_product_path(product)) + end + + expect(page).to have_link('Back to products list', + href: products_page_url) + expect(page).to have_link('Cancel', + href: products_page_url) + end + end + + context "directly navigates to the edit page" do + it "should navigate back to all the products page" do + # Navigating to a searched URL + visit spree.edit_admin_product_path(product) + + expect(page).to have_link('Back to products list', + href: admin_products_url) + expect(page).to have_link('Cancel', + href: admin_products_url) + end + end + end + it "editing a product" do visit spree.edit_admin_product_path product @@ -319,61 +356,6 @@ RSpec.describe ' expect(product.description).to eq("