-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
templates/hydrogen-theme/app/components/blocks/PriceBlock.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type {InferType} from 'groqd'; | ||
|
||
import {Await, useLoaderData} from '@remix-run/react'; | ||
import {flattenConnection} from '@shopify/hydrogen-react'; | ||
import {Suspense} from 'react'; | ||
|
||
import type {PRICE_BLOCK} from '~/qroq/blocks'; | ||
import type {loader} from '~/routes/($locale).products.$productHandle'; | ||
|
||
import {VariantPrice} from '../product/VariantPrice'; | ||
|
||
export function PriceBlock(props: InferType<typeof PRICE_BLOCK>) { | ||
const loaderData = useLoaderData<typeof loader>(); | ||
const variantsPromise = loaderData.variants; | ||
|
||
return ( | ||
<> | ||
{/* Todo => Add skeleton and errorElement */} | ||
<Suspense> | ||
<Await resolve={variantsPromise}> | ||
{({product}) => { | ||
const variants = product?.variants?.nodes.length | ||
? flattenConnection(product.variants) | ||
: []; | ||
|
||
return <VariantPrice variants={variants} />; | ||
}} | ||
</Await> | ||
</Suspense> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
templates/hydrogen-theme/app/components/product/VariantPrice.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type {ProductVariantFragmentFragment} from 'storefrontapi.generated'; | ||
|
||
import {Money} from '@shopify/hydrogen-react'; | ||
|
||
import {useSelectedVariant} from '~/hooks/useSelectedVariant'; | ||
|
||
export function VariantPrice({ | ||
variants, | ||
}: { | ||
variants: ProductVariantFragmentFragment[]; | ||
}) { | ||
const selectedVariant = useSelectedVariant({variants}); | ||
const price = selectedVariant?.price; | ||
const compareAtPrice = selectedVariant?.compareAtPrice; | ||
const isOutOfStock = !selectedVariant?.availableForSale; | ||
|
||
const isOnSale = | ||
selectedVariant?.price?.amount && | ||
selectedVariant?.compareAtPrice?.amount && | ||
selectedVariant?.price?.amount < selectedVariant?.compareAtPrice?.amount; | ||
|
||
// Todo => Add sale and sold out badges | ||
return ( | ||
<div className="flex items-center gap-3"> | ||
{compareAtPrice && ( | ||
<Money className="line-through opacity-50" data={compareAtPrice} /> | ||
)} | ||
{price && <Money className="text-lg" data={price} />} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters