diff --git a/components/layout/navbar/index.tsx b/components/layout/navbar/index.tsx index 7efa42270..554da5180 100644 --- a/components/layout/navbar/index.tsx +++ b/components/layout/navbar/index.tsx @@ -1,7 +1,10 @@ import Link from 'next/link'; +import Cart from 'components/cart'; +import CartIcon from 'components/icons/cart'; import LogoIcon from 'components/icons/logo'; import { Menu } from 'lib/medusa/types'; +import { Suspense } from 'react'; import MobileMenu from './mobile-menu'; import Search from './search'; @@ -40,10 +43,10 @@ export default async function Navbar() {
- {/* }> */} - {/* @ts-expect-error Server Component */} - {/* */} - {/* */} + }> + {/* @ts-expect-error Server Component */} + +
); diff --git a/lib/medusa/index.ts b/lib/medusa/index.ts index 49bdb44f2..c4d3cae30 100644 --- a/lib/medusa/index.ts +++ b/lib/medusa/index.ts @@ -4,8 +4,12 @@ import { MedusaCart, MedusaProduct, MedusaProductCollection, + MedusaProductOption, + MedusaProductVariant, Product, - ProductCollection + ProductCollection, + ProductOption, + ProductVariant } from './types'; // const endpoint = `${process.env.MEDUSA_BACKEND_API!}`; @@ -80,6 +84,11 @@ const reshapeProduct = (product: MedusaProduct): Product => { altText: product.images?.[0]?.id ?? '' }; const availableForSale = true; + const variants = product.variants.map((variant) => reshapeProductVariant(variant)); + + let options; + product.options && (options = product.options.map((option) => reshapeProductOption(option))); + // console.log({ options }); return { ...product, @@ -88,7 +97,43 @@ const reshapeProduct = (product: MedusaProduct): Product => { updatedAt, tags, descriptionHtml, - availableForSale + availableForSale, + options, + variants + }; +}; + +const reshapeProductOption = (productOption: MedusaProductOption): ProductOption => { + const availableForSale = true; + const name = productOption.title; + let values = productOption.values?.map((option) => option.value) || []; + values = [...new Set(values)]; + + return { + ...productOption, + availableForSale, + name, + values + }; +}; + +const reshapeProductVariant = (productVariant: MedusaProductVariant): ProductVariant => { + console.log({ productVariant }); + const availableForSale = !!productVariant.inventory_quantity; + const selectedOptions = + productVariant.options?.map((option) => ({ + name: option.option?.title ?? '', + value: option.value + })) || []; + const price = { + amount: productVariant.prices?.[0]?.amount.toString() ?? '', + currencyCode: productVariant.prices?.[0]?.currency_code ?? '' + }; + return { + ...productVariant, + availableForSale, + selectedOptions, + price }; }; @@ -193,8 +238,9 @@ export async function getCollections(): Promise { } export async function getProduct(handle: string): Promise { - const res = await medusaRequest('get', `/products?handle=${handle}&limit=1`); - return res.body.product; + const res = await medusaRequest('GET', `/products?handle=${handle}&limit=1`); + const product = res.body.products[0]; + return reshapeProduct(product); } export async function getProducts({ diff --git a/lib/medusa/types.ts b/lib/medusa/types.ts index c8d9b0970..8640d117b 100644 --- a/lib/medusa/types.ts +++ b/lib/medusa/types.ts @@ -29,8 +29,8 @@ export type MedusaProduct = { status?: 'draft' | 'proposed' | 'published' | 'rejected'; images?: Array; thumbnail?: string | null; - options?: Array; - variants: Array; + options?: Array; + variants: Array; categories?: Array; profile_id?: string | null; profile?: ShippingProfile | null; @@ -52,7 +52,7 @@ export type MedusaProduct = { tags?: ProductTag[]; }; -export type Product = Omit & { +export type Product = Omit & { featuredImage: FeaturedImage; seo?: { title?: string; @@ -68,6 +68,8 @@ export type Product = Omit & { descriptionHtml: string; tags: Array; availableForSale: boolean; + options?: Array; + variants: Array; }; export type FeaturedImage = { @@ -127,7 +129,7 @@ export type ProductCategory = { updated_at: string; }; -export type ProductVariant = { +export type MedusaProductVariant = { id: string; title?: string; product_id: string; @@ -155,6 +157,51 @@ export type ProductVariant = { deleted_at: string | null; }; +export type ProductVariant = MedusaProductVariant & { + availableForSale: boolean; + selectedOptions: { + name: string; + value: string; + }[]; + price: Money; +}; + +export type MedusaProductOption = { + id: string; + title: string; + values?: ProductOptionValue[]; + product_id: string; + product?: Product | null; + created_at: string; + updated_at: string; + deleted_at?: string | null; + metadata?: Record | null; +}; + +export type ProductOption = Omit & { + availableForSale: boolean; + name: string; + values: string[]; +}; + +export type ProductOptionValue = { + id: string; + value: string; + option_id: string; + option?: MedusaProductOption | null; + variant_id: string; + variant?: MedusaProductVariant | null; + created_at: string; + updated_at: string; + deleted_at?: string | null; + metadata?: Record | null; +}; + +export type Money = { + amount: string; + currencyCode: string; +}; + type MoneyAmount = { id: string; currency_code: string; @@ -202,31 +249,6 @@ export type CustomerGroup = { name: string; }; -export type ProductOption = { - id: string; - title: string; - values?: ProductOptionValue[]; - product_id: string; - product?: Product | null; - created_at: string; - updated_at: string; - deleted_at?: string | null; - metadata?: Record | null; -}; - -export type ProductOptionValue = { - id: string; - value: string; - option_id: string; - option?: ProductOption | null; - variant_id: string; - variant?: ProductVariant | null; - created_at: string; - updated_at: string; - deleted_at?: string | null; - metadata?: Record | null; -}; - type ShippingOption = { id: string; name: string; @@ -287,3 +309,8 @@ export type Cart = Partial & { lines: []; totalQuantity: number; }; + +export type Menu = { + title: string; + path: string; +};