From e75813f03e7d2a5ce23ee59ac408a910b1713123 Mon Sep 17 00:00:00 2001 From: Vincent Marta Date: Thu, 16 May 2024 09:57:14 -0700 Subject: [PATCH] Rough refactoring --- .../app/components/product-tile/index.jsx | 5 +- .../components/product-tile/promo-callout.jsx | 17 +++--- .../app/utils/product-utils.js | 52 ++++++++++++------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/packages/template-retail-react-app/app/components/product-tile/index.jsx b/packages/template-retail-react-app/app/components/product-tile/index.jsx index af583ebb51..f003654db8 100644 --- a/packages/template-retail-react-app/app/components/product-tile/index.jsx +++ b/packages/template-retail-react-app/app/components/product-tile/index.jsx @@ -115,7 +115,7 @@ const ProductTile = (props) => { {/* Promotion call-out message */} - + {product.productPromotions && } {enableFavourite && ( { - const {variantWithLowestPrice} = priceData - const {minPrice, variant} = variantWithLowestPrice ?? {} +// TODO: support other product types (other than master/variants) +// - variant.productPromotions +// - product.productPromotions +const PromoCallout = ({product}) => { + const {variants} = product + const {minPrice, variant} = getVariantWithLowestPrice(variants) ?? {} if (!variant?.productPromotions) { return } @@ -22,12 +26,7 @@ const PromoCallout = ({priceData}) => { } PromoCallout.propTypes = { - priceData: PropTypes.shape({ - variantWithLowestPrice: PropTypes.shape({ - minPrice: PropTypes.number, - variant: PropTypes.object - }) - }) + product: PropTypes.object } export default PromoCallout diff --git a/packages/template-retail-react-app/app/utils/product-utils.js b/packages/template-retail-react-app/app/utils/product-utils.js index c6938d4770..1cb815c9ef 100644 --- a/packages/template-retail-react-app/app/utils/product-utils.js +++ b/packages/template-retail-react-app/app/utils/product-utils.js @@ -52,23 +52,7 @@ export const getPriceData = (product, opts = {}) => { // grab the variant that has the lowest price (including promotional price) if (isMaster) { const variants = product?.variants || [] - variantWithLowestPrice = variants.reduce( - (minVariant, variant) => { - const promotions = variant.productPromotions || [] - const smallestPromotionalPrice = getSmallestValByProperty( - promotions, - 'promotionalPrice' - ) - const variantSalePrice = - smallestPromotionalPrice && smallestPromotionalPrice < variant.price - ? smallestPromotionalPrice - : variant.price - return variantSalePrice < minVariant.minPrice - ? {minPrice: variantSalePrice, variant} - : minVariant - }, - {minPrice: Infinity, variant: null} - ) + variantWithLowestPrice = getVariantWithLowestPrice(variants) currentPrice = variantWithLowestPrice?.minPrice } else { const promotionalPrice = getSmallestValByProperty( @@ -112,11 +96,41 @@ export const getPriceData = (product, opts = {}) => { isRange: (isMaster && product?.variants?.length > 1) || isASet || false, // priceMax is for product set tieredPrice: closestTieredPrice?.price, - maxPrice: product?.priceMax || maxTieredPrice, - variantWithLowestPrice + maxPrice: product?.priceMax || maxTieredPrice } } +export const getVariantWithLowestPrice = (variants) => { + if (!variants || variants.length === 0) return + + return variants.reduce( + (minVariant, variant) => { + const promotions = variant.productPromotions || [] + const smallestPromotionalPrice = getSmallestValByProperty( + promotions, + 'promotionalPrice' + ) + const variantSalePrice = + smallestPromotionalPrice && smallestPromotionalPrice < variant.price + ? smallestPromotionalPrice + : variant.price + return variantSalePrice < minVariant.minPrice + ? {minPrice: variantSalePrice, variant} + : minVariant + }, + {minPrice: Infinity, variant: null} + ) +} + +// export const getPromoData = (product) => { + +// return { +// variantWithLowestPrice, +// isPromoPrice, +// getCurrentPrice +// } +// } + /** * @private * Find the smallest value by key from a given array