Use shared page controls on products screen

This has an auto submit and can potentially work with Turbo, like on the Orders screen.
This commit is contained in:
David Cook
2024-04-04 09:54:45 +11:00
committed by Filipe
parent 0fc3d39106
commit fd8be37a62
9 changed files with 25 additions and 41 deletions

View File

@@ -43,8 +43,8 @@ module Admin
def init_pagination_params
# prority is given to element dataset (if present) over url params
@page = params[:_page].presence || 1
@per_page = params[:_per_page].presence || 15
@page = params[:page].presence || 1
@per_page = params[:per_page].presence || 15
end
def producers

View File

@@ -5,10 +5,6 @@ class ProductsReflex < ApplicationReflex
before_reflex :init_filters_params, :init_pagination_params
def fetch
fetch_and_render_products_with_flash
end
def change_per_page
@per_page = element.value.to_i
@page = 1

View File

@@ -13,7 +13,7 @@
= render partial: 'sort', locals: { pagy: pagy, search_term: search_term, producer_id: producer_id, category_id: category_id }
= render partial: 'table', locals: { products: products }
- if pagy.pages > 1
= render partial: 'admin/shared/v3/pagy', locals: { pagy: pagy, reflex: "click->Products#fetch" }
= render partial: 'admin/shared/stimulus_pagination', locals: { pagy: pagy }
- else
#no-products
= render partial: "no_products", locals: { search_term: search_term, producer_id: producer_id, category_id: category_id }

View File

@@ -1,4 +1,7 @@
%form{ id: "filters" }
= form_with url: admin_products_path, id: "filters", method: :get, data: { remote: false, "search-target": "form" } do
= hidden_field_tag :page, @page, class: "page"
= hidden_field_tag :per_page, @per_page, class: "per-page"
.query
.search-input
= text_field_tag :search_term, search_term, placeholder: t('.search_products')

View File

@@ -2,8 +2,11 @@
%div
= t(".pagination.total_html", total: pagy.count, from: pagy.from, to: pagy.to)
- if search_term.present? || producer_id.present? || category_id.present?
%a{ href: "#", class: "button disruptive" }
%a{ href: url_for(per_page: @per_page, page: @page), class: "button disruptive" }
= t(".pagination.clear_search")
%form.with-dropdown
= t(".pagination.per_page.show")
= select_tag :per_page, options_for_select([15, 25, 50, 100].collect{|i| [t('.pagination.per_page.per_page', num: i), i]}, pagy.items), class: "no-input per-page", data: { reflex: "change->products#change_per_page", controller: "tom-select", "tom-select-options-value": '{ "plugins": [] }'}
= select_tag :per_page,
options_for_select([15, 25, 50, 100].collect{|i| [t('.pagination.per_page.per_page', num: i), i]}, pagy.items),
class: "no-input per-page",
data: { controller: "tom-select search", action: "change->search#changePerPage", "tom-select-options-value": '{ "plugins": [] }'}

View File

@@ -5,8 +5,8 @@
} } do |form|
= render(partial: "admin/shared/flashes", locals: { flashes: }) if defined? flashes
= hidden_field_tag :_page, @page
= hidden_field_tag :_per_page, @per_page
= hidden_field_tag :page, @page
= hidden_field_tag :per_page, @per_page
%table.products
%colgroup

View File

@@ -1,15 +0,0 @@
%nav.pagy_nav.pagination{"aria-label" => "pager", :role => "navigation"}
- if pagy.prev
%a.page.prev{ href: "#", id: "pagy-prev", "data-reflex": reflex, "data-perPage": pagy.items, "data-page": pagy.prev || 1, "aria-label": "previous"}
%i.icon-chevron-left
- pagy.series.each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36]
- if item.is_a?(Integer) # page link
%a.page{ href: "#", id:"pagy-#{item}", "data-reflex": reflex, "data-perPage": pagy.items, "data-page": item, "aria-label": "page #{item}"}
= item
- elsif item.is_a?(String) # current page
%span.page.current= item
- elsif item == :gap # page gap
%span.page.gap &hellip;
- if pagy.next
%a.page.next{ href: "#", id:"pagy-next", "data-reflex": reflex, "data-perPage": pagy.items, "data-page": pagy.next || pagy.last, "aria-label": "next"}
%i.icon-chevron-right

View File

@@ -848,13 +848,6 @@ en:
prompt: "This will permanently remove it from your list."
confirmation_text: "Delete variant"
cancellation_text: "Keep variant"
sort:
pagination:
total_html: "<strong>%{total} products</strong> found for your search criteria. Showing %{from} to %{to}."
per_page:
show: Show
per_page: "%{num} per page"
clear_search: Clear search
filters:
search_products: Search for products
search_for_producers: Search for producers
@@ -866,7 +859,13 @@ en:
categories:
label: Categories
search: Search
content:
sort:
pagination:
total_html: "<strong>%{total} products</strong> found for your search criteria. Showing %{from} to %{to}."
per_page:
show: Show
per_page: "%{num} per page"
clear_search: Clear search
no_products:
no_products_found: No products found
import_products: Import multiple products

View File

@@ -43,7 +43,7 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do
expect(page).to have_selector ".pagination"
expect_products_count_to_be 15
within ".pagination" do
click_link "2"
click_on "2"
end
expect(page).to have_content "Showing 16 to 16" # todo: remove unnecessary duplication
@@ -86,7 +86,7 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do
visit admin_products_url
within ".pagination" do
click_link "2"
click_on "2"
end
expect(page).to have_content "Showing 16 to 16"
@@ -617,9 +617,8 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do
visit admin_products_url
within ".pagination" do
click_link "2"
click_on "2"
end
within row_containing_name("zucchini") do
fill_in "Name", with: "zucchinis"
end
@@ -631,7 +630,6 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do
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
@@ -959,7 +957,7 @@ describe 'As an admin, I can manage products', feature: :admin_style_v3 do
end
def expect_page_to_be(page_number)
expect(page).to have_selector ".pagination span.page.current", text: page_number.to_s
expect(page).to have_selector ".pagination .page.current", text: page_number.to_s
end
def expect_per_page_to_be(per_page)