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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 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,23 @@
import useEnterpriseCustomer from './useEnterpriseCustomer';
import useLateEnrollmentBufferDays from './useLateEnrollmentBufferDays';

const getContentListPrice = ({ courseRuns }) => {
console.log(courseRuns);

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

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
const flatContentPrice = courseRuns.flatMap(run => run.listPrice?.usd).filter(x => !!x);
console.log(flatContentPrice);

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

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
// Find the max and min prices
if (!flatContentPrice.length) {
return null;
}
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];
}
return [flatContentPrice[0]];
};

export function transformCourseRedemptionEligibility({
courseMetadata,
canRedeemData,
Expand All @@ -17,7 +34,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: 20 additions & 9 deletions src/components/course/CourseSidebarPrice.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@
useCoursePrice,
useIsCourseAssigned,
useUserSubsidyApplicableToCourse,
ZERO_PRICE,
} 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 getContentPriceDisplay = (priceRange) => {
if (!priceRange?.length) {
return numberWithPrecision(ZERO_PRICE);
}
const minPrice = Math.min(...priceRange);
const maxPrice = Math.max(...priceRange);
if (maxPrice !== minPrice) {
return `${numberWithPrecision(minPrice)} - ${numberWithPrecision(maxPrice)}`;
}
return numberWithPrecision(priceRange.sort((a, b) => a - b)[0]);
};

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

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

const originalPriceDisplay = numberWithPrecision(coursePrice.list);
console.log(coursePrice);

Check warning on line 43 in src/components/course/CourseSidebarPrice.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
const originalPriceDisplay = getContentPriceDisplay(coursePrice.listRange);
const showOrigPrice = !enterpriseCustomer.hideCourseOriginalPrice;
const crossedOutOriginalPrice = (
<del>
Expand Down Expand Up @@ -62,8 +75,7 @@
);
}

const hasDiscountedPrice = coursePrice.discounted < coursePrice.list;

const hasDiscountedPrice = coursePrice.discounted && sumOfArray(coursePrice.discounted) < sumOfArray(coursePrice.listRange);

Check failure on line 78 in src/components/course/CourseSidebarPrice.jsx

View workflow job for this annotation

GitHub Actions / tests

This line has a length of 126. Maximum allowed is 120
// Case 2: No subsidies found but learner can request a subsidy
if (!hasDiscountedPrice && canRequestSubsidy) {
return (
Expand Down Expand Up @@ -113,14 +125,13 @@
});
}
}
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
Expand Down
22 changes: 14 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 { 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 @@
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 @@ -201,21 +201,26 @@
if (!listPrice) {
return null;
}
console.log(listPrice, userSubsidyApplicableToCourse);

Check warning on line 204 in src/components/course/data/hooks.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement

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(
(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 +231,7 @@
}
return {
...onlyListPrice,
discounted: onlyListPrice.list,
discounted: onlyListPrice.listRange,
};
}

Expand Down Expand Up @@ -631,6 +636,7 @@
export function useCoursePrice() {
const { data: courseListPrice } = useCourseListPrice();
const { userSubsidyApplicableToCourse } = useUserSubsidyApplicableToCourse();
console.log(courseListPrice, userSubsidyApplicableToCourse);

Check warning on line 639 in src/components/course/data/hooks.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
return useCoursePriceForUserSubsidy({
userSubsidyApplicableToCourse,
listPrice: courseListPrice,
Expand Down
18 changes: 16 additions & 2 deletions src/components/course/data/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@

export const numberWithPrecision = (number, precision = 2) => number.toFixed(precision);

export const formatPrice = (price, options = {}) => {
const USDollar = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
...options,
});
return USDollar.format(Math.abs(price));
};

/**
*
* @param couponCodes
Expand Down Expand Up @@ -817,10 +827,14 @@
* @returns Price for the course run.
*/
export function getCoursePrice(course) {
console.log(course);

Check warning on line 830 in src/components/course/data/utils.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
if (course.activeCourseRun?.fixedPriceUsd) {
return [course.activeCourseRun?.fixedPriceUsd];
}
if (course.activeCourseRun?.firstEnrollablePaidSeatPrice) {
return course.activeCourseRun.firstEnrollablePaidSeatPrice;
return [course.activeCourseRun?.firstEnrollablePaidSeatPrice];
}
return getEntitlementPrice(course.entitlements);
return [course.entitlements];
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/components/dashboard/SubscriptionExpirationModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
} = useContext(AppContext);

const intl = useIntl();
const [isOpen, , close] = useToggle(true);
const { data: enterpriseCustomer } = useEnterpriseCustomer();
const { data: subscriptions } = useSubscriptions();
const { subscriptionPlan, subscriptionLicense } = subscriptions;
const seenExpiredSubscriptionModal = !!global.localStorage.getItem(
EXPIRED_SUBSCRIPTION_MODAL_LOCALSTORAGE_KEY(subscriptionLicense),
);
console.log(seenExpiredSubscriptionModal);

Check warning on line 33 in src/components/dashboard/SubscriptionExpirationModal.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
const [isOpen, , close] = useToggle(!seenExpiredSubscriptionModal);
const { data: enterpriseCustomer } = useEnterpriseCustomer();
const {
daysUntilExpirationIncludingRenewals,
expirationDate,
Expand Down Expand Up @@ -88,10 +92,6 @@
close();
global.localStorage.setItem(EXPIRED_SUBSCRIPTION_MODAL_LOCALSTORAGE_KEY(subscriptionLicense), 'true');
};

const seenExpiredSubscriptionModal = !!global.localStorage.getItem(
EXPIRED_SUBSCRIPTION_MODAL_LOCALSTORAGE_KEY(subscriptionLicense),
);
// If the subscription has already expired, we show a different un-dismissible modal
if (!isCurrent) {
if (seenExpiredSubscriptionModal) {
Expand All @@ -109,6 +109,7 @@
</ActionRow>
)}
hasCloseButton
onClose={handleSubscriptionExpiredModalDismissal}
>
<p>
Your organization&#39;s access to your subscription has expired. You will only have audit
Expand Down Expand Up @@ -170,6 +171,7 @@
</ActionRow>
)}
hasCloseButton
onClose={handleSubscriptionExpiringModalDismissal}
>
<p>
Your organization&#39;s access to your current subscription is expiring in
Expand Down
5 changes: 5 additions & 0 deletions src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export const isNull = (value) => {
};

export const isDefinedAndNotNull = (value) => {
if (Array.isArray(value)) {
return value.every(item => isDefined(item) && !isNull(item));
}
const values = createArrayFromValue(value);
return values.every(item => isDefined(item) && !isNull(item));
};
Expand All @@ -39,6 +42,8 @@ export const hasTruthyValue = (value) => {
return values.every(item => !!item);
};

export const sumOfArray = (values) => values.reduce((prev, next) => prev + next, 0);

export const hasValidStartExpirationDates = ({ startDate, expirationDate, endDate }) => {
const now = dayjs();
// Subscriptions use "expirationDate" while Codes use "endDate"
Expand Down
Loading