Skip to content

Commit 292d0f0

Browse files
committed
fix: Issues with Product types
1 parent 79cbbf1 commit 292d0f0

File tree

17 files changed

+7472
-9542
lines changed

17 files changed

+7472
-9542
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"dependencies": {
2323
"@headlessui/react": "^1.6.1",
2424
"@hookform/error-message": "^2.0.0",
25-
"@medusajs/medusa-js": "^1.3.7",
25+
"@medusajs/medusa-js": "^5.0.0",
2626
"@meilisearch/instant-meilisearch": "^0.7.1",
2727
"@paypal/paypal-js": "^5.0.6",
2828
"@paypal/react-paypal-js": "^7.8.1",
@@ -31,7 +31,7 @@
3131
"@tanstack/react-query": "^4.22.4",
3232
"clsx": "^1.1.1",
3333
"lodash": "^4.17.21",
34-
"medusa-react": "^4.0.4",
34+
"medusa-react": "^8.0.0",
3535
"next": "^12.2.0",
3636
"react": "17.0.2",
3737
"react-country-flag": "^3.0.2",
@@ -43,6 +43,7 @@
4343
},
4444
"devDependencies": {
4545
"@babel/core": "^7.17.5",
46+
"@medusajs/client-types": "^0.2.2",
4647
"@medusajs/medusa": "^1.7.5",
4748
"@types/node": "17.0.21",
4849
"@types/react": "17.0.40",

src/lib/context/checkout-context.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export type CheckoutFormValues = {
4343

4444
interface CheckoutContext {
4545
cart?: Omit<Cart, "refundable_amount" | "refunded_total">
46-
shippingMethods: { label: string; value: string; price: string }[]
46+
shippingMethods: { label?: string; value?: string; price: string }[]
4747
isLoading: boolean
4848
readyToComplete: boolean
4949
sameAsBilling: StateType
@@ -242,7 +242,7 @@ export const CheckoutProvider = ({ children }: CheckoutProviderProps) => {
242242
const prepareFinalSteps = () => {
243243
initPayment()
244244

245-
if (shippingMethods) {
245+
if (shippingMethods?.length && shippingMethods?.[0]?.value) {
246246
setShippingOption(shippingMethods[0].value)
247247
}
248248
}

src/lib/context/product-context.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import React, {
1111
} from "react"
1212
import { Product, Variant } from "types/medusa"
1313
import { useStore } from "./store-context"
14+
import { PricedProduct } from "@medusajs/medusa/dist/types/pricing"
1415

1516
interface ProductContext {
1617
formattedPrice: string
@@ -30,7 +31,7 @@ const ProductActionContext = createContext<ProductContext | null>(null)
3031

3132
interface ProductProviderProps {
3233
children?: React.ReactNode
33-
product: Product
34+
product: PricedProduct
3435
}
3536

3637
export const ProductProvider = ({
@@ -44,12 +45,12 @@ export const ProductProvider = ({
4445

4546
const { addItem } = useStore()
4647
const { cart } = useCart()
47-
const { variants } = product
48+
const variants = product.variants as unknown as Variant[]
4849

4950
useEffect(() => {
5051
// initialize the option state
5152
const optionObj: Record<string, string> = {}
52-
for (const option of product.options) {
53+
for (const option of (product.options || [])) {
5354
Object.assign(optionObj, { [option.id]: undefined })
5455
}
5556
setOptions(optionObj)

src/lib/data/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { medusaClient } from "@lib/config"
2-
import { Product, StoreGetProductsParams } from "@medusajs/medusa"
2+
import { StoreGetProductsParams } from "@medusajs/medusa"
3+
import { PricedProduct } from "@medusajs/medusa/dist/types/pricing"
34

45
const COL_LIMIT = 15
56

6-
const getFeaturedProducts = async (): Promise<Product[]> => {
7+
const getFeaturedProducts = async (): Promise<PricedProduct[]> => {
78
const payload = {} as Record<string, unknown>
89

910
if (process.env.FEATURED_PRODUCTS) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { medusaClient } from "@lib/config"
22
import { getPercentageDiff } from "@lib/util/get-precentage-diff"
33
import { Product, ProductCollection, Region } from "@medusajs/medusa"
4+
import { PricedProduct } from "@medusajs/medusa/dist/types/pricing"
45
import { useQuery } from "@tanstack/react-query"
56
import { formatAmount, useCart } from "medusa-react"
67
import { ProductPreviewType } from "types/global"
@@ -57,12 +58,12 @@ const fetchFeaturedProducts = async (
5758
cart_id: cartId,
5859
})
5960
.then(({ products }) => products)
60-
.catch((_) => [] as Product[])
61+
.catch((_) => [] as PricedProduct[])
6162

6263
return products
6364
.filter((p) => !!p.variants)
6465
.map((p) => {
65-
const variants = p.variants as CalculatedVariant[]
66+
const variants = p.variants as unknown as CalculatedVariant[]
6667

6768
const cheapestVariant = variants.reduce((acc, curr) => {
6869
if (acc.calculated_price > curr.calculated_price) {
@@ -72,10 +73,10 @@ const fetchFeaturedProducts = async (
7273
}, variants[0])
7374

7475
return {
75-
id: p.id,
76-
title: p.title,
77-
handle: p.handle,
78-
thumbnail: p.thumbnail,
76+
id: p.id!,
77+
title: p.title!,
78+
handle: p.handle!,
79+
thumbnail: p.thumbnail!,
7980
price: cheapestVariant
8081
? {
8182
calculated_price: formatAmount({

src/lib/hooks/use-previews.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import transformProductPreview from "@lib/util/transform-product-preview"
22
import { Product, Region } from "@medusajs/medusa"
3+
import { PricedProduct } from "@medusajs/medusa/dist/types/pricing"
34
import { useMemo } from "react"
45
import { InfiniteProductPage, ProductPreviewType } from "types/global"
56

@@ -17,7 +18,7 @@ const usePreviews = <T extends InfiniteProductPage>({
1718
return []
1819
}
1920

20-
const products: Product[] = []
21+
const products: PricedProduct[] = []
2122

2223
for (const page of pages) {
2324
products.push(...page.response.products)

src/lib/hooks/use-product-price.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const useProductPrice = ({ id, variantId }: useProductPriceProps) => {
3838
return null
3939
}
4040

41-
const variants = product.variants as CalculatedVariant[]
41+
const variants = product.variants as unknown as CalculatedVariant[]
4242

4343
const cheapestVariant = variants.reduce((prev, curr) => {
4444
return prev.calculated_price < curr.calculated_price ? prev : curr
@@ -70,7 +70,7 @@ const useProductPrice = ({ id, variantId }: useProductPriceProps) => {
7070

7171
const variant = product.variants.find(
7272
(v) => v.id === variantId || v.sku === variantId
73-
) as CalculatedVariant
73+
) as unknown as CalculatedVariant
7474

7575
if (!variant) {
7676
return null

src/lib/util/transform-product-preview.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { getPercentageDiff } from "@lib/util/get-precentage-diff"
2-
import { Product, Region } from "@medusajs/medusa"
2+
import { Region } from "@medusajs/medusa"
3+
import { PricedProduct } from "@medusajs/medusa/dist/types/pricing"
34
import { formatAmount } from "medusa-react"
45
import { ProductPreviewType } from "types/global"
56
import { CalculatedVariant } from "types/medusa"
67

78
const transformProductPreview = (
8-
product: Product,
9+
product: PricedProduct,
910
region: Region
1011
): ProductPreviewType => {
11-
const variants = product.variants as CalculatedVariant[]
12+
const variants = product.variants as unknown as CalculatedVariant[]
1213

1314
let cheapestVariant = undefined
1415

@@ -22,10 +23,10 @@ const transformProductPreview = (
2223
}
2324

2425
return {
25-
id: product.id,
26-
title: product.title,
27-
handle: product.handle,
28-
thumbnail: product.thumbnail,
26+
id: product.id!,
27+
title: product.title!,
28+
handle: product.handle!,
29+
thumbnail: product.thumbnail!,
2930
price: cheapestVariant
3031
? {
3132
calculated_price: formatAmount({

src/modules/checkout/components/shipping/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { Controller, useForm } from "react-hook-form"
1111
import StepContainer from "../step-container"
1212

1313
type ShippingOption = {
14-
value: string
15-
label: string
14+
value?: string
15+
label?: string
1616
price: string
1717
}
1818

src/modules/products/components/mobile-actions/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import clsx from "clsx"
99
import React, { Fragment, useMemo } from "react"
1010
import { Product } from "types/medusa"
1111
import OptionSelect from "../option-select"
12+
import { PricedProduct } from "@medusajs/medusa/dist/types/pricing"
1213

1314
type MobileActionsProps = {
14-
product: Product
15+
product: PricedProduct
1516
show: boolean
1617
}
1718

1819
const MobileActions: React.FC<MobileActionsProps> = ({ product, show }) => {
1920
const { variant, addToCart, options, inStock, updateOptions } = useProductActions()
2021
const { state, open, close } = useToggleState()
2122

22-
const price = useProductPrice({ id: product.id, variantId: variant?.id })
23+
const price = useProductPrice({ id: product.id!, variantId: variant?.id })
2324

2425
const selectedPrice = useMemo(() => {
2526
const { variantPrice, cheapestPrice } = price
@@ -122,7 +123,7 @@ const MobileActions: React.FC<MobileActionsProps> = ({ product, show }) => {
122123
<div className="bg-white px-6 py-12">
123124
{product.variants.length > 1 && (
124125
<div className="flex flex-col gap-y-6">
125-
{product.options.map((option) => {
126+
{(product.options || []).map((option) => {
126127
return (
127128
<div key={option.id}>
128129
<OptionSelect

0 commit comments

Comments
 (0)