scroll infinito de productos
This commit is contained in:
@@ -1,12 +1,36 @@
|
||||
'use client'
|
||||
import { useSafeQuery } from "@/hooks/use-safe-query";
|
||||
import { useSafeQuery, useSafeInfiniteQuery } from "@/hooks/use-safe-query";
|
||||
import { getInventoryAction, getAllProducts } from "../actions/actions";
|
||||
// import { useInfiniteQuery } from "@tanstack/react-query";
|
||||
|
||||
interface Params {
|
||||
page?: number;
|
||||
limit?: number;
|
||||
search?: string;
|
||||
sortBy?: string;
|
||||
sortOrder?: 'asc' | 'desc';
|
||||
}
|
||||
|
||||
// Hook for users
|
||||
export function useProductQuery(params = {}) {
|
||||
export function useProductQuery(params: Params = {}) {
|
||||
return useSafeQuery(['product',params], () => getInventoryAction(params))
|
||||
}
|
||||
|
||||
export function useAllProductQuery(params = {}) {
|
||||
export function useAllProductQuery(params: Params = {}) {
|
||||
return useSafeQuery(['product',params], () => getAllProducts(params))
|
||||
}
|
||||
|
||||
export function useAllProductInfiniteQuery() {
|
||||
return useSafeInfiniteQuery(
|
||||
['product'],
|
||||
// pageParam + 1 para evitar duplicación de datos
|
||||
({ pageParam = 0 }) => getAllProducts({ page: pageParam + 1, limit: 10 }),
|
||||
(lastPage, allPages) => {
|
||||
// Esta lógica determina el 'pageParam' para la siguiente página
|
||||
const nextPage = allPages.length;
|
||||
// Puedes añadir una condición para saber si hay más páginas
|
||||
if (lastPage.data.length < 10) return undefined;
|
||||
return nextPage;
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user