1
- import React , { useEffect } from 'react' ;
1
+ import React , { useEffect , useState } from 'react' ;
2
2
import { useSelector } from 'react-redux' ;
3
3
4
4
import { getConfig } from '@edx/frontend-platform' ;
5
+ import { getAuthenticatedUser } from '@edx/frontend-platform/auth' ;
5
6
import { useIntl } from '@edx/frontend-platform/i18n' ;
6
7
import {
7
8
breakpoints ,
@@ -21,18 +22,36 @@ import RecommendationsLargeLayout from './RecommendationsPageLayouts/LargeLayout
21
22
import RecommendationsSmallLayout from './RecommendationsPageLayouts/SmallLayout' ;
22
23
import { LINK_TIMEOUT , trackRecommendationsViewed , trackSkipButtonClicked } from './track' ;
23
24
import { DEFAULT_REDIRECT_URL } from '../data/constants' ;
25
+ import { getAllPossibleQueryParams } from '../data/utils' ;
24
26
25
27
const RecommendationsPage = ( ) => {
26
28
const { formatMessage } = useIntl ( ) ;
29
+ const DASHBOARD_URL = getConfig ( ) . LMS_BASE_URL . concat ( DEFAULT_REDIRECT_URL ) ;
27
30
const isExtraSmall = useMediaQuery ( { maxWidth : breakpoints . extraSmall . maxWidth - 1 } ) ;
28
31
const location = useLocation ( ) ;
32
+ const queryParams = getAllPossibleQueryParams ( ) ;
33
+ // flag to show recommendations for onboarding component experience
34
+ const showRecommendations = ! ! queryParams ?. levelOfEducation
35
+ || ( ! ! queryParams ?. finalRedirectUrl && ! ! queryParams ?. country ) ;
36
+ const backendCountryCode = useSelector ( ( state ) => state . register . backendCountryCode ) ;
29
37
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 ;
38
+ const [ redirectUrl , setRedirectUrl ] = useState ( location . state ?. registrationResult ?. redirectUrl ) ;
39
+ const [ educationLevel , setEducationLevel ] = useState ( EDUCATION_LEVEL_MAPPING [ location . state ?. educationLevel ] ) ;
40
+ const [ userId , setUserId ] = useState ( location . state ?. userId || - 1 ) ;
41
+ const [ userCountry , setUserCountry ] = useState ( backendCountryCode ) ;
42
+
43
+ useEffect ( ( ) => {
44
+ if ( showRecommendations ) {
45
+ const authenticatedUser = getAuthenticatedUser ( ) ;
46
+ if ( authenticatedUser ) {
47
+ setRedirectUrl ( queryParams . finalRedirectUrl ) ;
48
+ setEducationLevel ( EDUCATION_LEVEL_MAPPING [ queryParams . levelOfEducation ] ) ;
49
+ setUserCountry ( queryParams . country ) ;
50
+ setUserId ( authenticatedUser ?. userId ) ;
51
+ }
52
+ }
53
+ } , [ showRecommendations , queryParams ] ) ;
34
54
35
- const userCountry = useSelector ( ( state ) => state . register . backendCountryCode ) ;
36
55
const {
37
56
recommendations : algoliaRecommendations ,
38
57
isLoading,
@@ -46,8 +65,8 @@ const RecommendationsPage = () => {
46
65
47
66
const handleSkipRecommendationPage = ( ) => {
48
67
window . history . replaceState ( location . state , null , '' ) ;
49
- if ( registrationResponse ) {
50
- window . location . href = registrationResponse . redirectUrl ;
68
+ if ( redirectUrl ) {
69
+ window . location . href = redirectUrl ;
51
70
} else {
52
71
window . location . href = DASHBOARD_URL ;
53
72
}
@@ -59,7 +78,7 @@ const RecommendationsPage = () => {
59
78
setTimeout ( ( ) => { handleSkipRecommendationPage ( ) ; } , LINK_TIMEOUT ) ;
60
79
} ;
61
80
62
- if ( ! registrationResponse ) {
81
+ if ( ! redirectUrl && ! showRecommendations ) {
63
82
window . location . href = DASHBOARD_URL ;
64
83
return null ;
65
84
}
0 commit comments