-
Notifications
You must be signed in to change notification settings - Fork 214
@W-18820722 Refactor PDP for better readability and maintainability #2668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kevinxh
merged 20 commits into
feature/chakra-ui-upgrade-v3
from
@W-18820722-refactor-pdp
Jun 27, 2025
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
727b30a
move product details component
kevinxh fa5ee8c
fix bug where cause product bundle to not work
kevinxh a3267d2
create simple product and composite product
kevinxh a410cc4
rename partial pdp components
kevinxh 0a442ae
create useProductDetailData hook
kevinxh b7a09ab
fix copyright
kevinxh 8a1ad44
reorder imports
kevinxh 24936b2
extract analytics
kevinxh 0c8368f
extract pdp wishlist logic
kevinxh 4f356a0
extract recommended products section
kevinxh 79dc224
lint
kevinxh 912ca64
rename page metadata
kevinxh f0023c7
extract page cache
kevinxh e8be233
add comments
kevinxh cfad42a
extract page-analytics
kevinxh d925fd4
lint
kevinxh 00a10d4
debug analytics
kevinxh fbff22a
remove console logs
kevinxh 1d285db
fix product set add to cart issue
kevinxh 400e93e
lint
kevinxh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
666 changes: 65 additions & 601 deletions
666
packages/extension-chakra-storefront/src/pages/product-detail/index.jsx
Large diffs are not rendered by default.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
packages/extension-chakra-storefront/src/pages/product-detail/index.test.js
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
packages/extension-chakra-storefront/src/pages/product-detail/page-analytics.jsx
This file contains hidden or 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,59 @@ | ||
| /* | ||
| * Copyright (c) 2025, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
|
|
||
| import {useEffect} from 'react' | ||
| import PropTypes from 'prop-types' | ||
| import useEinstein from '../../hooks/use-einstein' | ||
| import useDataCloud from '../../hooks/use-datacloud' | ||
| import useActiveData from '../../hooks/use-active-data' | ||
| import logger from '../../utils/logger-instance' | ||
|
|
||
| const PageAnalytics = ({product, category}) => { | ||
| const einstein = useEinstein() | ||
| const dataCloud = useDataCloud() | ||
| const activeData = useActiveData() | ||
|
|
||
| useEffect(() => { | ||
| if (product && product.type.set) { | ||
| einstein.sendViewProduct(product) | ||
| dataCloud.sendViewProduct(product) | ||
| const childrenProducts = product.setProducts | ||
| childrenProducts.map((child) => { | ||
| try { | ||
| einstein.sendViewProduct(child) | ||
| } catch (err) { | ||
| logger.error('Einstein sendViewProduct error', { | ||
| namespace: 'useProductAnalytics.useEffect', | ||
| additionalProperties: {error: err, child} | ||
| }) | ||
| } | ||
| activeData.sendViewProduct(category, child, 'detail') | ||
| dataCloud.sendViewProduct(child) | ||
| }) | ||
| } else if (product) { | ||
| try { | ||
| einstein.sendViewProduct(product) | ||
| } catch (err) { | ||
| logger.error('Einstein sendViewProduct error', { | ||
| namespace: 'useProductAnalytics.useEffect', | ||
| additionalProperties: {error: err, product} | ||
| }) | ||
| } | ||
| activeData.sendViewProduct(category, product, 'detail') | ||
| dataCloud.sendViewProduct(product) | ||
| } | ||
| }, [product, category]) | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| PageAnalytics.propTypes = { | ||
| product: PropTypes.object, | ||
| category: PropTypes.object | ||
| } | ||
|
|
||
| export default PageAnalytics |
22 changes: 22 additions & 0 deletions
22
packages/extension-chakra-storefront/src/pages/product-detail/page-cache.jsx
This file contains hidden or 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,22 @@ | ||
| /* | ||
| * Copyright (c) 2025, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import {useServerContext} from '@salesforce/pwa-kit-react-sdk/ssr/universal/hooks' | ||
| import {useExtensionConfig} from '../../hooks' | ||
| /* | ||
| * This component is used to set the cache headers for the page. | ||
| */ | ||
| export default function PageCache() { | ||
| const {res} = useServerContext() | ||
| const {maxCacheAge: MAX_CACHE_AGE, staleWhileRevalidate: STALE_WHILE_REVALIDATE} = | ||
| useExtensionConfig() | ||
| if (res) { | ||
| res.set( | ||
| 'Cache-Control', | ||
| `s-maxage=${MAX_CACHE_AGE}, stale-while-revalidate=${STALE_WHILE_REVALIDATE}` | ||
| ) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
157 changes: 157 additions & 0 deletions
157
...tension-chakra-storefront/src/pages/product-detail/partials/product-details-composite.jsx
This file contains hidden or 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,157 @@ | ||
| /* | ||
| * Copyright (c) 2025, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import React, {Fragment} from 'react' | ||
| import PropTypes from 'prop-types' | ||
| import {Box} from '@chakra-ui/react' | ||
| import ProductView from '../../../components/product-view' | ||
| import InformationAccordion from './information-accordion' | ||
|
|
||
| /** | ||
| * This component is used to render the product details for a composite product. | ||
| * | ||
| * A composite product is a product that is made up of multiple products. | ||
| * | ||
| * It can be a set or a bundle. | ||
| */ | ||
| const CompositeProductDetails = ({ | ||
| product, | ||
| primaryCategory, | ||
| isProductASet, | ||
| isProductABundle, | ||
| isProductLoading, | ||
| isBasketLoading, | ||
| isWishlistLoading, | ||
| handleAddToWishlist, | ||
| handleAddToCart, | ||
| handleProductSetAddToCart, | ||
| handleProductBundleAddToCart, | ||
| handleChildProductValidation, | ||
| childProductOrderability, | ||
| setSelectedBundleQuantity, | ||
| comboProduct, | ||
| childProductRefs, | ||
| selectedBundleQuantity, | ||
| setChildProductSelection, | ||
| childProductSelection, | ||
| setChildProductOrderability | ||
| }) => { | ||
| return ( | ||
| <Fragment> | ||
| <ProductView | ||
| product={product} | ||
| category={primaryCategory?.parentCategoryTree || []} | ||
| addToCart={isProductASet ? handleProductSetAddToCart : handleProductBundleAddToCart} | ||
| addToWishlist={handleAddToWishlist} | ||
| isProductLoading={isProductLoading} | ||
| isBasketLoading={isBasketLoading} | ||
| isWishlistLoading={isWishlistLoading} | ||
| validateOrderability={handleChildProductValidation} | ||
| childProductOrderability={childProductOrderability} | ||
| setSelectedBundleQuantity={setSelectedBundleQuantity} | ||
| /> | ||
|
|
||
| <hr /> | ||
|
|
||
| {/* TODO: consider `childProduct.belongsToSet` */} | ||
| { | ||
| // Render the child products | ||
| comboProduct.childProducts.map( | ||
| ({product: childProduct, quantity: childQuantity}) => ( | ||
| <Box key={childProduct.id} data-testid="child-product"> | ||
| <ProductView | ||
| // Do not use an arrow function as we are manipulating the functions scope. | ||
| ref={function (productViewRef) { | ||
| // The ref callback will be called with `null` when the component unmounts. | ||
| // We need to guard against that to prevent a runtime error. | ||
| if (productViewRef) { | ||
| // Assign the "set" scope of the ref, this is how we access the internal | ||
| // validation. | ||
| childProductRefs.current[childProduct.id] = { | ||
| ref: productViewRef, | ||
| validateOrderability: | ||
| productViewRef.validateOrderability | ||
| } | ||
| } | ||
| }} | ||
| product={childProduct} | ||
| isProductPartOfSet={isProductASet} | ||
| isProductPartOfBundle={isProductABundle} | ||
| childOfBundleQuantity={childQuantity} | ||
| selectedBundleParentQuantity={selectedBundleQuantity} | ||
| addToCart={ | ||
| isProductASet | ||
| ? (variant, quantity) => | ||
| handleAddToCart([ | ||
| { | ||
| product: childProduct, | ||
| variant, | ||
| quantity | ||
| } | ||
| ]) | ||
| : null | ||
| } | ||
| addToWishlist={isProductASet ? handleAddToWishlist : null} | ||
| onVariantSelected={(product, variant, quantity) => { | ||
| if (quantity) { | ||
| setChildProductSelection((previousState) => ({ | ||
| ...previousState, | ||
| [product.id]: { | ||
| product, | ||
| variant, | ||
| quantity: isProductABundle | ||
| ? childQuantity | ||
| : quantity | ||
| } | ||
| })) | ||
| } else { | ||
| const selections = {...childProductSelection} | ||
| delete selections[product.id] | ||
| setChildProductSelection(selections) | ||
| } | ||
| }} | ||
| isProductLoading={isProductLoading} | ||
| isBasketLoading={isBasketLoading} | ||
| isWishlistLoading={isWishlistLoading} | ||
| setChildProductOrderability={setChildProductOrderability} | ||
| /> | ||
| <InformationAccordion product={childProduct} /> | ||
|
|
||
| <Box display={['none', 'none', 'none', 'block']}> | ||
| <hr /> | ||
| </Box> | ||
| </Box> | ||
| ) | ||
| ) | ||
| } | ||
| </Fragment> | ||
| ) | ||
| } | ||
|
|
||
| CompositeProductDetails.propTypes = { | ||
| product: PropTypes.object, | ||
| primaryCategory: PropTypes.object, | ||
| isProductASet: PropTypes.bool, | ||
| isProductABundle: PropTypes.bool, | ||
| isProductLoading: PropTypes.bool, | ||
| isBasketLoading: PropTypes.bool, | ||
| isWishlistLoading: PropTypes.bool, | ||
| handleAddToWishlist: PropTypes.func, | ||
| handleAddToCart: PropTypes.func, | ||
| handleProductSetAddToCart: PropTypes.func, | ||
| handleProductBundleAddToCart: PropTypes.func, | ||
| handleChildProductValidation: PropTypes.func, | ||
| childProductOrderability: PropTypes.object, | ||
| setSelectedBundleQuantity: PropTypes.func, | ||
| comboProduct: PropTypes.object, | ||
| childProductRefs: PropTypes.object, | ||
| selectedBundleQuantity: PropTypes.number, | ||
| setChildProductSelection: PropTypes.func, | ||
| childProductSelection: PropTypes.object, | ||
| setChildProductOrderability: PropTypes.func | ||
| } | ||
|
|
||
| export default CompositeProductDetails |
47 changes: 47 additions & 0 deletions
47
.../extension-chakra-storefront/src/pages/product-detail/partials/product-details-simple.jsx
This file contains hidden or 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,47 @@ | ||
| /* | ||
| * Copyright (c) 2025, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import React, {Fragment} from 'react' | ||
| import PropTypes from 'prop-types' | ||
| import ProductView from '../../../components/product-view' | ||
| import InformationAccordion from './information-accordion' | ||
|
|
||
| const SimpleProductDetails = ({ | ||
| product, | ||
| primaryCategory, | ||
| isProductLoading, | ||
| isBasketLoading, | ||
| isWishlistLoading, | ||
| handleAddToWishlist, | ||
| handleAddToCart | ||
| }) => { | ||
| return ( | ||
| <Fragment> | ||
| <ProductView | ||
| product={product} | ||
| category={primaryCategory?.parentCategoryTree || []} | ||
| addToCart={(variant, quantity) => handleAddToCart([{product, variant, quantity}])} | ||
| addToWishlist={handleAddToWishlist} | ||
| isProductLoading={isProductLoading} | ||
| isBasketLoading={isBasketLoading} | ||
| isWishlistLoading={isWishlistLoading} | ||
| /> | ||
| <InformationAccordion product={product} /> | ||
| </Fragment> | ||
| ) | ||
| } | ||
|
|
||
| SimpleProductDetails.propTypes = { | ||
| product: PropTypes.object, | ||
| primaryCategory: PropTypes.object, | ||
| isProductLoading: PropTypes.bool, | ||
| isBasketLoading: PropTypes.bool, | ||
| isWishlistLoading: PropTypes.bool, | ||
| handleAddToWishlist: PropTypes.func, | ||
| handleAddToCart: PropTypes.func | ||
| } | ||
|
|
||
| export default SimpleProductDetails |
45 changes: 45 additions & 0 deletions
45
packages/extension-chakra-storefront/src/pages/product-detail/partials/product-details.jsx
This file contains hidden or 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,45 @@ | ||
| /* | ||
| * Copyright (c) 2025, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import React from 'react' | ||
| import PropTypes from 'prop-types' | ||
| import SimpleProductDetails from './product-details-simple' | ||
| import CompositeProductDetails from './product-details-composite' | ||
|
|
||
| const ProductDetails = (props) => { | ||
| const {isProductASet, isProductABundle} = props | ||
|
|
||
| if (isProductASet || isProductABundle) { | ||
| return <CompositeProductDetails {...props} /> | ||
| } | ||
|
|
||
| return <SimpleProductDetails {...props} /> | ||
| } | ||
|
|
||
| ProductDetails.propTypes = { | ||
| product: PropTypes.object, | ||
| primaryCategory: PropTypes.object, | ||
| isProductASet: PropTypes.bool, | ||
| isProductABundle: PropTypes.bool, | ||
| isProductLoading: PropTypes.bool, | ||
| isBasketLoading: PropTypes.bool, | ||
| isWishlistLoading: PropTypes.bool, | ||
| handleAddToWishlist: PropTypes.func, | ||
| handleAddToCart: PropTypes.func, | ||
| handleProductSetAddToCart: PropTypes.func, | ||
| handleProductBundleAddToCart: PropTypes.func, | ||
| handleChildProductValidation: PropTypes.func, | ||
| childProductOrderability: PropTypes.object, | ||
| setSelectedBundleQuantity: PropTypes.func, | ||
| comboProduct: PropTypes.object, | ||
| childProductRefs: PropTypes.object, | ||
| selectedBundleQuantity: PropTypes.number, | ||
| setChildProductSelection: PropTypes.func, | ||
| childProductSelection: PropTypes.object, | ||
| setChildProductOrderability: PropTypes.func | ||
| } | ||
|
|
||
| export default ProductDetails | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: how about merging this back into the, um, parent product details component? This isn't much code, so I think it doesn't need to be in its own component.