Skip to content

Commit 5d6fdf0

Browse files
committed
refactor: move functions to utils
1 parent b14617e commit 5d6fdf0

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

src/shared/streak-celebration/StreakCelebrationModal.jsx

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/* eslint-disable react/prop-types */
22
import React, { useEffect, useState } from 'react';
33
import PropTypes from 'prop-types';
4-
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
4+
import { getConfig } from '@edx/frontend-platform';
55
import { sendTrackEvent } from '@edx/frontend-platform/analytics';
6-
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
76
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
87
import { Lightbulb, MoneyFilled } from '@openedx/paragon/icons';
98
import {
@@ -16,7 +15,12 @@ import { useModel } from '../../generic/model-store';
1615
import StreakMobileImage from './assets/Streak_mobile.png';
1716
import StreakDesktopImage from './assets/Streak_desktop.png';
1817
import messages from './messages';
19-
import { recordModalClosing, recordStreakCelebration } from './utils';
18+
import {
19+
calculateVoucherDiscountPercentage,
20+
getDiscountCodePercentage,
21+
recordModalClosing,
22+
recordStreakCelebration,
23+
} from './utils';
2024

2125
function getRandomFactoid(intl, streakLength) {
2226
const boldedSectionA = intl.formatMessage(messages.streakFactoidABoldedSection);
@@ -42,36 +46,6 @@ function getRandomFactoid(intl, streakLength) {
4246
return factoids[Math.floor(Math.random() * (factoids.length))];
4347
}
4448

45-
async function calculateVoucherDiscountPercentage(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-
49-
const result = await getAuthenticatedHttpClient().get(url);
50-
const { totalInclTax, totalInclTaxExclDiscounts } = camelCaseObject(result).data;
51-
52-
if (totalInclTaxExclDiscounts && totalInclTax !== totalInclTaxExclDiscounts) {
53-
// Just store the percent (rather than using these values directly), because ecommerce doesn't give us
54-
// the currency symbol to use, so we want to use the symbol that LMS gives us. And I don't want to assume
55-
// ecommerce's currency is the same as the LMS. So we'll keep using the values in verifiedMode, just
56-
// multiplied by the calculated percentage.
57-
return 1 - totalInclTax / totalInclTaxExclDiscounts;
58-
}
59-
60-
return 0;
61-
}
62-
63-
async function getDiscountCodePercentage(code, courseId) {
64-
const params = new URLSearchParams();
65-
params.append('code', code);
66-
params.append('course_run_key', courseId);
67-
const url = `${getConfig().DISCOUNT_CODE_INFO_URL}?${params.toString()}`;
68-
69-
const result = await getAuthenticatedHttpClient().get(url);
70-
const { isApplicable, discountPercentage } = camelCaseObject(result).data;
71-
72-
return isApplicable ? +discountPercentage : 0;
73-
}
74-
7549
const CloseText = ({ intl }) => (
7650
<span>
7751
{intl.formatMessage(messages.streakButton)}

src/shared/streak-celebration/utils.jsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { sendTrackEvent } from '@edx/frontend-platform/analytics';
2-
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
2+
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
3+
import {
4+
getAuthenticatedHttpClient,
5+
getAuthenticatedUser,
6+
} from '@edx/frontend-platform/auth';
37

48
import { updateModel } from '../../generic/model-store';
59

@@ -24,4 +28,39 @@ function recordModalClosing(celebrations, org, courseId, dispatch) {
2428
}));
2529
}
2630

27-
export { recordStreakCelebration, recordModalClosing };
31+
async function calculateVoucherDiscountPercentage(voucher, sku, username) {
32+
const urlBase = `${getConfig().ECOMMERCE_BASE_URL}/api/v2/baskets/calculate`;
33+
const url = `${urlBase}/?code=${voucher}&sku=${sku}&username=${username}`;
34+
35+
const result = await getAuthenticatedHttpClient().get(url);
36+
const { totalInclTax, totalInclTaxExclDiscounts } = camelCaseObject(result).data;
37+
38+
if (totalInclTaxExclDiscounts && totalInclTax !== totalInclTaxExclDiscounts) {
39+
// Just store the percent (rather than using these values directly), because ecommerce doesn't give us
40+
// the currency symbol to use, so we want to use the symbol that LMS gives us. And I don't want to assume
41+
// ecommerce's currency is the same as the LMS. So we'll keep using the values in verifiedMode, just
42+
// multiplied by the calculated percentage.
43+
return 1 - totalInclTax / totalInclTaxExclDiscounts;
44+
}
45+
46+
return 0;
47+
}
48+
49+
async function getDiscountCodePercentage(code, courseId) {
50+
const params = new URLSearchParams();
51+
params.append('code', code);
52+
params.append('course_run_key', courseId);
53+
const url = `${getConfig().DISCOUNT_CODE_INFO_URL}?${params.toString()}`;
54+
55+
const result = await getAuthenticatedHttpClient().get(url);
56+
const { isApplicable, discountPercentage } = camelCaseObject(result).data;
57+
58+
return isApplicable ? +discountPercentage : 0;
59+
}
60+
61+
export {
62+
calculateVoucherDiscountPercentage,
63+
getDiscountCodePercentage,
64+
recordModalClosing,
65+
recordStreakCelebration,
66+
};

0 commit comments

Comments
 (0)