Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update course about sidebar to take into account usd fixed price #1199

Merged
merged 8 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/components/app/data/hooks/useCourseRedemptionEligibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@
import useEnterpriseCustomer from './useEnterpriseCustomer';
import useLateEnrollmentBufferDays from './useLateEnrollmentBufferDays';

const getContentListPrice = ({ courseRuns }) => {
brobro10000 marked this conversation as resolved.
Show resolved Hide resolved
const flatContentPrice = courseRuns.flatMap(run => run.listPrice?.usd).filter(x => !!x);
// Find the max and min prices
if (!flatContentPrice.length) {
return null;

Check warning on line 13 in src/components/app/data/hooks/useCourseRedemptionEligibility.js

View check run for this annotation

Codecov / codecov/patch

src/components/app/data/hooks/useCourseRedemptionEligibility.js#L13

Added line #L13 was not covered by tests
brobro10000 marked this conversation as resolved.
Show resolved Hide resolved
}
const maxPrice = Math.max(...flatContentPrice);
const minPrice = Math.min(...flatContentPrice);
// Heuristic for displaying the price as a range or a singular price based on runs
if (maxPrice !== minPrice) {
return [minPrice, maxPrice];

Check warning on line 19 in src/components/app/data/hooks/useCourseRedemptionEligibility.js

View check run for this annotation

Codecov / codecov/patch

src/components/app/data/hooks/useCourseRedemptionEligibility.js#L19

Added line #L19 was not covered by tests
}
return [flatContentPrice[0]];
};

