-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
171 lines (152 loc) · 5.27 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, { useEffect, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { AppProvider } from '@edx/frontend-platform/react';
import { Spinner } from '@openedx/paragon';
import PropTypes from 'prop-types';
import { getThirdPartyAuthContext, setCurrentOpenedForm } from './data/reducers';
import validateContextData from './data/utils';
import BaseContainer from '../base-container';
import store from '../data/configureStore';
import {
ENTERPRISE_LOGIN,
FORGOT_PASSWORD_FORM,
LOGIN_FORM,
PENDING_STATE,
PROGRESSIVE_PROFILING_FORM,
REGISTRATION_FORM,
VALID_FORMS,
} from '../data/constants';
import getAllPossibleQueryParams from '../data/utils';
import { ForgotPasswordForm, LoginForm, RegistrationForm } from '../forms';
import EnterpriseSSO from '../forms/enterprise-sso-popup';
import { getTpaHint, getTpaProvider } from '../forms/enterprise-sso-popup/data/utils';
import ProgressiveProfilingForm from '../forms/progressive-profiling-popup';
/**
* Main component that conditionally renders a login or registration form inside a modal window.
*
* @param {boolean} isOpen - Required. Whether the modal window is open.
* @param {function} close - Required. Function to close the modal window.
* @param {string} formToRender - Optional. Indicates which form to render ('login' or 'register').
* @param {Object} context - Optional. Additional context needed for authentication, such as enrollment data.
*
* @returns {JSX.Element} The rendered component containing the login or registration form.
*/
export const AuthnComponent = ({
isOpen, close, context, formToRender,
}) => {
const dispatch = useDispatch();
const queryParams = useMemo(() => getAllPossibleQueryParams(), []);
const currentForm = useSelector(state => state.commonData.currentForm);
const providers = useSelector(state => state.commonData.thirdPartyAuthContext?.providers);
const secondaryProviders = useSelector(state => state.commonData.thirdPartyAuthContext?.secondaryProviders);
const thirdPartyAuthApiStatus = useSelector(state => state.commonData.thirdPartyAuthApiStatus);
const tpaHint = getTpaHint();
const { provider: tpaProvider } = getTpaProvider(tpaHint, providers, secondaryProviders);
const pendingState = queryParams?.tpa_hint && thirdPartyAuthApiStatus === PENDING_STATE;
useEffect(() => {
if (tpaProvider) {
dispatch(setCurrentOpenedForm(ENTERPRISE_LOGIN));
}
if (!tpaProvider && formToRender) {
dispatch(setCurrentOpenedForm(formToRender));
}
}, [dispatch, formToRender, tpaProvider, queryParams]);
useEffect(() => {
let validatedContext = {};
if (context) {
validatedContext = validateContextData(context);
}
dispatch(getThirdPartyAuthContext({ ...validatedContext, ...queryParams }));
}, [context, dispatch, queryParams]);
const getForm = () => {
if (currentForm === ENTERPRISE_LOGIN) {
return <EnterpriseSSO provider={tpaProvider} />;
}
if (currentForm === REGISTRATION_FORM) {
return <RegistrationForm />;
}
if (currentForm === LOGIN_FORM) {
return <LoginForm />;
}
if (currentForm === PROGRESSIVE_PROFILING_FORM) {
return <ProgressiveProfilingForm />;
}
if (currentForm === FORGOT_PASSWORD_FORM) {
return <ForgotPasswordForm />;
}
return <RegistrationForm />;
};
const getSpinner = (
<div className="w-100 text-center">
<Spinner animation="border" variant="primary" />
</div>
);
return (
<BaseContainer isOpen={isOpen} close={close}>
{pendingState
? getSpinner
: getForm()}
</BaseContainer>
);
};
AuthnComponent.propTypes = {
isOpen: PropTypes.bool.isRequired,
close: PropTypes.func.isRequired,
context: PropTypes.shape({
course_id: PropTypes.string,
enrollment_action: PropTypes.string,
email_opt_in: PropTypes.bool,
}),
formToRender: PropTypes.oneOf(VALID_FORMS),
};
AuthnComponent.defaultProps = {
context: null,
formToRender: REGISTRATION_FORM,
};
/**
* Higher Order Component that wraps AuthnComponent with AppProvider.
*/
const AuthnComponentWithProvider = (props) => {
const { isOpen } = props;
if (isOpen) {
return (
<AppProvider store={store}>
<AuthnComponent {...props} />
</AppProvider>
);
}
return null;
};
AuthnComponentWithProvider.propTypes = {
isOpen: PropTypes.bool.isRequired,
close: PropTypes.func.isRequired,
context: PropTypes.shape({
course_id: PropTypes.string,
enrollment_action: PropTypes.string,
email_opt_in: PropTypes.bool,
}),
formToRender: PropTypes.oneOf(VALID_FORMS),
};
AuthnComponentWithProvider.defaultProps = {
context: null,
formToRender: REGISTRATION_FORM,
};
/**
* Component that renders a sign-in form using AuthnComponentWithProvider.
*
* @param {Object} props - Props for the component.
* @returns {JSX.Element} The rendered sign-in component.
*/
export const SignInComponent = (props) => (
<AuthnComponentWithProvider {...props} formToRender={LOGIN_FORM} />
);
/**
* Component that renders a sign-up form using AuthnComponentWithProvider.
*
* @param {Object} props - Props for the component.
* @returns {JSX.Element} The rendered sign-up component.
*/
const SignUpComponent = (props) => (
<AuthnComponentWithProvider {...props} />
);
export default SignUpComponent;