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("
A description...
") end - it "editing a product comming from the bulk product update page with filter" do - visit spree.edit_admin_product_path(product, filter) - - click_button 'Update' - expect(flash_message).to eq('Product "a product" has been successfully updated!') - - # Check the url still includes the filters - uri = URI.parse(current_url) - expect("#{uri.path}?#{uri.query}").to eq spree.edit_admin_product_path(product, filter) - - # Link back to the bulk product update page should include the filters - expected_admin_product_url = - Regexp.new(Regexp.escape("#{spree.admin_products_path}#?#{filter.to_query}")) - expect(page).to have_link('Back to products list', - href: expected_admin_product_url) - expect(page).to have_link('Cancel', href: expected_admin_product_url) - - expected_product_url = Regexp.new(Regexp.escape(spree.edit_admin_product_path( - product.id, filter - ))) - expect(page).to have_link('Product Details', - href: expected_product_url) - - expected_product_image_url = Regexp.new(Regexp.escape(spree.admin_product_images_path( - product.id, filter - ))) - expect(page).to have_link('Images', - href: expected_product_image_url) - - expected_product_variant_url = Regexp.new(Regexp.escape(spree.admin_product_variants_path( - product.id, filter - ))) - expect(page).to have_link('Variants', - href: expected_product_variant_url) - - expected_product_properties_url = - Regexp.new(Regexp.escape(spree.admin_product_product_properties_path( - product.id, filter - ))) - expect(page).to have_link('Product Properties', - href: expected_product_properties_url) - - expected_product_group_buy_option_url = - Regexp.new(Regexp.escape(spree.group_buy_options_admin_product_path( - product.id, filter - ))) - expect(page).to have_link('Group Buy Options', - href: expected_product_group_buy_option_url) - - expected_product_seo_url = Regexp.new(Regexp.escape(spree.seo_admin_product_path( - product.id, filter - ))) - expect(page).to have_link('Search', href: expected_product_seo_url) - end - it "editing product group buy options" do visit spree.edit_admin_product_path product within('#sidebar') { click_link 'Group Buy Options' }