export function transformCourseRedemptionEligibility({
courseMetadata,
canRedeemData,
Expand All @@ -17,7 +32,7 @@
const otherSubsidyAccessPolicy = canRedeemData.find(
r => r.redeemableSubsidyAccessPolicy,
)?.redeemableSubsidyAccessPolicy;
const listPrice = redeemabilityForActiveCourseRun?.listPrice?.usd;
const listPrice = getContentListPrice({ courseRuns: canRedeemData });
const hasSuccessfulRedemption = courseRunKey
? !!canRedeemData.find(r => r.contentKey === courseRunKey)?.hasSuccessfulRedemption
: canRedeemData.some(r => r.hasSuccessfulRedemption);
Expand Down
29 changes: 15 additions & 14 deletions src/components/course/CourseSidebarPrice.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Skeleton } from '@openedx/paragon';
import classNames from 'classnames';
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';

import { numberWithPrecision } from './data/utils';
import {
getContentPriceDisplay,
useCanUserRequestSubsidyForCourse,
useCoursePrice,
useIsCourseAssigned,
useUserSubsidyApplicableToCourse,
} from './data';
import {
ENTERPRISE_OFFER_SUBSIDY_TYPE,
LEARNER_CREDIT_SUBSIDY_TYPE,
LICENSE_SUBSIDY_TYPE,
ENTERPRISE_OFFER_SUBSIDY_TYPE,
useEnterpriseCustomer,
} from '../app/data';
import { sumOfArray } from '../../utils/common';

const CourseSidebarPrice = () => {
const intl = useIntl();
Expand All @@ -23,12 +23,10 @@ const CourseSidebarPrice = () => {
const { isCourseAssigned } = useIsCourseAssigned();
const canRequestSubsidy = useCanUserRequestSubsidyForCourse();
const { userSubsidyApplicableToCourse } = useUserSubsidyApplicableToCourse();

if (!coursePrice) {
return <Skeleton containerTestId="course-price-skeleton" height={24} />;
}

const originalPriceDisplay = numberWithPrecision(coursePrice.list);
const originalPriceDisplay = getContentPriceDisplay(coursePrice.listRange);
const showOrigPrice = !enterpriseCustomer.hideCourseOriginalPrice;
const crossedOutOriginalPrice = (
<del>
Expand All @@ -38,7 +36,8 @@ const CourseSidebarPrice = () => {
defaultMessage="Priced reduced from:"
description="Message to indicate that the price has been reduced."
/>
</span>${originalPriceDisplay} {currency}
</span>
${originalPriceDisplay} {currency}
</del>
);

Expand All @@ -62,13 +61,15 @@ const CourseSidebarPrice = () => {
);
}

const hasDiscountedPrice = coursePrice.discounted < coursePrice.list;

const hasDiscountedPrice = coursePrice.discounted
&& sumOfArray(coursePrice.discounted) < sumOfArray(coursePrice.listRange);
// Case 2: No subsidies found but learner can request a subsidy
if (!hasDiscountedPrice && canRequestSubsidy) {
return (
<span style={{ whiteSpace: 'pre-wrap' }} data-testid="browse-and-request-pricing">
<s>${originalPriceDisplay} {currency}</s><br />
<s>
${originalPriceDisplay} {currency}
</s><br />
brobro10000 marked this conversation as resolved.
Show resolved Hide resolved
<FormattedMessage
id="enterprise.course.about.course.sidebar.price.free.when.approved"
defaultMessage="Free to me{br}(when approved)"
Expand Down Expand Up @@ -113,22 +114,22 @@ const CourseSidebarPrice = () => {
});
}
}
const discountedPriceDisplay = `${numberWithPrecision(coursePrice.discounted)} ${currency}`;

const discountedPriceDisplay = `${getContentPriceDisplay(coursePrice.discounted)} ${currency}`;
return (
<>
<div className={classNames({ 'mb-2': coursePrice.discounted > 0 || showOrigPrice })}>
{/* discounted > 0 means partial discount */}
{showOrigPrice && <>{crossedOutOriginalPrice}{' '}</>}
{coursePrice.discounted > 0 && (
{sumOfArray(coursePrice.discounted) > 0 && (
<>
<span className="sr-only">
<FormattedMessage
id="enterprise.course.about.price.discounted"
defaultMessage="Discounted price:"
description="Message to indicate that the price has been discounted."
/>
</span>${discountedPriceDisplay}
</span>
${discountedPriceDisplay}
</>
)}
</div>
Expand Down
20 changes: 12 additions & 8 deletions src/components/course/data/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import { canUserRequestSubsidyForCourse, getExternalCourseEnrollmentUrl } from '
import { createExecutiveEducationFailureMessage } from '../../executive-education-2u/ExecutiveEducation2UError';
import { SUBSIDY_TYPE } from '../../../constants';
import {
COUPON_CODE_SUBSIDY_TYPE,
determineAllocatedAssignmentsForCourse,
getSubsidyToApplyForCourse,
LICENSE_SUBSIDY_TYPE,
useBrowseAndRequest,
useBrowseAndRequestConfiguration,
useCatalogsForSubsidyRequests,
Expand All @@ -48,9 +51,6 @@ import {
useEnterpriseOffers,
useRedeemablePolicies,
useSubscriptions,
COUPON_CODE_SUBSIDY_TYPE,
LICENSE_SUBSIDY_TYPE,
determineAllocatedAssignmentsForCourse,
} from '../../app/data';
import { LICENSE_STATUS } from '../../enterprise-user-subsidy/data/constants';
import { CourseContext } from '../CourseContextProvider';
Expand Down Expand Up @@ -203,19 +203,23 @@ export const useCoursePriceForUserSubsidy = ({
}

const onlyListPrice = {
list: listPrice,
listRange: listPrice,
};

if (userSubsidyApplicableToCourse) {
const { discountType, discountValue } = userSubsidyApplicableToCourse;
let discountedPrice;
let discountedPrice = [];

if (discountType && discountType.toLowerCase() === SUBSIDY_DISCOUNT_TYPE_MAP.PERCENTAGE.toLowerCase()) {
discountedPrice = listPrice - (listPrice * (discountValue / 100));
discountedPrice = onlyListPrice.listRange.map(
brobro10000 marked this conversation as resolved.
Show resolved Hide resolved
(individualPrice) => individualPrice - (individualPrice * (discountValue / 100)),
);
}

if (discountType && discountType.toLowerCase() === SUBSIDY_DISCOUNT_TYPE_MAP.ABSOLUTE.toLowerCase()) {
discountedPrice = Math.max(listPrice - discountValue, 0);
discountedPrice = onlyListPrice.listRange.map(
(individualPrice) => Math.max(individualPrice - discountValue, 0),
);
}

if (isDefinedAndNotNull(discountedPrice)) {
Expand All @@ -226,7 +230,7 @@ export const useCoursePriceForUserSubsidy = ({
}
return {
...onlyListPrice,
discounted: onlyListPrice.list,
discounted: onlyListPrice.listRange,
brobro10000 marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand Down
Loading
Loading