From c786f300cae2bf13c8e2516b6cfefe735f720c28 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 29 Jun 2023 17:34:43 +0200 Subject: [PATCH] Have a tiny url state management by using `replace_state` of cable_ready --- app/controllers/admin/products_v3_controller.rb | 5 ++++- app/reflexes/products_reflex.rb | 11 +++++++++++ app/views/admin/products_v3/index.html.haml | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index ede851965d..d4fc3f7b4e 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -2,6 +2,9 @@ module Admin class ProductsV3Controller < Spree::Admin::BaseController - def index; end + def index + @page = params[:page] || 1 + @per_page = params[:per_page] || 15 + end end end diff --git a/app/reflexes/products_reflex.rb b/app/reflexes/products_reflex.rb index c3e7a54cb2..2932f772dc 100644 --- a/app/reflexes/products_reflex.rb +++ b/app/reflexes/products_reflex.rb @@ -28,6 +28,10 @@ class ProductsReflex < ApplicationReflex locals: { products: @products, pagy: @pagy }) ).broadcast + cable_ready.replace_state( + url: current_url, + ).broadcast_later + morph :nothing end @@ -60,4 +64,11 @@ class ProductsReflex < ApplicationReflex # ] ] end + + def current_url + url = URI(request.original_url) + url.query = url.query.present? ? "#{url.query}&" : "" + url.query += "page=#{@page}&per_page=#{@per_page}" + url.to_s + end end diff --git a/app/views/admin/products_v3/index.html.haml b/app/views/admin/products_v3/index.html.haml index 54dd7f363a..d5551ead36 100644 --- a/app/views/admin/products_v3/index.html.haml +++ b/app/views/admin/products_v3/index.html.haml @@ -10,7 +10,7 @@ = render partial: 'spree/admin/shared/product_sub_menu' -#products_v3_page{"data-controller": "products"} +#products_v3_page{"data-controller": "products", "data-page": @page , "data-perpage": @per_page} #loading-spinner.spinner-container{ "data-controller": "loading" } .spinner = t('.loading')