Skip to content

Commit

Permalink
fixup! feat: Add plugin slot for login page
Browse files Browse the repository at this point in the history
  • Loading branch information
xitij2000 committed Dec 18, 2024
1 parent 4bf420a commit 50d2066
Showing 1 changed file with 63 additions and 104 deletions.
167 changes: 63 additions & 104 deletions src/login/LoginPage.jsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
import React, { useEffect, useMemo, useState } from 'react';
import { connect, useSelector } from 'react-redux';
import React, {
useCallback, useEffect, useMemo, useState,
} from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { getConfig } from '@edx/frontend-platform';
import { sendPageEvent, sendTrackEvent } from '@edx/frontend-platform/analytics';
import { injectIntl, useIntl } from '@edx/frontend-platform/i18n';
import {
Form, StatefulButton,
} from '@openedx/paragon';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Form, StatefulButton } from '@openedx/paragon';
import PropTypes from 'prop-types';
import { Helmet } from 'react-helmet';
import Skeleton from 'react-loading-skeleton';
import { Link } from 'react-router-dom';

import AccountActivationMessage from './AccountActivationMessage';
import {
backupLoginFormBegin,
dismissPasswordResetBanner,
loginRequest,
} from './data/actions';
import { INVALID_FORM, TPA_AUTHENTICATION_FAILURE } from './data/constants';
import LoginFailureMessage from './LoginFailure';
import messages from './messages';
import {
FormGroup,
InstitutionLogistration,
PasswordField,
RedirectLogistration,
ThirdPartyAuthAlert,
} from '../common-components';
import AccountActivationMessage from './AccountActivationMessage';
import { getThirdPartyAuthContext } from '../common-components/data/actions';
import { thirdPartyAuthContextSelector } from '../common-components/data/selectors';
import EnterpriseSSO from '../common-components/EnterpriseSSO';
import ThirdPartyAuth from '../common-components/ThirdPartyAuth';
import {
PENDING_STATE, RESET_PAGE,
} from '../data/constants';
import { PENDING_STATE, RESET_PAGE } from '../data/constants';
import {
getActivationStatus,
getAllPossibleQueryParams,
Expand All @@ -43,23 +33,18 @@ import {
updatePathWithQueryParams,
} from '../data/utils';
import ResetPasswordSuccess from '../reset-password/ResetPasswordSuccess';
import { backupLoginFormBegin, dismissPasswordResetBanner, loginRequest } from './data/actions';
import { INVALID_FORM, TPA_AUTHENTICATION_FAILURE } from './data/constants';
import LoginFailureMessage from './LoginFailure';
import messages from './messages';

const LoginPage = (props) => {
const {
thirdPartyAuthContext: {
providers,
currentProvider,
secondaryProviders,
finishAuthUrl,
platformName,
errorMessage: thirdPartyErrorMessage,
},
institutionLogin,
// Actions
backupFormState,
handleInstitutionLogin,
getTPADataFromBackend,
} = props;
const LoginPage = ({
institutionLogin,
handleInstitutionLogin,
}) => {
const dispatch = useDispatch();
const backupFormState = useCallback((data) => dispatch(backupLoginFormBegin(data)), [dispatch]);
const getTPADataFromBackend = useCallback(() => dispatch(getThirdPartyAuthContext()), [dispatch]);
const {
backedUpFormData,
loginErrorCode,
Expand All @@ -68,6 +53,7 @@ const LoginPage = (props) => {
shouldBackupState,
showResetPasswordSuccessBanner,
submitState,
thirdPartyAuthContext,
thirdPartyAuthApiStatus,
} = useSelector((state) => ({
backedUpFormData: state.login.loginFormData,
Expand All @@ -80,12 +66,24 @@ const LoginPage = (props) => {
thirdPartyAuthContext: thirdPartyAuthContextSelector(state),
thirdPartyAuthApiStatus: state.commonComponents.thirdPartyAuthApiStatus,
}));
const {
providers,
currentProvider,
secondaryProviders,
finishAuthUrl,
platformName,
errorMessage: thirdPartyErrorMessage,
} = thirdPartyAuthContext;
const { formatMessage } = useIntl();
const activationMsgType = getActivationStatus();
const queryParams = useMemo(() => getAllPossibleQueryParams(), []);

const [formFields, setFormFields] = useState({ ...backedUpFormData.formFields });
const [errorCode, setErrorCode] = useState({ type: '', count: 0, context: {} });
const [errorCode, setErrorCode] = useState({
type: '',
count: 0,
context: {},
});
const [errors, setErrors] = useState({ ...backedUpFormData.errors });
const tpaHint = getTpaHint();

Expand All @@ -99,7 +97,7 @@ const LoginPage = (props) => {
payload.tpa_hint = tpaHint;
}
getTPADataFromBackend(payload);
}, [getTPADataFromBackend, queryParams, tpaHint]);
}, [queryParams, tpaHint, getTPADataFromBackend]);
/**
* Backup the login form in redux when login page is toggled.
*/
Expand All @@ -110,7 +108,7 @@ const LoginPage = (props) => {
errors: { ...errors },
});
}
}, [shouldBackupState, formFields, errors, backupFormState]);
}, [backupFormState, shouldBackupState, formFields, errors]);

