11/* eslint-disable react/prop-types */
22import React , { useEffect , useState } from 'react' ;
33import PropTypes from 'prop-types' ;
4- import { camelCaseObject , getConfig } from '@edx/frontend-platform' ;
4+ import { getConfig } from '@edx/frontend-platform' ;
55import { sendTrackEvent } from '@edx/frontend-platform/analytics' ;
6- import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth' ;
76import { FormattedMessage , useIntl } from '@edx/frontend-platform/i18n' ;
87import { Lightbulb , MoneyFilled } from '@openedx/paragon/icons' ;
98import {
@@ -16,7 +15,12 @@ import { useModel } from '../../generic/model-store';
1615import StreakMobileImage from './assets/Streak_mobile.png' ;
1716import StreakDesktopImage from './assets/Streak_desktop.png' ;
1817import messages from './messages' ;
19- import { recordModalClosing , recordStreakCelebration } from './utils' ;
18+ import {
19+ calculateVoucherDiscountPercentage ,
20+ getDiscountCodePercentage ,
21+ recordModalClosing ,
22+ recordStreakCelebration ,
23+ } from './utils' ;
2024
2125function getRandomFactoid ( intl , streakLength ) {
2226 const boldedSectionA = intl . formatMessage ( messages . streakFactoidABoldedSection ) ;
@@ -42,13 +46,6 @@ function getRandomFactoid(intl, streakLength) {
4246 return factoids [ Math . floor ( Math . random ( ) * ( factoids . length ) ) ] ;
4347}
4448
45- async function calculateVoucherDiscount ( voucher , sku , username ) {
46- const urlBase = `${ getConfig ( ) . ECOMMERCE_BASE_URL } /api/v2/baskets/calculate` ;
47- const url = `${ urlBase } /?code=${ voucher } &sku=${ sku } &username=${ username } ` ;
48- return getAuthenticatedHttpClient ( ) . get ( url )
49- . then ( res => camelCaseObject ( res ) ) ;
50- }
51-
5249const CloseText = ( { intl } ) => (
5350 < span >
5451 { intl . formatMessage ( messages . streakButton ) }
@@ -83,34 +80,38 @@ const StreakModal = ({
8380
8481 // Ask ecommerce to calculate discount savings
8582 useEffect ( ( ) => {
86- if ( streakDiscountCouponEnabled && verifiedMode && getConfig ( ) . ECOMMERCE_BASE_URL ) {
87- calculateVoucherDiscount ( discountCode , verifiedMode . sku , username )
88- . then (
89- ( result ) => {
90- const { totalInclTax, totalInclTaxExclDiscounts } = result . data ;
91- if ( totalInclTaxExclDiscounts && totalInclTax !== totalInclTaxExclDiscounts ) {
92- // Just store the percent (rather than using these values directly), because ecommerce doesn't give us
93- // the currency symbol to use, so we want to use the symbol that LMS gives us. And I don't want to assume
94- // ecommerce's currency is the same as the LMS. So we'll keep using the values in verifiedMode, just
95- // multiplied by the calculated percentage.
96- setDiscountPercent ( 1 - totalInclTax / totalInclTaxExclDiscounts ) ;
97- sendTrackEvent ( 'edx.bi.course.streak_discount_enabled' , {
98- course_id : courseId ,
99- sku : verifiedMode . sku ,
100- } ) ;
101- } else {
102- setDiscountPercent ( 0 ) ;
103- }
104- } ,
105- ( ) => {
106- // ignore any errors - we just won't show the discount to the user then
107- setDiscountPercent ( 0 ) ;
108- } ,
109- ) ;
110- } else {
111- setDiscountPercent ( 0 ) ;
112- }
113- // eslint-disable-next-line react-hooks/exhaustive-deps
83+ ( async ( ) => {
84+ let streakDiscountPercentage = 0 ;
85+ try {
86+ if ( streakDiscountCouponEnabled && verifiedMode ) {
87+ // If the discount service is available, use it to get the discount percentage
88+ if ( getConfig ( ) . DISCOUNT_CODE_INFO_URL ) {
89+ streakDiscountPercentage = await getDiscountCodePercentage (
90+ discountCode ,
91+ courseId ,
92+ ) ;
93+ // If the discount service is not available, fall back to ecommerce to calculate the discount percentage
94+ } else if ( getConfig ( ) . ECOMMERCE_BASE_URL ) {
95+ streakDiscountPercentage = await calculateVoucherDiscountPercentage (
96+ discountCode ,
97+ verifiedMode . sku ,
98+ username ,
99+ ) ;
100+ }
101+ }
102+ } catch {
103+ // ignore any errors - we just won't show the discount to the user then
104+ } finally {
105+ if ( streakDiscountPercentage ) {
106+ sendTrackEvent ( 'edx.bi.course.streak_discount_enabled' , {
107+ course_id : courseId ,
108+ sku : verifiedMode . sku ,
109+ } ) ;
110+ }
111+ setDiscountPercent ( streakDiscountPercentage ) ;
112+ }
113+ } ) ( ) ;
114+ // eslint-disable-next-line react-hooks/exhaustive-deps
114115 } , [ streakDiscountCouponEnabled , username , verifiedMode ] ) ;
115116
116117 if ( ! isStreakCelebrationOpen ) {
0 commit comments