Skip to content

Commit 037675e

Browse files
handling multiple rule based and list based promotions
1 parent b02f97e commit 037675e

File tree

1 file changed

+62
-22
lines changed
  • packages/template-retail-react-app/app/components/product-view

1 file changed

+62
-22
lines changed

packages/template-retail-react-app/app/components/product-view/index.jsx

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ const ProductView = forwardRef(
145145
const [promotionIdToSearch, setPromotionIdToSearch] = useState(null)
146146
const {data: bonusProductSearchResult} = useBonusProductSearch(promotionIdToSearch)
147147

148+
// State to track all promotion IDs and their results
149+
const [promotionResults, setPromotionResults] = useState({})
150+
const [pendingPromotionIds, setPendingPromotionIds] = useState([])
151+
152+
// Effect to handle multiple promotions sequentially
153+
useEffect(() => {
154+
if (bonusProductSearchResult?.hits?.length > 0 && promotionIdToSearch) {
155+
// Store the result for current promotion
156+
setPromotionResults((prev) => ({
157+
...prev,
158+
[promotionIdToSearch]: bonusProductSearchResult
159+
}))
160+
161+
// Move to next promotion if any pending
162+
if (pendingPromotionIds.length > 0) {
163+
const nextPromotionId = pendingPromotionIds[0]
164+
setPendingPromotionIds((prev) => prev.slice(1))
165+
setPromotionIdToSearch(nextPromotionId)
166+
}
167+
}
168+
}, [bonusProductSearchResult, promotionIdToSearch, pendingPromotionIds])
169+
148170
const {
149171
showLoading,
150172
showInventoryMessage,
@@ -309,36 +331,54 @@ const ProductView = forwardRef(
309331
if (isValidResponse) {
310332
// Show bonus product modal first if there are bonus items
311333
if (newBonusItems?.length > 0) {
312-
// Update bonusProducts list with the new bonus items
313-
let isRuleBasedPromotion = !newBonusItems.some(
334+
let bonusProductsToShow = []
335+
let ruleBasedBonusProducts = []
336+
let listBasedBonusProducts = newBonusItems.filter(
314337
(item) => item.bonusProducts
315338
)
316-
let bonusProductsToShow = []
339+
// Collect all promotion IDs for rule-based promotions
340+
const promotionIds = newBonusItems
341+
.filter((item) => !item.bonusProducts) // Rule-based promotions don't have bonusProducts
342+
.map((item) => item.promotionId)
343+
.filter(Boolean)
344+
345+
//create a map of promotionId and ids for rule based promotions
346+
const promotionIdToIdMap = newBonusItems
347+
.filter((item) => !item.bonusProducts)
348+
.reduce((acc, item) => {
349+
acc[item.promotionId] = item.id
350+
return acc
351+
}, {})
352+
353+
// Start sequential processing if we have promotion IDs
354+
if (promotionIds.length > 0) {
355+
// Set first promotion ID and queue the rest
356+
setPromotionIdToSearch(promotionIds[0])
357+
setPendingPromotionIds(promotionIds.slice(1))
358+
}
317359

318-
if (isRuleBasedPromotion) {
319-
setPromotionIdToSearch(newBonusItems[0].promotionId)
320-
let ruleBasedBonusProducts = []
321-
if (bonusProductSearchResult?.hits?.length > 0) {
322-
bonusProductSearchResult.hits.forEach((bonusProduct) => {
323-
ruleBasedBonusProducts.push({
360+
// Combine all stored results
361+
Object.entries(promotionResults).forEach(([promotionId, result]) => {
362+
if (result?.hits?.length > 0) {
363+
let formattedBonusProducts = result.hits.map((bonusProduct) => {
364+
return {
324365
productId: bonusProduct.productId,
325366
productName: bonusProduct.productName,
326367
c_productUrl: bonusProduct.c_productUrl
327-
})
368+
}
328369
})
329-
bonusProductsToShow = ruleBasedBonusProducts
330-
}
331-
} else {
332-
let listBasedBonusProducts = []
333-
newBonusItems[0].bonusProducts.forEach((bonusProduct) => {
334-
listBasedBonusProducts.push({
335-
productId: bonusProduct.productId,
336-
productName: bonusProduct.productName,
337-
title: bonusProduct.title
370+
ruleBasedBonusProducts.push({
371+
bonusProducts: formattedBonusProducts,
372+
id: promotionIdToIdMap[promotionId]
338373
})
339-
})
340-
bonusProductsToShow = listBasedBonusProducts
341-
}
374+
}
375+
})
376+
377+
bonusProductsToShow = [
378+
...ruleBasedBonusProducts,
379+
...listBasedBonusProducts
380+
]
381+
342382
addBonusProducts(newBonusItems)
343383
onBonusProductModalOpen({
344384
newBonusItems: bonusProductsToShow,

0 commit comments

Comments
 (0)