Skip to content

Commit fade402

Browse files
committed
fix: Use redux hooks in Authn login page
1 parent 488644f commit fade402

File tree

3 files changed

+47
-101
lines changed

3 files changed

+47
-101
lines changed

src/login/LoginPage.jsx

Lines changed: 27 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { useEffect, useMemo, useState } from 'react';
2-
import { connect } from 'react-redux';
2+
import { useDispatch, useSelector } from 'react-redux';
33

44
import { getConfig } from '@edx/frontend-platform';
55
import { sendPageEvent, sendTrackEvent } from '@edx/frontend-platform/analytics';
6-
import { injectIntl, useIntl } from '@edx/frontend-platform/i18n';
6+
import { useIntl } from '@edx/frontend-platform/i18n';
77
import {
88
Form, StatefulButton,
99
} from '@openedx/paragon';
@@ -14,7 +14,7 @@ import { Link } from 'react-router-dom';
1414

1515
import AccountActivationMessage from './AccountActivationMessage';
1616
import {
17-
backupLoginFormBegin,
17+
backupLoginFormBegin as backupFormState,
1818
dismissPasswordResetBanner,
1919
loginRequest,
2020
} from './data/actions';
@@ -28,12 +28,11 @@ import {
2828
RedirectLogistration,
2929
ThirdPartyAuthAlert,
3030
} from '../common-components';
31-
import { getThirdPartyAuthContext } from '../common-components/data/actions';
32-
import { thirdPartyAuthContextSelector } from '../common-components/data/selectors';
31+
import { getThirdPartyAuthContext as getTPADataFromBackend } from '../common-components/data/actions';
3332
import EnterpriseSSO from '../common-components/EnterpriseSSO';
3433
import ThirdPartyAuth from '../common-components/ThirdPartyAuth';
3534
import {
36-
DEFAULT_STATE, PENDING_STATE, RESET_PAGE,
35+
PENDING_STATE, RESET_PAGE,
3736
} from '../data/constants';
3837
import {
3938
getActivationStatus,
@@ -45,12 +44,21 @@ import {
4544
import ResetPasswordSuccess from '../reset-password/ResetPasswordSuccess';
4645

4746
const LoginPage = (props) => {
47+
const dispatch = useDispatch();
48+
const { formatMessage } = useIntl();
49+
4850
const {
49-
backedUpFormData,
51+
loginFormData: backedUpFormData,
5052
loginErrorCode,
5153
loginErrorContext,
5254
loginResult,
5355
shouldBackupState,
56+
showResetPasswordSuccessBanner,
57+
submitState,
58+
} = useSelector(state => state.login);
59+
60+
const {
61+
thirdPartyAuthApiStatus,
5462
thirdPartyAuthContext: {
5563
providers,
5664
currentProvider,
@@ -59,19 +67,15 @@ const LoginPage = (props) => {
5967
platformName,
6068
errorMessage: thirdPartyErrorMessage,
6169
},
62-
thirdPartyAuthApiStatus,
70+
} = useSelector(state => state.commonComponents);
71+
72+
const {
6373
institutionLogin,
64-
showResetPasswordSuccessBanner,
65-
submitState,
66-
// Actions
67-
backupFormState,
6874
handleInstitutionLogin,
69-
getTPADataFromBackend,
7075
} = props;
71-
const { formatMessage } = useIntl();
76+
7277
const activationMsgType = getActivationStatus();
7378
const queryParams = useMemo(() => getAllPossibleQueryParams(), []);
74-
7579
const [formFields, setFormFields] = useState({ ...backedUpFormData.formFields });
7680
const [errorCode, setErrorCode] = useState({ type: '', count: 0, context: {} });
7781
const [errors, setErrors] = useState({ ...backedUpFormData.errors });
@@ -86,19 +90,19 @@ const LoginPage = (props) => {
8690
if (tpaHint) {
8791
payload.tpa_hint = tpaHint;
8892
}
89-
getTPADataFromBackend(payload);
90-
}, [getTPADataFromBackend, queryParams, tpaHint]);
93+
dispatch(getTPADataFromBackend(payload));
94+
}, [dispatch, queryParams, tpaHint]);
9195
/**
9296
* Backup the login form in redux when login page is toggled.
9397
*/
9498
useEffect(() => {
9599
if (shouldBackupState) {
96-
backupFormState({
100+
dispatch(backupFormState({
97101
formFields: { ...formFields },
98102
errors: { ...errors },
99-
});
103+
}));
100104
}
101-
}, [shouldBackupState, formFields, errors, backupFormState]);
105+
}, [shouldBackupState, formFields, errors, dispatch]);
102106

