Skip to content

Commit 4616ed4

Browse files
committed
fix: collection infinite scroll
1 parent ffab067 commit 4616ed4

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

src/app/collections/route.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import medusaRequest from "@lib/medusa-fetch"
22
import { NextRequest, NextResponse } from "next/server"
33

4+
export const runtime = "edge"
5+
46
export async function GET(request: NextRequest) {
57
const searchParams = request.nextUrl.searchParams
68
const handle = searchParams.get("handle") ?? ""
79
const pageParam = searchParams.get("pageParam") ?? "0"
10+
const limit = searchParams.get("limit") ?? "12"
11+
12+
console.log({ handle, pageParam, limit })
813

914
const collection = await fetchCollection(handle)
1015
.then((res) => res)
@@ -15,6 +20,7 @@ export async function GET(request: NextRequest) {
1520
const { products, count, nextPage } = await fetchCollectionProducts({
1621
pageParam,
1722
id: collection.id,
23+
limit,
1824
}).then((res) => res)
1925

2026
return NextResponse.json({
@@ -38,24 +44,30 @@ async function fetchCollection(handle: string) {
3844
}
3945

4046
async function fetchCollectionProducts({
41-
pageParam = "0",
47+
pageParam,
4248
id,
49+
limit,
4350
}: {
44-
pageParam?: string
51+
pageParam: string
4552
id: string
53+
limit: string
4654
}) {
4755
const { products, count, offset } = await medusaRequest(
4856
"GET",
4957
`/products?collection_id[]=${id}`,
5058
{
51-
limit: "12",
59+
limit,
5260
offset: pageParam,
61+
expand: "variants,variants.prices",
5362
}
5463
).then((res) => res.body)
5564

5665
return {
57-
products: [...products, ...products, ...products, ...products],
58-
count: 15,
59-
nextPage: count > offset + 12 ? offset + 12 : null,
66+
products,
67+
count,
68+
nextPage:
69+
count > parseInt(offset) + parseInt(limit)
70+
? parseInt(offset) + parseInt(limit)
71+
: null,
6072
}
6173
}

src/app/store/layout.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import PageLayout from "app/page-layout"
2+
3+
export default function StoreLayout({
4+
children,
5+
}: {
6+
children: React.ReactNode
7+
}) {
8+
return <PageLayout>{children}</PageLayout>
9+
}

src/app/store/page.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { Metadata } from "next"
2-
import Store from "./store"
3-
import PageLayout from "app/page-layout"
2+
import StoreTemplate from "@modules/store/templates"
43

54
export const metadata: Metadata = {
65
title: "Store",
76
description: "Explore all of our products.",
87
}
98

109
export default function StorePage() {
11-
return (
12-
<PageLayout>
13-
<Store />
14-
</PageLayout>
15-
)
10+
return <StoreTemplate />
1611
}

src/modules/collections/templates/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ type CollectionTemplateProps = {
2020
const BASEURL = process.env.NEXT_PUBLIC_BASE_URL
2121

2222
const fetchCollectionProducts = async ({
23-
pageParam,
23+
pageParam = 0,
2424
handle,
2525
cartId,
2626
}: {
27-
pageParam?: string
27+
pageParam?: number
2828
handle: string
2929
cartId?: string
3030
}) => {
3131
const { response, nextPage } = await fetch(
32-
`${BASEURL}/collections?handle=${handle}&cart_id=${cartId}&page=${pageParam}`
32+
`${BASEURL}/collections?handle=${handle}&cart_id=${cartId}&page=${pageParam.toString()}`
3333
).then((res) => res.json())
3434
return {
3535
response,

src/app/store/store.tsx renamed to src/modules/store/templates/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import InfiniteProducts from "@modules/products/components/infinite-products"
55
import RefinementList from "@modules/store/components/refinement-list"
66
import { useState } from "react"
77

8-
const Store = () => {
8+
const StoreTemplate = () => {
99
const [params, setParams] = useState<StoreGetProductsParams>({})
1010

1111
return (
@@ -16,4 +16,4 @@ const Store = () => {
1616
)
1717
}
1818

19-
export default Store
19+
export default StoreTemplate

0 commit comments

Comments
 (0)