diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index c5c55e580..925c8d829 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -8,7 +8,7 @@ class API::ProductsController < API::ApiController def index @products = ProductService.list(params) - @pages = ProductService.pages if params[:page].present? + @pages = ProductService.pages(params) if params[:page].present? end def show diff --git a/app/frontend/src/javascript/components/store/store.tsx b/app/frontend/src/javascript/components/store/store.tsx index 05452b8dd..221172cff 100644 --- a/app/frontend/src/javascript/components/store/store.tsx +++ b/app/frontend/src/javascript/components/store/store.tsx @@ -49,13 +49,12 @@ const Store: React.FC = ({ onError, onSuccess, currentUser }) => { const [currentPage, setCurrentPage] = useState(1); useEffect(() => { - ProductAPI.index({ page: 1 }).then(data => { + ProductAPI.index({ page: 1, is_active: true }).then(data => { setPageCount(data.total_pages); setProducts(data.products); }).catch(() => { onError(t('app.public.store.unexpected_error_occurred')); }); - ProductCategoryAPI.index().then(data => { setProductCategories(data); formatCategories(data); @@ -70,14 +69,6 @@ const Store: React.FC = ({ onError, onSuccess, currentUser }) => { }); }, []); - useEffect(() => { - ProductAPI.index({ page: currentPage }).then(data => { - setProducts(data.products); - setPageCount(data.total_pages); - window.document.getElementById('content-main').scrollTo({ top: 100, behavior: 'smooth' }); - }); - }, [currentPage]); - /** * Create categories tree (parent/children) */ @@ -155,6 +146,22 @@ const Store: React.FC = ({ onError, onSuccess, currentUser }) => { onSuccess(t('app.public.store.add_to_cart_success')); }; + /** + * Handle products pagination + */ + const handlePagination = (page: number) => { + if (page !== currentPage) { + ProductAPI.index({ page, is_active: true }).then(data => { + setCurrentPage(page); + setProducts(data.products); + setPageCount(data.total_pages); + window.document.getElementById('content-main').scrollTo({ top: 100, behavior: 'smooth' }); + }).catch(() => { + onError(t('app.public.store.unexpected_error_occurred')); + }); + } + }; + return (
    @@ -245,7 +252,7 @@ const Store: React.FC = ({ onError, onSuccess, currentUser }) => { ))}
{pageCount > 1 && - + } diff --git a/app/services/product_service.rb b/app/services/product_service.rb index 7d0a398b5..52d069781 100644 --- a/app/services/product_service.rb +++ b/app/services/product_service.rb @@ -10,14 +10,17 @@ class ProductService state = filters[:disabled] == 'false' ? [nil, false] : true products = products.where(is_active: state) end - if filters[:page].present? - products = products.page(filters[:page]).per(PRODUCTS_PER_PAGE) - end + products = products.page(filters[:page]).per(PRODUCTS_PER_PAGE) if filters[:page].present? products end - def self.pages - Product.page(1).per(PRODUCTS_PER_PAGE).total_pages + def self.pages(filters) + products = Product.all + if filters[:is_active].present? + state = filters[:disabled] == 'false' ? [nil, false] : true + products = Product.where(is_active: state) + end + products.page(1).per(PRODUCTS_PER_PAGE).total_pages end # amount params multiplied by hundred