Skip to content

Commit

Permalink
feat: adds price range for lc variant based assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Sep 16, 2024
1 parent 13e9d7f commit f75e9d1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
6 changes: 6 additions & 0 deletions __mocks__/react-instantsearch-dom.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const fakeHits = [
pacing_type: 'self_paced',
weeks_to_complete: 8,
upgrade_deadline: dayjs(mockCurrentStartDate).add(3, 'months').unix(),
content_price: 100,
},
key: 'Bees101',
normalized_metadata: mockNormalizedData,
Expand All @@ -52,6 +53,7 @@ const fakeHits = [
pacing_type: 'self_paced',
weeks_to_complete: 8,
upgrade_deadline: dayjs(mockCurrentStartDate).add(3, 'months').unix(),
content_price: 100,
},
{
key: 'course-v1:edX+Bee101+3T2020',
Expand All @@ -64,6 +66,7 @@ const fakeHits = [
pacing_type: 'self_paced',
weeks_to_complete: 8,
upgrade_deadline: dayjs('2020-09-09T04:00:00Z').add(3, 'months').unix(),
content_price: 100,
},
],
},
Expand All @@ -85,6 +88,7 @@ const fakeHits = [
pacing_type: 'self_paced',
weeks_to_complete: 8,
upgrade_deadline: dayjs('2022-10-09T04:00:00Z').add(3, 'months').unix(),
content_price: 100,
},
key: 'Wasps200',
normalized_metadata: mockNormalizedData,
Expand All @@ -100,6 +104,7 @@ const fakeHits = [
pacing_type: 'self_paced',
weeks_to_complete: 8,
upgrade_deadline: dayjs('2022-10-09T04:00:00Z').add(3, 'months').unix(),
content_price: 100,
},
{
key: 'course-v1:edX+Wasps200+3T2020',
Expand All @@ -112,6 +117,7 @@ const fakeHits = [
pacing_type: 'self_paced',
weeks_to_complete: 8,
upgrade_deadline: dayjs('2020-09-09T04:00:00Z').add(3, 'months').unix(),
content_price: 100,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import {
useMediaQuery,
breakpoints,
Card,
Stack,
Badge,
Badge, breakpoints, Card, Stack, useMediaQuery,
} from '@openedx/paragon';
import { camelCaseObject } from '@edx/frontend-platform/utils';

import { FormattedMessage } from '@edx/frontend-platform/i18n';
import { useCourseCardMetadata } from './data';
import AssignmentModalImportantDates from '../assignment-modal/AssignmentModalmportantDates';
import { formatPrice } from '../data';

const BaseCourseCard = ({
original,
Expand All @@ -23,7 +20,6 @@ const BaseCourseCard = ({
}) => {
const isSmall = useMediaQuery({ maxWidth: breakpoints.small.maxWidth });
const isExtraSmall = useMediaQuery({ maxWidth: breakpoints.extraSmall.maxWidth });

const courseCardMetadata = useCourseCardMetadata({
course: camelCaseObject(original),
courseRun,
Expand Down Expand Up @@ -54,7 +50,7 @@ const BaseCourseCard = ({
subtitle={subtitle}
actions={(
<Stack gap={1} className="text-right">
<div className="h4 mt-2.5 mb-0">{formattedPrice}</div>
<div className="h4 mt-2.5 mb-0">{courseRun ? formatPrice(courseRun.contentPrice) : formattedPrice}</div>
<span className="micro">
<FormattedMessage
id="lcm.budget.detail.page.catalog.tab.course.card.price.per.learner"
Expand Down Expand Up @@ -124,6 +120,7 @@ BaseCourseCard.propTypes = {
}).isRequired,
courseRun: PropTypes.shape({
enrollBy: PropTypes.string,
contentPrice: PropTypes.number,
start: PropTypes.string,
}),
footerActions: PropTypes.elementType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import cardFallbackImg from '@edx/brand/paragon/images/card-imagecap-fallback.pn

import { defineMessages, useIntl } from '@edx/frontend-platform/i18n';
import {
CARD_TEXT,
EXEC_ED_COURSE_TYPE,
formatDate,
formatPrice,
getAssignableCourseRuns,
getEnrollmentDeadline,
Expand All @@ -15,8 +13,6 @@ import {
} from '../../data';
import { pluralText } from '../../../../utils';

const { ENROLLMENT } = CARD_TEXT;

const messages = defineMessages({
courseFooterMessage: {
id: 'lcm.budget.detail.page.catalog.tab.course.card.footer-text',
Expand All @@ -25,6 +21,21 @@ const messages = defineMessages({
},
});

const getContentPriceDisplay = ({ courseRuns }) => {
const flatContentPrice = courseRuns.flatMap(run => run?.contentPrice);
// Find the max and min prices
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 (flatContentPrice.length > 1 && maxPrice !== minPrice) {
return `${formatPrice(minPrice)} - ${formatPrice(maxPrice)}`;

Check warning on line 31 in src/components/learner-credit-management/cards/data/useCourseCardMetadata.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/learner-credit-management/cards/data/useCourseCardMetadata.jsx#L31

Added line #L31 was not covered by tests
}
if (flatContentPrice.length === 1) {
return formatPrice(flatContentPrice[0]);
}
return 'N/A';
};

const useCourseCardMetadata = ({
course,
enterpriseSlug,
Expand All @@ -34,7 +45,6 @@ const useCourseCardMetadata = ({
const { subsidyAccessPolicyId } = useBudgetId();
const { data: subsidyAccessPolicy } = useSubsidyAccessPolicy(subsidyAccessPolicyId);
const {
availability,
cardImageUrl,
courseType,
key,
Expand All @@ -43,7 +53,15 @@ const useCourseCardMetadata = ({
title,
courseRuns,
} = course;
const formattedPrice = (normalizedMetadata.contentPrice || normalizedMetadata.contentPrice === 0) ? formatPrice(normalizedMetadata.contentPrice) : 'N/A';
const assignableCourseRuns = getAssignableCourseRuns({
courseRuns,
subsidyExpirationDatetime: subsidyAccessPolicy.subsidyExpirationDatetime,
isLateRedemptionAllowed: subsidyAccessPolicy.isLateRedemptionAllowed,
});

// Extracts the content price from assignable course runs
const formattedPrice = getContentPriceDisplay({ courseRuns: assignableCourseRuns });

const imageSrc = cardImageUrl || cardFallbackImg;

let logoSrc;
Expand All @@ -54,28 +72,15 @@ const useCourseCardMetadata = ({
}

const altText = `${title} course image`;
const formattedAvailability = availability?.length ? availability.join(', ') : null;
const enrollmentDeadline = getEnrollmentDeadline(normalizedMetadata.enrollByDate);

let courseEnrollmentInfo = '';
if (formattedAvailability) {
courseEnrollmentInfo = `${formattedAvailability} • `;
}
courseEnrollmentInfo += `${ENROLLMENT.text} ${enrollmentDeadline}`;
const execEdEnrollmentInfo = `Starts ${formatDate(normalizedMetadata.startDate)}${ENROLLMENT.text} ${enrollmentDeadline}`;

const isExecEdCourseType = courseType === EXEC_ED_COURSE_TYPE;

let linkToCourse = `${ENTERPRISE_LEARNER_PORTAL_URL}/${enterpriseSlug}/course/${key}`;
if (isExecEdCourseType) {
linkToCourse = `${ENTERPRISE_LEARNER_PORTAL_URL}/${enterpriseSlug}/executive-education-2u/course/${key}`;
}

const assignableCourseRuns = getAssignableCourseRuns({
courseRuns,
subsidyExpirationDatetime: subsidyAccessPolicy.subsidyExpirationDatetime,
isLateRedemptionAllowed: subsidyAccessPolicy.isLateRedemptionAllowed,
});
const footerText = intl.formatMessage(messages.courseFooterMessage, {
courseRuns: assignableCourseRuns.length,
pluralText: pluralText('date', assignableCourseRuns.length),
Expand All @@ -90,8 +95,6 @@ const useCourseCardMetadata = ({
logoSrc,
logoAlt,
enrollmentDeadline,
courseEnrollmentInfo,
execEdEnrollmentInfo,
linkToCourse,
isExecEdCourseType,
footerText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { renderWithRouter, sendEnterpriseTrackEvent } from '@edx/frontend-enterp
import dayjs from 'dayjs';
import CourseCard from '../CourseCard';
import {
formatPrice,
DATETIME_FORMAT,
formatPrice,
getNormalizedEnrollByDate,
learnerCreditManagementQueryKeys,
SHORT_MONTH_DATE_FORMAT,
useBudgetId,
useSubsidyAccessPolicy,
getNormalizedEnrollByDate,
} from '../../data';
import { getButtonElement, queryClient } from '../../../test/testUtils';

Expand Down Expand Up @@ -79,6 +79,7 @@ const originalData = {
is_active: true,
weeks_to_complete: 60,
end: dayjs().add(1, 'years').toISOString(),
content_price: '100',
},
],
advertised_course_run: {
Expand All @@ -90,6 +91,7 @@ const originalData = {
is_active: true,
weeks_to_complete: 60,
end: dayjs().add(1, 'year').toISOString(),
content_price: '100',
},
};
const imageAltText = `${originalData.title} course image`;
Expand Down Expand Up @@ -121,6 +123,7 @@ const execEdData = {
is_active: true,
weeks_to_complete: 60,
end: dayjs().add(1, 'year').toISOString(),
content_price: 999,
},
],
advertised_course_run: {
Expand All @@ -132,6 +135,7 @@ const execEdData = {
is_active: true,
weeks_to_complete: 60,
end: dayjs().add(1, 'year').toISOString(),
content_price: 999,
},
original_image_url: '',
partners: [{ logo_image_url: '', name: 'Course Provider' }],
Expand Down
8 changes: 0 additions & 8 deletions webpack.dev.config.js

This file was deleted.

0 comments on commit f75e9d1

Please sign in to comment.