Skip to content

Commit

Permalink
Merge pull request #54 from buildheadless/pdp
Browse files Browse the repository at this point in the history
Fix ShopPayButton flickering
  • Loading branch information
thomasKn authored Dec 8, 2023
2 parents 445b689 + 1ae8730 commit 6a248b5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import type {loader} from '~/routes/($locale).products.$productHandle';

import {ProductForm} from '../product/ProductForm';

export function AddToCartButtonBlock(
props: InferType<typeof ADD_TO_CART_BUTTON_BLOCK>,
) {
export type AddToCartButtonBlockProps = InferType<
typeof ADD_TO_CART_BUTTON_BLOCK
>;

export function AddToCartButtonBlock(props: AddToCartButtonBlockProps) {
const loaderData = useLoaderData<typeof loader>();
const variantsPromise = loaderData.variants;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import type {loader} from '~/routes/($locale).products.$productHandle';

import {VariantPrice} from '../product/VariantPrice';

export function PriceBlock(props: InferType<typeof PRICE_BLOCK>) {
export type PriceBlockProps = InferType<typeof PRICE_BLOCK>;

export function PriceBlock(props: PriceBlockProps) {
const loaderData = useLoaderData<typeof loader>();
const variantsPromise = loaderData.variants;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {useProduct} from '@shopify/hydrogen-react';

import type {SHOPIFY_DESCRIPTION_BLOCK} from '~/qroq/blocks';

export function ShopifyDescriptionBlock(
props: InferType<typeof SHOPIFY_DESCRIPTION_BLOCK>,
) {
export type ShopifyDescriptionBlockProps = InferType<
typeof SHOPIFY_DESCRIPTION_BLOCK
>;

export function ShopifyDescriptionBlock(props: ShopifyDescriptionBlockProps) {
const {product} = useProduct() as {product: ProductQuery['product']};

return product ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {useProduct} from '@shopify/hydrogen-react';

import type {SHOPIFY_TITLE_BLOCK} from '~/qroq/blocks';

export function ShopifyTitleBlock(
props: InferType<typeof SHOPIFY_TITLE_BLOCK>,
) {
export type ShopifyTitleBlockProps = InferType<typeof SHOPIFY_TITLE_BLOCK>;

export function ShopifyTitleBlock(props: ShopifyTitleBlockProps) {
const {product} = useProduct() as {product: ProductQuery['product']};

return <h1>{product?.title}</h1>;
Expand Down
41 changes: 26 additions & 15 deletions templates/hydrogen-theme/app/components/product/ProductDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {PortableText} from '@portabletext/react';
import {useMemo} from 'react';

import type {AddToCartButtonBlockProps} from '../blocks/AddToCartButtonBlock';
import type {PriceBlockProps} from '../blocks/PriceBlock';
import type {ShopifyDescriptionBlockProps} from '../blocks/ShopifyDescriptionBlock';
import type {ShopifyTitleBlockProps} from '../blocks/ShopifyTitleBlock';
import type {ProductInformationSectionProps} from '../sections/ProductInformationSection';

import {AddToCartButtonBlock} from '../blocks/AddToCartButtonBlock';
Expand All @@ -8,24 +13,30 @@ import {ShopifyDescriptionBlock} from '../blocks/ShopifyDescriptionBlock';
import {ShopifyTitleBlock} from '../blocks/ShopifyTitleBlock';

export function ProductDetails(props: {data: ProductInformationSectionProps}) {
const components = useMemo(
() => ({
types: {
addToCartButton: (props: {value: AddToCartButtonBlockProps}) => {
return <AddToCartButtonBlock {...props.value} />;
},
price: (props: {value: PriceBlockProps}) => (
<PriceBlock {...props.value} />
),
shopifyDescription: (props: {value: ShopifyDescriptionBlockProps}) => (
<ShopifyDescriptionBlock {...props.value} />
),
shopifyTitle: (props: {value: ShopifyTitleBlockProps}) => (
<ShopifyTitleBlock {...props.value} />
),
},
}),
[],
);

return (
<div className="space-y-4">
{props.data.richtext && (
<PortableText
components={{
types: {
addToCartButton: ({value}) => {
return <AddToCartButtonBlock {...value} />;
},
price: ({value}) => <PriceBlock {...value} />,
shopifyDescription: ({value}) => (
<ShopifyDescriptionBlock {...value} />
),
shopifyTitle: ({value}) => <ShopifyTitleBlock {...value} />,
},
}}
value={props.data.richtext}
/>
<PortableText components={components} value={props.data.richtext} />
)}
</div>
);
Expand Down

0 comments on commit 6a248b5

Please sign in to comment.