diff --git a/app/product/[handle]/page.tsx b/app/product/[handle]/page.tsx index 63b205c92..4c15cdd3e 100644 --- a/app/product/[handle]/page.tsx +++ b/app/product/[handle]/page.tsx @@ -14,7 +14,7 @@ import Link from 'next/link'; export const runtime = 'edge'; export async function generateMetadata({ - params + params, }: { params: { handle: string }; }): Promise { @@ -51,7 +51,7 @@ export async function generateMetadata({ }; } -export default async function ProductPage({ params }: { params: { handle: string } }) { +export default async function ProductPage({ params, searchParams }: { params: { handle: string }; searchParams: URLSearchParams }) { const product = await getProduct(params.handle); if (!product) return notFound(); @@ -93,7 +93,7 @@ export default async function ProductPage({ params }: { params: { handle: string
- +
diff --git a/components/cart/add-to-cart.tsx b/components/cart/add-to-cart.tsx index 15582acc3..fd0c31446 100644 --- a/components/cart/add-to-cart.tsx +++ b/components/cart/add-to-cart.tsx @@ -2,65 +2,37 @@ import { PlusIcon } from '@heroicons/react/24/outline'; import clsx from 'clsx'; -import { addItem } from 'components/cart/actions'; import LoadingDots from 'components/loading-dots'; -import { ProductVariant } from 'lib/shopify/types'; -import { useRouter, useSearchParams } from 'next/navigation'; -import { useEffect, useState, useTransition } from 'react'; +import { experimental_useFormStatus as useFormStatus } from 'react-dom'; export function AddToCart({ - variants, - availableForSale + availableForSale, + addToCart, }: { - variants: ProductVariant[]; availableForSale: boolean; + addToCart: any; }) { - const [selectedVariantId, setSelectedVariantId] = useState(variants[0]?.id); - const router = useRouter(); - const searchParams = useSearchParams(); - const [isPending, startTransition] = useTransition(); - - useEffect(() => { - const variant = variants.find((variant: ProductVariant) => - variant.selectedOptions.every( - (option) => option.value === searchParams.get(option.name.toLowerCase()) - ) - ); - - if (variant) { - setSelectedVariantId(variant.id); - } - }, [searchParams, variants, setSelectedVariantId]); + const { pending } = useFormStatus() return ( - + )} + > +
+ {!pending ? : } +
+ {availableForSale ? 'Add To Cart' : 'Out Of Stock'} + + ); } diff --git a/components/product/product-description.tsx b/components/product/product-description.tsx index a81a6b02c..bbf4d4e0c 100644 --- a/components/product/product-description.tsx +++ b/components/product/product-description.tsx @@ -2,9 +2,32 @@ import { AddToCart } from 'components/cart/add-to-cart'; import Price from 'components/price'; import Prose from 'components/prose'; import { Product } from 'lib/shopify/types'; -import { VariantSelector } from './variant-selector'; +// import { VariantSelector } from './variant-selector'; + +export function ProductDescription({ product, searchParams }: { product: Product; searchParams: URLSearchParams }) { + async function addToCart() { + 'use server'; + + if (!product.availableForSale) return; + + console.log(product.variants) + const variant = product.variants.find((variant: ProductVariant) => + variant.selectedOptions.every( + (option) => option.value === searchParams.get(option.name.toLowerCase()) + ) + ); + + const variantId = variant?.id || product.variants[0]!.id + + console.log(variantId) + // const error = await addItem(variantId); + + // if (error) { + // console.error(error); + // return; + // } + } -export function ProductDescription({ product }: { product: Product }) { return ( <>
@@ -16,7 +39,7 @@ export function ProductDescription({ product }: { product: Product }) { />
- + {/* */} {product.descriptionHtml ? ( ) : null} - + ); }