const onSubmitLoyaltyPromoCode = useCallback(async () => {
setPromoCodeValidationLoading(true)
// const loyaltyId = 'ec5cec01-b41b-509b-9111-310ab5a18154'
let event = createRbiApplyPromoCodeEvent(promoCodeInput, 'Successful')
const personalizedOffer = await redeemMutation(user?.loyaltyId || '', promoCodeInput)
.catch((e: PromoCodeError) => {
const reason = buildErrorMessageFromPromoCodeError(e)
setPromoCodeErrorMessageId(
(reason as TPromoCodeErrorMessageIds) || PromoCodeErrorMessageIds.default
)
logger.error(`Error validating promo code ${e}`)
event = createRbiApplyPromoCodeEvent(promoCodeInput, 'Failed', e.message)
})
.finally(() => {
setPromoCodeValidationLoading(false)
})
trackEvent(event)
if (personalizedOffer) {
await handleRedemption(personalizedOffer)
// clear promo code input & error message
setPromoCodeErrorMessageId(null)
setPromoCodeInput('')
toast.success(formatMessage({ id: 'offerAddedToCart' }))
}
}, [
formatMessage,
handleRedemption,
promoCodeInput,
redeemMutation,
trackEvent,
user?.loyaltyId,
])
useEffect(() => {
if (appliedOfferPromoCode?.loyaltyEngineId) {
const standardOffersLimit =
earningCalculationData?.EarningCalculation?.offerRedemptionLimits?.standardOffersLimit || 1
dispatch(actions.loyalty.setSelectedOffer(appliedOfferPromoCode))
dispatch(actions.loyalty.setAppliedOffers([appliedOfferPromoCode]))
if (isDiscountLoyaltyOffer(appliedOfferPromoCode)) {
// If limit of offers reached remove the first one
if (appliedOffers?.length >= standardOffersLimit) {
removeFromCart({ cartId: appliedOffers[0].cartId })
}
//Discount offers should not show menu item details
dispatch(
actions.loyalty.applyOffer({
id: appliedOfferPromoCode.loyaltyEngineId,
type: OfferType.GLOBAL,
isStackable: appliedOfferPromoCode.isStackable,
isSurprise: isSurpriseOffer(appliedOfferPromoCode),
cmsId: appliedOfferPromoCode._id,
cartId: 'discount-offer',
})
)
}
dispatch(actions.loyalty.setCmsOffers([appliedOfferPromoCode]))
return
}
dispatch(actions.loyalty.setSelectedOffer(null))
dispatch(actions.loyalty.setAppliedOffers([]))
dispatch(actions.loyalty.setCmsOffers([]))
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [appliedOfferPromoCode]) |