1- import React , { useEffect } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import { useSelector } from 'react-redux' ;
33
44import { getConfig } from '@edx/frontend-platform' ;
5+ import { getAuthenticatedUser } from '@edx/frontend-platform/auth' ;
56import { useIntl } from '@edx/frontend-platform/i18n' ;
67import {
78 breakpoints ,
@@ -21,18 +22,35 @@ import RecommendationsLargeLayout from './RecommendationsPageLayouts/LargeLayout
2122import RecommendationsSmallLayout from './RecommendationsPageLayouts/SmallLayout' ;
2223import { LINK_TIMEOUT , trackRecommendationsViewed , trackSkipButtonClicked } from './track' ;
2324import { DEFAULT_REDIRECT_URL } from '../data/constants' ;
25+ import { getAllPossibleQueryParams } from '../data/utils' ;
2426
2527const RecommendationsPage = ( ) => {
2628 const { formatMessage } = useIntl ( ) ;
29+ const DASHBOARD_URL = getConfig ( ) . LMS_BASE_URL . concat ( DEFAULT_REDIRECT_URL ) ;
2730 const isExtraSmall = useMediaQuery ( { maxWidth : breakpoints . extraSmall . maxWidth - 1 } ) ;
2831 const location = useLocation ( ) ;
32+ const queryParams = getAllPossibleQueryParams ( ) ;
33+ // flag to show recommendations for onboarding component experience
34+ const showRecommendations = ! ! queryParams ?. levelOfEducation || ( ! ! queryParams ?. finalRedirectUrl && ! ! queryParams ?. country ) ;
35+ const backendCountryCode = useSelector ( ( state ) => state . register . backendCountryCode ) ;
2936
30- const registrationResponse = location . state ?. registrationResult ;
31- const DASHBOARD_URL = getConfig ( ) . LMS_BASE_URL . concat ( DEFAULT_REDIRECT_URL ) ;
32- const educationLevel = EDUCATION_LEVEL_MAPPING [ location . state ?. educationLevel ] ;
33- const userId = location . state ?. userId ;
37+ const [ redirectUrl , setRedirectUrl ] = useState ( location . state ?. registrationResult ?. redirectUrl ) ;
38+ const [ educationLevel , setEducationLevel ] = useState ( EDUCATION_LEVEL_MAPPING [ location . state ?. educationLevel ] ) ;
39+ const [ userId , setUserId ] = useState ( location . state ?. userId || - 1 ) ;
40+ const [ userCountry , setUserCountry ] = useState ( backendCountryCode ) ;
41+
42+ useEffect ( ( ) => {
43+ if ( showRecommendations ) {
44+ const authenticatedUser = getAuthenticatedUser ( ) ;
45+ if ( authenticatedUser ) {
46+ setRedirectUrl ( queryParams . finalRedirectUrl ) ;
47+ setEducationLevel ( EDUCATION_LEVEL_MAPPING [ queryParams . levelOfEducation ] ) ;
48+ setUserCountry ( queryParams . country ) ;
49+ setUserId ( authenticatedUser ?. userId ) ;
50+ }
51+ }
52+ } , [ showRecommendations , queryParams ] ) ;
3453
35- const userCountry = useSelector ( ( state ) => state . register . backendCountryCode ) ;
3654 const {
3755 recommendations : algoliaRecommendations ,
3856 isLoading,
@@ -46,8 +64,8 @@ const RecommendationsPage = () => {
4664
4765 const handleSkipRecommendationPage = ( ) => {
4866 window . history . replaceState ( location . state , null , '' ) ;
49- if ( registrationResponse ) {
50- window . location . href = registrationResponse . redirectUrl ;
67+ if ( redirectUrl ) {
68+ window . location . href = redirectUrl ;
5169 } else {
5270 window . location . href = DASHBOARD_URL ;
5371 }
@@ -59,7 +77,7 @@ const RecommendationsPage = () => {
5977 setTimeout ( ( ) => { handleSkipRecommendationPage ( ) ; } , LINK_TIMEOUT ) ;
6078 } ;
6179
62- if ( ! registrationResponse ) {
80+ if ( ! redirectUrl && ! showRecommendations ) {
6381 window . location . href = DASHBOARD_URL ;
6482 return null ;
6583 }
0 commit comments