useEffect(() => {
if (loginErrorCode) {
Expand All @@ -135,7 +133,10 @@ const LoginPage = (props) => {
}, [thirdPartyErrorMessage]);

const validateFormFields = (payload) => {
const { emailOrUsername, password } = payload;
const {
emailOrUsername,
password,
} = payload;
const fieldErrors = { ...errors };

if (emailOrUsername === '') {
Expand All @@ -153,14 +154,18 @@ const LoginPage = (props) => {
const handleSubmit = (event) => {
event.preventDefault();
if (showResetPasswordSuccessBanner) {
props.dismissPasswordResetBanner();
dispatch(dismissPasswordResetBanner());
}

const formData = { ...formFields };
const validationErrors = validateFormFields(formData);
if (validationErrors.emailOrUsername || validationErrors.password) {
setErrors({ ...validationErrors });
setErrorCode(prevState => ({ type: INVALID_FORM, count: prevState.count + 1, context: {} }));
setErrorCode(prevState => ({
type: INVALID_FORM,
count: prevState.count + 1,
context: {},
}));
return;
}

Expand All @@ -170,23 +175,35 @@ const LoginPage = (props) => {
password: formData.password,
...queryParams,
};
props.loginRequest(payload);
dispatch(loginRequest(payload));
};

const handleOnChange = (event) => {
const { name, value } = event.target;
setFormFields(prevState => ({ ...prevState, [name]: value }));
const {
name,
value,
} = event.target;
setFormFields(prevState => ({
...prevState,
[name]: value,
}));
};

const handleOnFocus = (event) => {
const { name } = event.target;
setErrors(prevErrors => ({ ...prevErrors, [name]: '' }));
setErrors(prevErrors => ({
...prevErrors,
[name]: '',
}));
};
const trackForgotPasswordLinkClick = () => {
sendTrackEvent('edx.bi.password-reset_form.toggled', { category: 'user-engagement' });
};

const { provider, skipHintedLogin } = getTpaProvider(tpaHint, providers, secondaryProviders);
const {
provider,
skipHintedLogin,
} = getTpaProvider(tpaHint, providers, secondaryProviders);

if (tpaHint) {
if (thirdPartyAuthApiStatus === PENDING_STATE) {
Expand Down Expand Up @@ -293,67 +310,9 @@ const LoginPage = (props) => {
);
};

const mapStateToProps = state => ({
thirdPartyAuthContext: thirdPartyAuthContextSelector(state),
});

LoginPage.propTypes = {
backedUpFormData: PropTypes.shape({
formFields: PropTypes.shape({}),
errors: PropTypes.shape({}),
}),
loginErrorContext: PropTypes.shape({
email: PropTypes.string,
redirectUrl: PropTypes.string,
context: PropTypes.shape({}),
}),
loginResult: PropTypes.shape({
redirectUrl: PropTypes.string,
success: PropTypes.bool,
}),
institutionLogin: PropTypes.bool.isRequired,
thirdPartyAuthContext: PropTypes.shape({
currentProvider: PropTypes.string,
errorMessage: PropTypes.string,
platformName: PropTypes.string,
providers: PropTypes.arrayOf(PropTypes.shape({})),
secondaryProviders: PropTypes.arrayOf(PropTypes.shape({})),
finishAuthUrl: PropTypes.string,
}),
// Actions
backupFormState: PropTypes.func.isRequired,
dismissPasswordResetBanner: PropTypes.func.isRequired,
loginRequest: PropTypes.func.isRequired,
getTPADataFromBackend: PropTypes.func.isRequired,
handleInstitutionLogin: PropTypes.func.isRequired,
};

LoginPage.defaultProps = {
backedUpFormData: {
formFields: {
emailOrUsername: '', password: '',
},
errors: {
emailOrUsername: '', password: '',
},
},
loginErrorContext: {},
loginResult: {},
thirdPartyAuthContext: {
currentProvider: null,
errorMessage: null,
finishAuthUrl: null,
providers: [],
secondaryProviders: [],
},
};

export default connect(
mapStateToProps,
{
backupFormState: backupLoginFormBegin,
dismissPasswordResetBanner,
loginRequest,
getTPADataFromBackend: getThirdPartyAuthContext,
},
)(injectIntl(LoginPage));
export default LoginPage;

0 comments on commit 50d2066

Please sign in to comment.