From efaf4874616c2d51ce0231df52c00e92aabd8b20 Mon Sep 17 00:00:00 2001 From: James Kachel Date: Thu, 4 Jan 2024 14:02:21 -0600 Subject: [PATCH] When resolving the current run, make sure it's also enrollable (#2046) --- .../components/CourseProductDetailEnroll.js | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/frontend/public/src/components/CourseProductDetailEnroll.js b/frontend/public/src/components/CourseProductDetailEnroll.js index 6b30494e2..36261668c 100644 --- a/frontend/public/src/components/CourseProductDetailEnroll.js +++ b/frontend/public/src/components/CourseProductDetailEnroll.js @@ -83,11 +83,44 @@ export class CourseProductDetailEnroll extends React.Component< destinationUrl: "" } + resolveFirstEnrollableRun() { + const { courseRuns } = this.props + + const enrollableRun = + courseRuns && + courseRuns + .sort( + (a: EnrollmentFlaggedCourseRun, b: EnrollmentFlaggedCourseRun) => { + if ( + moment(a.enrollment_start).isBefore(moment(b.enrollment_start)) + ) { + return -1 + } else if ( + moment(a.enrollment_start).isAfter(moment(b.enrollment_start)) + ) { + return 1 + } else { + return 0 + } + } + ) + .find((run: EnrollmentFlaggedCourseRun) => { + return ( + (run.enrollment_start === null || + moment(run.enrollment_start).isBefore(moment.now())) && + (run.enrollment_end === null || + moment(run.enrollment_end).isAfter(moment.now())) + ) + }) + + return enrollableRun || (courseRuns && courseRuns[0]) + } + resolveCurrentRun() { const { courseRuns } = this.props return !this.getCurrentCourseRun() && courseRuns - ? courseRuns[0] + ? this.resolveFirstEnrollableRun() : this.getCurrentCourseRun() } @@ -313,8 +346,12 @@ export class CourseProductDetailEnroll extends React.Component< const run = this.resolveCurrentRun() const course = - courses && courses.find((elem: any) => elem.id === run.course.id) + courses && + courses.find( + (elem: any) => run && run.course && elem.id === run.course.id + ) const needFinancialAssistanceLink = + run && isFinancialAssistanceAvailable(run) && !run.approved_flexible_price_exists ? (

@@ -328,14 +365,14 @@ export class CourseProductDetailEnroll extends React.Component<

) : null const { upgradeEnrollmentDialogVisibility } = this.state - const product = run.products ? run.products[0] : null + const product = run && run.products ? run.products[0] : null const upgradableCourseRuns = courseRuns ? courseRuns.filter( (run: EnrollmentFlaggedCourseRun) => run.is_upgradable ) : [] - return product ? ( + return run && product ? ( showNewDesign ? (