From bd470f892cdb76e3d45bf835e9b1a7fb9b3a532e Mon Sep 17 00:00:00 2001 From: Mubbshar Anwar <78487564+mubbsharanwar@users.noreply.github.com> Date: Fri, 23 Apr 2021 20:08:50 +0500 Subject: [PATCH] Pre papulate country in registration form (#251) - get country code based on IP address from server - set country code in country field VAN-366 --- src/common-components/data/service.js | 2 +- src/register/RegistrationPage.jsx | 10 ++++++ src/register/tests/RegistrationPage.test.jsx | 32 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/common-components/data/service.js b/src/common-components/data/service.js index 566a6fb8..cd6d97b1 100644 --- a/src/common-components/data/service.js +++ b/src/common-components/data/service.js @@ -11,7 +11,7 @@ export async function getThirdPartyAuthContext(urlParams) { const { data } = await getAuthenticatedHttpClient() .get( - `${getConfig().LMS_BASE_URL}/api/third_party_auth_context`, + `${getConfig().LMS_BASE_URL}/api/mfe_context`, requestConfig, ) .catch((e) => { diff --git a/src/register/RegistrationPage.jsx b/src/register/RegistrationPage.jsx index df7158ac..d50a54f7 100644 --- a/src/register/RegistrationPage.jsx +++ b/src/register/RegistrationPage.jsx @@ -128,6 +128,14 @@ class RegistrationPage extends React.Component { return false; } + if (this.props.thirdPartyAuthContext.countryCode + && this.state.country !== nextProps.thirdPartyAuthContext.countryCode) { + this.setState({ + country: nextProps.thirdPartyAuthContext.countryCode || '', + }); + return false; + } + return true; } @@ -657,6 +665,7 @@ RegistrationPage.defaultProps = { thirdPartyAuthContext: { currentProvider: null, finishAuthUrl: null, + countryCode: null, providers: [], secondaryProviders: [], pipelineUserDetails: null, @@ -688,6 +697,7 @@ RegistrationPage.propTypes = { providers: PropTypes.array, secondaryProviders: PropTypes.array, finishAuthUrl: PropTypes.string, + countryCode: PropTypes.string, pipelineUserDetails: PropTypes.shape({ email: PropTypes.string, fullname: PropTypes.string, diff --git a/src/register/tests/RegistrationPage.test.jsx b/src/register/tests/RegistrationPage.test.jsx index 1d046c07..2b9bf327 100644 --- a/src/register/tests/RegistrationPage.test.jsx +++ b/src/register/tests/RegistrationPage.test.jsx @@ -55,6 +55,7 @@ describe('RegistrationPage', () => { providers: [], secondaryProviders: [], pipelineUserDetails: null, + countryCode: null, }, }, }; @@ -671,6 +672,37 @@ describe('RegistrationPage', () => { registerPage.find('button.username-suggestion').at(0).simulate('click'); expect(registerPage.find('RegistrationPage').state('username')).toEqual('test_1'); }); + + it('Should update state if countryCode is present in context', () => { + store = mockStore({ + ...initialState, + commonComponents: { + ...initialState.commonComponents, + thirdPartyAuthContext: { + ...initialState.commonComponents.thirdPartyAuthContext, + countryCode: 'US', + }, + thirdPartyAuthApiStatus: COMPLETE_STATE, + }, + }); + + const nextProps = { + validations: null, + thirdPartyAuthContext: { + pipelineUserDetails: { + name: 'test', + email: 'test@example.com', + username: 'test-username', + }, + countryCode: 'US', + }, + }; + + const registrationPage = mount(reduxWrapper()); + const shouldUpdate = registrationPage.find('RegistrationPage').instance().shouldComponentUpdate(nextProps); + expect(registrationPage.find('RegistrationPage').state('country')).toEqual('US'); + expect(shouldUpdate).toBe(false); + }); }); describe('TestOptionalFields', () => {