Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: onboarding component recommendations on authn #1287

Open
wants to merge 1 commit into
base: 2u-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/data/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ export const VALID_EMAIL_REGEX = '(^[-!#$%&\'*+/=?^_`{}|~0-9A-Z]+(\\.[-!#$%&\'*+

// Query string parameters that can be passed to LMS to manage
// things like auto-enrollment upon login and registration.
export const AUTH_PARAMS = ['course_id', 'enrollment_action', 'course_mode', 'email_opt_in', 'purchase_workflow', 'next', 'register_for_free', 'track', 'is_account_recovery', 'variant', 'host', 'cta'];
export const AUTH_PARAMS = ['course_id', 'enrollment_action', 'course_mode', 'country', 'email_opt_in', 'purchase_workflow', 'next', 'register_for_free', 'track', 'is_account_recovery', 'variant', 'host', 'cta', 'levelOfEducation', 'finishAuthUrl'];
export const REDIRECT = 'redirect';
export const APP_NAME = 'authn_mfe';
36 changes: 27 additions & 9 deletions src/recommendations/RecommendationsPage.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';

import { getConfig } from '@edx/frontend-platform';
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { useIntl } from '@edx/frontend-platform/i18n';
import {
breakpoints,
Expand All @@ -21,18 +22,35 @@ import RecommendationsLargeLayout from './RecommendationsPageLayouts/LargeLayout
import RecommendationsSmallLayout from './RecommendationsPageLayouts/SmallLayout';
import { LINK_TIMEOUT, trackRecommendationsViewed, trackSkipButtonClicked } from './track';
import { DEFAULT_REDIRECT_URL } from '../data/constants';
import { getAllPossibleQueryParams } from '../data/utils';

const RecommendationsPage = () => {
const { formatMessage } = useIntl();
const DASHBOARD_URL = getConfig().LMS_BASE_URL.concat(DEFAULT_REDIRECT_URL);
const isExtraSmall = useMediaQuery({ maxWidth: breakpoints.extraSmall.maxWidth - 1 });
const location = useLocation();
const queryParams = getAllPossibleQueryParams();
// flag to show recommendations for onboarding component experience
const showRecommendations = !!queryParams?.finalRedirectUrl && !!queryParams?.country;
const backendCountryCode = useSelector((state) => state.register.backendCountryCode);

const registrationResponse = location.state?.registrationResult;
const DASHBOARD_URL = getConfig().LMS_BASE_URL.concat(DEFAULT_REDIRECT_URL);
const educationLevel = EDUCATION_LEVEL_MAPPING[location.state?.educationLevel];
const userId = location.state?.userId;
const [redirectUrl, setRedirectUrl] = useState(location.state?.registrationResult?.redirectUrl);
const [educationLevel, setEducationLevel] = useState(EDUCATION_LEVEL_MAPPING[location.state?.educationLevel]);
const [userId, setUserId] = useState(location.state?.userId || null);
const [userCountry, setUserCountry] = useState(backendCountryCode);

useEffect(() => {
if (showRecommendations) {
const authenticatedUser = getAuthenticatedUser();
if (authenticatedUser) {
setRedirectUrl(queryParams.finalRedirectUrl);
setEducationLevel(EDUCATION_LEVEL_MAPPING[queryParams?.levelOfEducation]);
setUserCountry(queryParams.country);
setUserId(authenticatedUser?.userId);
}
}
}, [showRecommendations, queryParams]);

const userCountry = useSelector((state) => state.register.backendCountryCode);
const {
recommendations: algoliaRecommendations,
isLoading,
Expand All @@ -46,8 +64,8 @@ const RecommendationsPage = () => {

const handleSkipRecommendationPage = () => {
window.history.replaceState(location.state, null, '');
if (registrationResponse) {
window.location.href = registrationResponse.redirectUrl;
if (redirectUrl) {
window.location.href = redirectUrl;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something to discuss before merging:

I think it's a security risk to redirect to redirectUrl coming in query params without verifying if its safe or not.

window.location.href = DASHBOARD_URL;
}
Expand All @@ -59,7 +77,7 @@ const RecommendationsPage = () => {
setTimeout(() => { handleSkipRecommendationPage(); }, LINK_TIMEOUT);
};

if (!registrationResponse) {
if (!redirectUrl && !showRecommendations) {
window.location.href = DASHBOARD_URL;
syedsajjadkazmii marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
Expand Down
Loading