From 824070a54b18be6b12222bc2428c487a5865ed26 Mon Sep 17 00:00:00 2001 From: andr-ew Date: Thu, 22 Jun 2023 17:18:16 -0500 Subject: [PATCH] separate out PurchaseInput & input components (prep for interaction) --- app/(page)/product/[handle]/page.js | 63 ++-------------------------- components/input.js | 24 +++++++++++ components/product/purchase-input.js | 43 +++++++++++++++++++ 3 files changed, 70 insertions(+), 60 deletions(-) create mode 100644 components/input.js create mode 100644 components/product/purchase-input.js diff --git a/app/(page)/product/[handle]/page.js b/app/(page)/product/[handle]/page.js index fc8daf106..c4932e8f3 100644 --- a/app/(page)/product/[handle]/page.js +++ b/app/(page)/product/[handle]/page.js @@ -2,6 +2,8 @@ import xss from 'xss'; import { getProducts, getProduct } from 'lib/shopify'; +import PurchaseInput from '/components/product/purchase-input.js'; + export async function generateStaticParams() { const products = await getProducts({ sortKey: 'UPDATED_AT', @@ -12,36 +14,10 @@ export async function generateStaticParams() { return products.map(product => ({ product: product.handle })); } -export async function Option({ value, children, selected }) { - return ; -} - -export async function Select({ id, label, children }) { - return ( -
- - {/* TODO: parentheses around label w/ css */} - -
- ); -} - //TODO: NumberInput export default async function ProductPage({ params: { handle } }) { const product = await getProduct(handle); - - const hasOptions = product?.options?.[0]?.values.length > 1 ?? false; - //TODO: turn these checks into shared functions - // const onSale = - // (compareAtPriceRange?.minVariantPrice?.amount ?? 0) > - // (priceRange?.minVariantPrice?.amount ?? 0) || - // (compareAtPriceRange?.maxVariantPrice?.amount ?? 0) > - // (priceRange?.maxVariantPrice?.amount ?? 0); - const isForSale = (product?.priceRange?.maxVariantPrice?.amount ?? 0) > 0; - return ( <> {product?.handle ? ( @@ -52,40 +28,7 @@ export default async function ProductPage({ params: { handle } }) { __html: xss(product.descriptionHtml), }} /> - {product?.availableForSale && isForSale && ( - <> -
- - - -
- <> - {hasOptions && - product?.options?.map(option => ( - - ))} - - - )} + ) : (

Product not found

diff --git a/components/input.js b/components/input.js new file mode 100644 index 000000000..92f4059d2 --- /dev/null +++ b/components/input.js @@ -0,0 +1,24 @@ +export function Option({ children, ...props }) { + return ; +} + +export function Select({ id, label, children, ...props }) { + return ( +
+ + {/* TODO: parentheses around label w/ css */} + +
+ ); +} + +export function NumberInput({ id, label, ...props }) { + return ( +
+ + +
+ ); +} diff --git a/components/product/purchase-input.js b/components/product/purchase-input.js new file mode 100644 index 000000000..212cbc37d --- /dev/null +++ b/components/product/purchase-input.js @@ -0,0 +1,43 @@ +'use client'; + +import { Option, Select, NumberInput } from '/components/input.js'; + +export default function PurchaseInput({ product }) { + const hasOptions = product?.options?.[0]?.values.length > 1 ?? false; + //TODO: turn these checks into shared functions + // const onSale = + // (compareAtPriceRange?.minVariantPrice?.amount ?? 0) > + // (priceRange?.minVariantPrice?.amount ?? 0) || + // (compareAtPriceRange?.maxVariantPrice?.amount ?? 0) > + // (priceRange?.maxVariantPrice?.amount ?? 0); + const isForSale = (product?.priceRange?.maxVariantPrice?.amount ?? 0) > 0; + + return ( + product?.availableForSale && + isForSale && ( + <> + + <> + {hasOptions && + product?.options?.map(option => ( + + ))} + + + ) + ); +}