From 86f88792c21b474aa313287e2caa9c1b4d50e122 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Wed, 8 May 2024 11:52:02 +0500 Subject: [PATCH 1/3] 11058: add sortable products by name --- app/controllers/admin/products_v3_controller.rb | 3 +++ app/views/admin/products_v3/_content.html.haml | 2 +- app/views/admin/products_v3/_filters.html.haml | 1 + app/views/admin/products_v3/_table.html.haml | 3 ++- .../spree/admin/shared/_stimulus_sortable_header.html.haml | 2 +- 5 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 92386e1231..f8cdc339c2 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -48,6 +48,7 @@ module Admin # prority is given to element dataset (if present) over url params @page = params[:page].presence || 1 @per_page = params[:per_page].presence || 15 + @q = params.permit(q: {})[:q] || { s: 'name asc' } end def producers @@ -89,6 +90,8 @@ module Admin query.merge!(Spree::Variant::SEARCH_KEY => @search_term) end query.merge!(variants_primary_taxon_id_in: @category_id) if @category_id.present? + query.merge!(@q) if @q + query end diff --git a/app/views/admin/products_v3/_content.html.haml b/app/views/admin/products_v3/_content.html.haml index 0a9459abae..e605fdd8b3 100644 --- a/app/views/admin/products_v3/_content.html.haml +++ b/app/views/admin/products_v3/_content.html.haml @@ -1,5 +1,5 @@ %turbo-frame#products-content{ target: "_top", refresh: "morph" } - .spinner-overlay{ "data-controller": "loading", "data-products-target": "loading", class: "hidden" } + .spinner-overlay.hidden .spinner-container .spinner = t('.loading') diff --git a/app/views/admin/products_v3/_filters.html.haml b/app/views/admin/products_v3/_filters.html.haml index 2a0aa55428..4b90628c7c 100644 --- a/app/views/admin/products_v3/_filters.html.haml +++ b/app/views/admin/products_v3/_filters.html.haml @@ -1,6 +1,7 @@ = form_with url: admin_products_path, id: "filters", method: :get, data: { "search-target": "form", 'turbo-frame': "_self" } do = hidden_field_tag :page, nil, class: "page" = hidden_field_tag :per_page, nil, class: "per-page" + = hidden_field_tag '[q][s]', params.dig(:q, :s) || 'name asc', class: 'sort', 'data-default': 'name asc' .query .search-input diff --git a/app/views/admin/products_v3/_table.html.haml b/app/views/admin/products_v3/_table.html.haml index 10b62f911e..23f6fb5fb4 100644 --- a/app/views/admin/products_v3/_table.html.haml +++ b/app/views/admin/products_v3/_table.html.haml @@ -49,7 +49,8 @@ = form.submit t('.save'), class: "medium" %tr %th.align-left= # image - %th.align-left.with-input= t('admin.products_page.columns.name') + = render partial: 'spree/admin/shared/stimulus_sortable_header', + locals: { column: :name, sorted: params.dig(:q, :s), default: 'name asc' } %th.align-left.with-input= t('admin.products_page.columns.sku') %th.align-left.with-input= t('admin.products_page.columns.unit_scale') %th.align-left.with-input= t('admin.products_page.columns.unit') diff --git a/app/views/spree/admin/shared/_stimulus_sortable_header.html.haml b/app/views/spree/admin/shared/_stimulus_sortable_header.html.haml index 46443016ce..08db1b6ce9 100644 --- a/app/views/spree/admin/shared/_stimulus_sortable_header.html.haml +++ b/app/views/spree/admin/shared/_stimulus_sortable_header.html.haml @@ -1,5 +1,5 @@ %th - %a{ "data-action": "click->search#changeSorting", "data-column": "#{column}", "data-current": sorted.to_s } + %a{ "data-controller": "search", "data-action": "click->search#changeSorting", "data-column": "#{column}", "data-current": (sorted || default).to_s } = t("spree.admin.shared.sortable_header.#{column.to_s}") - if sorted == "#{column} asc" || sorted.blank? && local_assigns[:default] == "#{column} asc" From b5fe19cb7d897f31844c0e08eac05a1b82916927 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Wed, 8 May 2024 13:47:47 +0500 Subject: [PATCH 2/3] 11058: add specs --- .../admin/products_v3/_content.html.haml | 2 +- .../system/admin/products_v3/products_spec.rb | 27 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/views/admin/products_v3/_content.html.haml b/app/views/admin/products_v3/_content.html.haml index e605fdd8b3..0a9459abae 100644 --- a/app/views/admin/products_v3/_content.html.haml +++ b/app/views/admin/products_v3/_content.html.haml @@ -1,5 +1,5 @@ %turbo-frame#products-content{ target: "_top", refresh: "morph" } - .spinner-overlay.hidden + .spinner-overlay{ "data-controller": "loading", "data-products-target": "loading", class: "hidden" } .spinner-container .spinner = t('.loading') diff --git a/spec/system/admin/products_v3/products_spec.rb b/spec/system/admin/products_v3/products_spec.rb index 0eff69cde8..5f332407bb 100644 --- a/spec/system/admin/products_v3/products_spec.rb +++ b/spec/system/admin/products_v3/products_spec.rb @@ -26,13 +26,14 @@ describe 'As an enterprise user, I can manage my products', feature: :admin_styl describe "sorting" do let!(:product_b) { create(:simple_product, name: "Bananas") } let!(:product_a) { create(:simple_product, name: "Apples") } + let(:products_table) { "table.products" } before do visit admin_products_url end - it "Should sort products alphabetically by default" do - within "table.products" do + it "Should sort products alphabetically by default in ascending order" do + within products_table do # Gather input values, because page.content doesn't include them. input_content = page.find_all('input[type=text]').map(&:value).join @@ -40,6 +41,28 @@ describe 'As an enterprise user, I can manage my products', feature: :admin_styl expect(input_content).to match /Apples.*Bananas/ end end + + context "when clicked on 'Name' column header" do + it "Should sort products alphabetically in descending/ascending order" do + within products_table do + name_header = page.find('th > a[data-column="name"]') + + # Sort in descending order + name_header.click + expect(page).to have_content("Name ▼") # this indicates the re-sorted content has loaded + + input_content = page.find_all('input[type=text]').map(&:value).join + expect(input_content).to match /Bananas.*Apples/ + + # Sort in ascending order + name_header.click + expect(page).to have_content("Name ▲") # this indicates the re-sorted content has loaded + + input_content = page.find_all('input[type=text]').map(&:value).join + expect(input_content).to match /Apples.*Bananas/ + end + end + end end describe "pagination" do From 881da0f9ac8665bd3e46fbbebc803276551ee1c2 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Fri, 10 May 2024 04:05:29 +0500 Subject: [PATCH 3/3] 11058: add get_all_input_values --- spec/system/admin/products_v3/products_spec.rb | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/spec/system/admin/products_v3/products_spec.rb b/spec/system/admin/products_v3/products_spec.rb index 5f332407bb..1cddd3d42c 100644 --- a/spec/system/admin/products_v3/products_spec.rb +++ b/spec/system/admin/products_v3/products_spec.rb @@ -34,11 +34,8 @@ describe 'As an enterprise user, I can manage my products', feature: :admin_styl it "Should sort products alphabetically by default in ascending order" do within products_table do - # Gather input values, because page.content doesn't include them. - input_content = page.find_all('input[type=text]').map(&:value).join - # Products are in correct order. - expect(input_content).to match /Apples.*Bananas/ + expect(all_input_values).to match /Apples.*Bananas/ end end @@ -50,16 +47,12 @@ describe 'As an enterprise user, I can manage my products', feature: :admin_styl # Sort in descending order name_header.click expect(page).to have_content("Name ▼") # this indicates the re-sorted content has loaded - - input_content = page.find_all('input[type=text]').map(&:value).join - expect(input_content).to match /Bananas.*Apples/ + expect(all_input_values).to match /Bananas.*Apples/ # Sort in ascending order name_header.click expect(page).to have_content("Name ▲") # this indicates the re-sorted content has loaded - - input_content = page.find_all('input[type=text]').map(&:value).join - expect(input_content).to match /Apples.*Bananas/ + expect(all_input_values).to match /Apples.*Bananas/ end end end @@ -1199,4 +1192,8 @@ describe 'As an enterprise user, I can manage my products', feature: :admin_styl Spree::TaxCategory .pluck(:name).sample end + + def all_input_values + page.find_all('input[type=text]').map(&:value).join + end end