1
1
/* eslint-disable react/prop-types */
2
2
import React , { useEffect , useState } from 'react' ;
3
3
import PropTypes from 'prop-types' ;
4
- import { camelCaseObject , getConfig } from '@edx/frontend-platform' ;
4
+ import { getConfig } from '@edx/frontend-platform' ;
5
5
import { sendTrackEvent } from '@edx/frontend-platform/analytics' ;
6
- import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth' ;
7
6
import { FormattedMessage , useIntl } from '@edx/frontend-platform/i18n' ;
8
7
import { Lightbulb , MoneyFilled } from '@openedx/paragon/icons' ;
9
8
import {
@@ -16,7 +15,12 @@ import { useModel } from '../../generic/model-store';
16
15
import StreakMobileImage from './assets/Streak_mobile.png' ;
17
16
import StreakDesktopImage from './assets/Streak_desktop.png' ;
18
17
import messages from './messages' ;
19
- import { recordModalClosing , recordStreakCelebration } from './utils' ;
18
+ import {
19
+ calculateVoucherDiscountPercentage ,
20
+ getDiscountCodePercentage ,
21
+ recordModalClosing ,
22
+ recordStreakCelebration ,
23
+ } from './utils' ;
20
24
21
25
function getRandomFactoid ( intl , streakLength ) {
22
26
const boldedSectionA = intl . formatMessage ( messages . streakFactoidABoldedSection ) ;
@@ -42,13 +46,6 @@ function getRandomFactoid(intl, streakLength) {
42
46
return factoids [ Math . floor ( Math . random ( ) * ( factoids . length ) ) ] ;
43
47
}
44
48
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
-
52
49
const CloseText = ( { intl } ) => (
53
50
< span >
54
51
{ intl . formatMessage ( messages . streakButton ) }
@@ -83,34 +80,38 @@ const StreakModal = ({
83
80
84
81
// Ask ecommerce to calculate discount savings
85
82
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
114
115
} , [ streakDiscountCouponEnabled , username , verifiedMode ] ) ;
115
116
116
117
if ( ! isStreakCelebrationOpen ) {
0 commit comments