Skip to content

Fix pagination #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/(dashboard)/products-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Button } from '@/components/ui/button';
export function ProductsTable({
products,
offset,
totalProducts
totalProducts,
}: {
products: SelectProduct[];
offset: number;
Expand All @@ -34,11 +34,11 @@ export function ProductsTable({
let productsPerPage = 5;

function prevPage() {
router.back();
router.push(`/?offset=${offset - productsPerPage}`, { scroll: false });
}

function nextPage() {
router.push(`/?offset=${offset}`, { scroll: false });
router.push(`/?offset=${offset + productsPerPage}`, { scroll: false });
}

return (
Expand Down Expand Up @@ -80,7 +80,7 @@ export function ProductsTable({
<div className="text-xs text-muted-foreground">
Showing{' '}
<strong>
{Math.min(offset - productsPerPage, totalProducts) + 1}-{offset}
{totalProducts === 0 ? 0 : offset + 1}-{Math.min(offset + productsPerPage, totalProducts)}
</strong>{' '}
of <strong>{totalProducts}</strong> products
</div>
Expand All @@ -90,7 +90,7 @@ export function ProductsTable({
variant="ghost"
size="sm"
type="submit"
disabled={offset === productsPerPage}
disabled={offset < productsPerPage}
>
<ChevronLeft className="mr-2 h-4 w-4" />
Prev
Expand All @@ -100,7 +100,7 @@ export function ProductsTable({
variant="ghost"
size="sm"
type="submit"
disabled={offset + productsPerPage > totalProducts}
disabled={offset + productsPerPage >= totalProducts}
>
Next
<ChevronRight className="ml-2 h-4 w-4" />
Expand Down
29 changes: 15 additions & 14 deletions lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,34 @@ export async function getProducts(
offset: number
): Promise<{
products: SelectProduct[];
newOffset: number | null;
newOffset: number;
totalProducts: number;
}> {
// Always search the full table, not per page
if (search) {
let totalProducts = await db
.select({ count: count() })
.from(products)
.where(ilike(products.name, `%${search}%`))
let moreProducts = await db.select()
.from(products)
.where(ilike(products.name, `%${search}%`))
.limit(5)
.offset(offset);

return {
products: await db
.select()
.from(products)
.where(ilike(products.name, `%${search}%`))
.limit(1000),
newOffset: null,
totalProducts: 0
products: moreProducts,
newOffset: offset,
totalProducts: totalProducts[0].count
};
}

if (offset === null) {
return { products: [], newOffset: null, totalProducts: 0 };
}

let totalProducts = await db.select({ count: count() }).from(products);
let moreProducts = await db.select().from(products).limit(5).offset(offset);
let newOffset = moreProducts.length >= 5 ? offset + 5 : null;

return {
products: moreProducts,
newOffset,
newOffset: offset,
totalProducts: totalProducts[0].count
};
}
Expand Down