Skip to content

Commit 10cfdfd

Browse files
committed
fix: update featured products on region change
1 parent ce42a82 commit 10cfdfd

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

src/lib/data/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ export const fetchProductsList = async ({
1414
const params = new URLSearchParams(queryParams as Record<string, string>)
1515

1616
const { products, count, nextPage } = await fetch(
17-
`${API_BASE_URL}/api/products?limit=12&offset=${pageParam}&${params.toString()}`
17+
`${API_BASE_URL}/api/products?limit=12&offset=${pageParam}&${params.toString()}`,
18+
{
19+
next: {
20+
tags: ["products"],
21+
},
22+
}
1823
).then((res) => res.json())
1924

2025
return {

src/lib/hooks/use-layout-data.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { medusaClient } from "@lib/config"
21
import { getPercentageDiff } from "@lib/util/get-precentage-diff"
3-
import { Product, ProductCollection, Region } from "@medusajs/medusa"
2+
import { ProductCollection, Region } from "@medusajs/medusa"
43
import { PricedProduct } from "@medusajs/medusa/dist/types/pricing"
54
import { useQuery } from "@tanstack/react-query"
65
import { formatAmount, useCart } from "medusa-react"
@@ -19,7 +18,12 @@ const fetchCollectionData = async (): Promise<LayoutCollection[]> => {
1918

2019
do {
2120
await fetch(
22-
`${process.env.NEXT_PUBLIC_BASE_URL}/api/collections?offset=${offset}`
21+
`${process.env.NEXT_PUBLIC_BASE_URL}/api/collections?offset=${offset}`,
22+
{
23+
next: {
24+
tags: ["collections"],
25+
},
26+
}
2327
)
2428
.then((res) => res.json())
2529
.then(({ collections: newCollections, count: newCount }) => {
@@ -54,7 +58,12 @@ const fetchFeaturedProducts = async (
5458
region: Region
5559
): Promise<ProductPreviewType[]> => {
5660
const products: PricedProduct[] = await fetch(
57-
`${process.env.NEXT_PUBLIC_BASE_URL}/api/products?limit=4&cart_id=${cartId}`
61+
`${process.env.NEXT_PUBLIC_BASE_URL}/api/products?limit=4&cart_id=${cartId}&region_id=${region.id}`,
62+
{
63+
next: {
64+
tags: ["products"],
65+
},
66+
}
5867
)
5968
.then((res) => res.json())
6069
.then(({ products }) => products)

src/lib/util/get-product-prices.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { ProductDTO, ProductVariantDTO } from "@medusajs/types/dist/product"
66
* @param data Array of product objects (with variants) to get prices for
77
* @param cartId (Optional) cart id to get region-specific prices
88
*/
9-
export default async function getPrices(data: ProductDTO[], cartId?: string) {
9+
export default async function getPrices(
10+
data: ProductDTO[],
11+
cartId?: string,
12+
regionId?: string
13+
) {
1014
if (!data || !data.length) {
1115
return []
1216
}
@@ -25,6 +29,10 @@ export default async function getPrices(data: ProductDTO[], cartId?: string) {
2529
query.cart_id = cartId
2630
}
2731

32+
if (regionId) {
33+
query.region_id = regionId
34+
}
35+
2836
// Get all products with variants and prices from Medusa API
2937
const productsWithVariants = await medusaRequest("GET", `/products`, {
3038
query,

src/modules/layout/components/country-select/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const CountrySelect = () => {
4040
}, [countryCode, options])
4141

4242
const handleChange = (option: CountryOption) => {
43-
revalidateTags(["medusa_request"])
43+
revalidateTags(["medusa_request", "products", "collections"])
4444
setRegion(option.region, option.country)
4545
close()
4646
}

0 commit comments

Comments
 (0)