1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-03-21 12:29:03 +01:00

(bug) product pagination in store

This commit is contained in:
Du Peng 2022-09-09 17:14:22 +02:00
parent 9bfeaf3ef3
commit dff0cb26be
3 changed files with 27 additions and 17 deletions

View File

@ -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

View File

@ -49,13 +49,12 @@ const Store: React.FC<StoreProps> = ({ onError, onSuccess, currentUser }) => {
const [currentPage, setCurrentPage] = useState<number>(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<StoreProps> = ({ 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<StoreProps> = ({ 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 (
<div className="store">
<ul className="breadcrumbs">
@ -245,7 +252,7 @@ const Store: React.FC<StoreProps> = ({ onError, onSuccess, currentUser }) => {
))}
</div>
{pageCount > 1 &&
<FabPagination pageCount={pageCount} currentPage={currentPage} selectPage={setCurrentPage} />
<FabPagination pageCount={pageCount} currentPage={currentPage} selectPage={handlePagination} />
}
</div>
</div>

View File

@ -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