Skip to content

Commit

Permalink
Rough refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarta committed May 16, 2024
1 parent c0be4cc commit e75813f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const ProductTile = (props) => {
<DisplayPrice priceData={priceData} currency={currency} />

{/* Promotion call-out message */}
<PromoCallout priceData={priceData} />
{product.productPromotions && <PromoCallout product={{...product, variants}} />}
</Link>
{enableFavourite && (
<Box
Expand Down Expand Up @@ -199,7 +199,8 @@ ProductTile.propTypes = {

bundle: PropTypes.bool,
item: PropTypes.bool
})
}),
productPromotions: PropTypes.array
}),
/**
* Enable adding/removing product as a favourite.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
*/
import React from 'react'
import PropTypes from 'prop-types'
import {getVariantWithLowestPrice} from '@salesforce/retail-react-app/app/utils/product-utils'

const PromoCallout = ({priceData}) => {
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
}
Expand All @@ -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
52 changes: 33 additions & 19 deletions packages/template-retail-react-app/app/utils/product-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e75813f

Please sign in to comment.