103107
useEffect(() => {
104108
if (loginErrorCode) {
@@ -141,7 +145,7 @@ const LoginPage = (props) => {
141145
const handleSubmit = (event) => {
142146
event.preventDefault();
143147
if (showResetPasswordSuccessBanner) {
144-
props.dismissPasswordResetBanner();
148+
dispatch(dismissPasswordResetBanner());
145149
}
146150

147151
const formData = { ...formFields };
@@ -158,7 +162,7 @@ const LoginPage = (props) => {
158162
password: formData.password,
159163
...queryParams,
160164
};
161-
props.loginRequest(payload);
165+
dispatch(loginRequest(payload));
162166
};
163167

164168
const handleOnChange = (event) => {
@@ -281,88 +285,10 @@ const LoginPage = (props) => {
281285
);
282286
};
283287

284-
const mapStateToProps = state => {
285-
const loginPageState = state.login;
286-
return {
287-
backedUpFormData: loginPageState.loginFormData,
288-
loginErrorCode: loginPageState.loginErrorCode,
289-
loginErrorContext: loginPageState.loginErrorContext,
290-
loginResult: loginPageState.loginResult,
291-
shouldBackupState: loginPageState.shouldBackupState,
292-
showResetPasswordSuccessBanner: loginPageState.showResetPasswordSuccessBanner,
293-
submitState: loginPageState.submitState,
294-
thirdPartyAuthContext: thirdPartyAuthContextSelector(state),
295-
thirdPartyAuthApiStatus: state.commonComponents.thirdPartyAuthApiStatus,
296-
};
297-
};
298-
299288
LoginPage.propTypes = {
300-
backedUpFormData: PropTypes.shape({
301-
formFields: PropTypes.shape({}),
302-
errors: PropTypes.shape({}),
303-
}),
304-
loginErrorCode: PropTypes.string,
305-
loginErrorContext: PropTypes.shape({
306-
email: PropTypes.string,
307-
redirectUrl: PropTypes.string,
308-
context: PropTypes.shape({}),
309-
}),
310-
loginResult: PropTypes.shape({
311-
redirectUrl: PropTypes.string,
312-
success: PropTypes.bool,
313-
}),
314-
shouldBackupState: PropTypes.bool,
315-
showResetPasswordSuccessBanner: PropTypes.bool,
316-
submitState: PropTypes.string,
317-
thirdPartyAuthApiStatus: PropTypes.string,
318289
institutionLogin: PropTypes.bool.isRequired,
319-
thirdPartyAuthContext: PropTypes.shape({
320-
currentProvider: PropTypes.string,
321-
errorMessage: PropTypes.string,
322-
platformName: PropTypes.string,
323-
providers: PropTypes.arrayOf(PropTypes.shape({})),
324-
secondaryProviders: PropTypes.arrayOf(PropTypes.shape({})),
325-
finishAuthUrl: PropTypes.string,
326-
}),
327290
// Actions
328-
backupFormState: PropTypes.func.isRequired,
329-
dismissPasswordResetBanner: PropTypes.func.isRequired,
330-
loginRequest: PropTypes.func.isRequired,
331-
getTPADataFromBackend: PropTypes.func.isRequired,
332291
handleInstitutionLogin: PropTypes.func.isRequired,
333292
};
334293

335-
LoginPage.defaultProps = {
336-
backedUpFormData: {
337-
formFields: {
338-
emailOrUsername: '', password: '',
339-
},
340-
errors: {
341-
emailOrUsername: '', password: '',
342-
},
343-
},
344-
loginErrorCode: null,
345-
loginErrorContext: {},
346-
loginResult: {},
347-
shouldBackupState: false,
348-
showResetPasswordSuccessBanner: false,
349-
submitState: DEFAULT_STATE,
350-
thirdPartyAuthApiStatus: PENDING_STATE,
351-
thirdPartyAuthContext: {
352-
currentProvider: null,
353-
errorMessage: null,
354-
finishAuthUrl: null,
355-
providers: [],
356-
secondaryProviders: [],
357-
},
358-
};
359-
360-
export default connect(
361-
mapStateToProps,
362-
{
363-
backupFormState: backupLoginFormBegin,
364-
dismissPasswordResetBanner,
365-
loginRequest,
366-
getTPADataFromBackend: getThirdPartyAuthContext,
367-
},
368-
)(injectIntl(LoginPage));
294+
export default LoginPage;

src/login/tests/LoginPage.test.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@ describe('LoginPage', () => {
3939
</MemoryRouter>
4040
</IntlProvider>
4141
);
42+
const loginFormData = {
43+
formFields: {
44+
emailOrUsername: '', password: '',
45+
},
4246

47+
errors: {
48+
emailOrUsername: '', password: '',
49+
},
50+
};
4351
const initialState = {
4452
login: {
53+
loginFormData,
4554
loginResult: { success: false, redirectUrl: '' },
4655
},
4756
commonComponents: {

src/logistration/Logistration.test.jsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ describe('Logistration', () => {
4545
</MemoryRouter>
4646
</IntlProvider>
4747
);
48+
const loginFormData = {
49+
formFields: {
50+
emailOrUsername: '', password: '',
51+
},
52+
53+
errors: {
54+
emailOrUsername: '', password: '',
55+
},
56+
};
4857

4958
const initialState = {
5059
register: {
@@ -74,7 +83,9 @@ describe('Logistration', () => {
7483
},
7584
},
7685
login: {
86+
loginFormData,
7787
loginResult: { success: false, redirectUrl: '' },
88+
7889
},
7990
};
8091

0 commit comments

Comments
 (0)