diff --git a/src/components/course/routes/CoursePageRoutes.jsx b/src/components/course/routes/CoursePageRoutes.jsx index 454b4e5e11..40538ce39e 100644 --- a/src/components/course/routes/CoursePageRoutes.jsx +++ b/src/components/course/routes/CoursePageRoutes.jsx @@ -1,10 +1,9 @@ import React from 'react'; import { Switch, useRouteMatch } from 'react-router-dom'; import { PageRoute } from '@edx/frontend-platform/react'; - -import CourseAbout from './CourseAbout'; import ExternalCourseEnrollment from './ExternalCourseEnrollment'; import ExternalCourseEnrollmentConfirmation from './ExternalCourseEnrollmentConfirmation'; +import CourseAbout from './CourseAbout'; import NotFoundPage from '../../NotFoundPage'; const CoursePageRoutes = () => { diff --git a/src/components/course/routes/ExternalCourseEnrollment.jsx b/src/components/course/routes/ExternalCourseEnrollment.jsx index 23887ff6c5..5f54cb605d 100644 --- a/src/components/course/routes/ExternalCourseEnrollment.jsx +++ b/src/components/course/routes/ExternalCourseEnrollment.jsx @@ -1,5 +1,5 @@ import React, { useContext, useEffect, useRef } from 'react'; -import { useHistory } from 'react-router-dom'; +import { generatePath, useHistory, useRouteMatch } from 'react-router-dom'; import { Alert, Button, Col, Container, Hyperlink, Row, } from '@edx/paragon'; @@ -20,6 +20,7 @@ import { features } from '../../../config'; const ExternalCourseEnrollment = () => { const config = getConfig(); const history = useHistory(); + const routeMatch = useRouteMatch(); const { state: { activeCourseRun, @@ -31,9 +32,13 @@ const ExternalCourseEnrollment = () => { externalCourseFormSubmissionError, } = useContext(CourseContext); const { - enterpriseConfig: { authOrgId }, + enterpriseConfig: { authOrgId, slug }, } = useContext(AppContext); const { redeemableLearnerCreditPolicies } = useContext(UserSubsidyContext); + const completeEnrollmentUrl = generatePath( + `${routeMatch.path}/complete`, + { enterpriseSlug: slug, courseType: course.courseType, courseKey: course.key }, + ); const isCourseAssigned = useIsCourseAssigned(redeemableLearnerCreditPolicies?.learnerContentAssignments, course?.key); const courseMetadata = useMinimalCourseMetadata(); @@ -62,15 +67,14 @@ const ExternalCourseEnrollment = () => { } }, [externalCourseFormSubmissionError, containerRef]); - const handleCheckoutSuccess = () => { - history.push('enroll/complete'); - }; - useEffect(() => { + // Once a redemption has successfully completed and the can-redeem query has been invalidated or + // a user attempts to navigate directly to :slug/:courseType/course/:courseKey/enroll, + // it will run this conditional and perform the redirect if (hasSuccessfulRedemption) { - history.push('enroll/complete'); + history.push(completeEnrollmentUrl); } - }, [hasSuccessfulRedemption, history]); + }, [completeEnrollmentUrl, course.key, hasSuccessfulRedemption, history, routeMatch.path, slug]); return (