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: implemented restricted country implementation #1425

Open
wants to merge 4 commits into
base: 2u-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
10 changes: 8 additions & 2 deletions src/common-components/data/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ export const getThirdPartyAuthContextBegin = () => ({
type: THIRD_PARTY_AUTH_CONTEXT.BEGIN,
});

export const getThirdPartyAuthContextSuccess = (fieldDescriptions, optionalFields, thirdPartyAuthContext) => ({
export const getThirdPartyAuthContextSuccess = (
fieldDescriptions,
optionalFields,
thirdPartyAuthContext,
countries) => ({
type: THIRD_PARTY_AUTH_CONTEXT.SUCCESS,
payload: { fieldDescriptions, optionalFields, thirdPartyAuthContext },
payload: {
fieldDescriptions, optionalFields, thirdPartyAuthContext, countries,
},
});

export const getThirdPartyAuthContextFailure = () => ({
Expand Down
4 changes: 4 additions & 0 deletions src/common-components/data/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ export const progressiveProfilingFields = {
},
},
};

export const FIELD_LABELS = {
COUNTRY: 'country',
};
1 change: 1 addition & 0 deletions src/common-components/data/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const reducer = (state = defaultState, action = {}) => {
optionalFields: action.payload.optionalFields,
thirdPartyAuthContext: action.payload.thirdPartyAuthContext,
thirdPartyAuthApiStatus: COMPLETE_STATE,
countries: action.payload.countries,
};
}
case THIRD_PARTY_AUTH_CONTEXT.FAILURE:
Expand Down
5 changes: 4 additions & 1 deletion src/common-components/data/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from './actions';
import { progressiveProfilingFields, registerFields } from './constants';
import {
getCountryList,
getThirdPartyAuthContext,
} from './service';
import { setCountryFromThirdPartyAuthContext } from '../../register/data/actions';
Expand All @@ -20,6 +21,7 @@ export function* fetchThirdPartyAuthContext(action) {
const {
fieldDescriptions, optionalFields, thirdPartyAuthContext,
} = yield call(getThirdPartyAuthContext, action.payload.urlParams);
const countries = (yield call(getCountryList)) || [];

yield put(setCountryFromThirdPartyAuthContext(thirdPartyAuthContext.countryCode));
// hard code country field, level of education and gender fields
Expand All @@ -28,9 +30,10 @@ export function* fetchThirdPartyAuthContext(action) {
registerFields,
progressiveProfilingFields,
thirdPartyAuthContext,
countries,
));
} else {
yield put(getThirdPartyAuthContextSuccess(fieldDescriptions, optionalFields, thirdPartyAuthContext));
yield put(getThirdPartyAuthContextSuccess(fieldDescriptions, optionalFields, thirdPartyAuthContext, countries));
}
} catch (e) {
yield put(getThirdPartyAuthContextFailure());
Expand Down
28 changes: 28 additions & 0 deletions src/common-components/data/service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { logError } from '@edx/frontend-platform/logging';

import { FIELD_LABELS } from './constants';

// eslint-disable-next-line import/prefer-default-export
export async function getThirdPartyAuthContext(urlParams) {
Expand All @@ -23,3 +26,28 @@
thirdPartyAuthContext: data.contextData || {},
};
}

function extractCountryList(data) {

Check warning on line 30 in src/common-components/data/service.js

View check run for this annotation

Codecov / codecov/patch

src/common-components/data/service.js#L30

Added line #L30 was not covered by tests
return data?.fields
.find(({ name }) => name === FIELD_LABELS.COUNTRY)
?.options?.map(({ value, name }) => ({ code: value, name })) || [];

Check warning on line 33 in src/common-components/data/service.js

View check run for this annotation

Codecov / codecov/patch

src/common-components/data/service.js#L32-L33

Added lines #L32 - L33 were not covered by tests
}

export async function getCountryList() {
try {
const requestConfig = {

Check warning on line 38 in src/common-components/data/service.js

View check run for this annotation

Codecov / codecov/patch

src/common-components/data/service.js#L36-L38

Added lines #L36 - L38 were not covered by tests
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
isPublic: true,
};

const { data } = await getAuthenticatedHttpClient()

Check warning on line 43 in src/common-components/data/service.js

View check run for this annotation

Codecov / codecov/patch

src/common-components/data/service.js#L43

Added line #L43 was not covered by tests
.get(
`${getConfig().LMS_BASE_URL}/user_api/v1/account/registration/`,
requestConfig,
);
return extractCountryList(data);

Check warning on line 48 in src/common-components/data/service.js

View check run for this annotation

Codecov / codecov/patch

src/common-components/data/service.js#L48

Added line #L48 was not covered by tests
} catch (e) {
logError(e);
return [];

Check warning on line 51 in src/common-components/data/service.js

View check run for this annotation

Codecov / codecov/patch

src/common-components/data/service.js#L50-L51

Added lines #L50 - L51 were not covered by tests
}
}
8 changes: 7 additions & 1 deletion src/common-components/data/tests/sagas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import * as api from '../service';

const { loggingService } = initializeMockLogging();

jest.mock('../service', () => ({
getCountryList: jest.fn(),
getThirdPartyAuthContext: jest.fn(),
}));

describe('fetchThirdPartyAuthContext', () => {
const params = {
payload: { urlParams: {} },
Expand All @@ -31,6 +36,7 @@ describe('fetchThirdPartyAuthContext', () => {
thirdPartyAuthContext: data,
fieldDescriptions: {},
optionalFields: {},
countries: [],
}));

const dispatched = [];
Expand All @@ -44,7 +50,7 @@ describe('fetchThirdPartyAuthContext', () => {
expect(dispatched).toEqual([
actions.getThirdPartyAuthContextBegin(),
setCountryFromThirdPartyAuthContext(),
actions.getThirdPartyAuthContextSuccess({}, {}, data),
actions.getThirdPartyAuthContextSuccess({}, {}, data, []),
]);
getThirdPartyAuthContext.mockClear();
});
Expand Down
2 changes: 2 additions & 0 deletions src/register/RegistrationPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const RegistrationPage = (props) => {
const providers = useSelector(state => state.commonComponents.thirdPartyAuthContext.providers);
const secondaryProviders = useSelector(state => state.commonComponents.thirdPartyAuthContext.secondaryProviders);
const pipelineUserDetails = useSelector(state => state.commonComponents.thirdPartyAuthContext.pipelineUserDetails);
const countries = useSelector(state => state.commonComponents.countries);

const backendValidations = useSelector(getBackendValidations);
const queryParams = useMemo(() => getAllPossibleQueryParams(), []);
Expand Down Expand Up @@ -392,6 +393,7 @@ const RegistrationPage = (props) => {
setFormFields={setConfigurableFormFields}
autoSubmitRegisterForm={autoSubmitRegForm}
fieldDescriptions={fieldDescriptions}
countries={countries}
/>
<StatefulButton
id="register-user"
Expand Down
23 changes: 18 additions & 5 deletions src/register/components/ConfigurableRegistrationForm.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo } from 'react';
import React, { useCallback, useEffect, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { getConfig } from '@edx/frontend-platform';
Expand Down Expand Up @@ -37,6 +37,7 @@ const ConfigurableRegistrationForm = (props) => {
setFieldErrors,
setFormFields,
autoSubmitRegistrationForm,
countries,
} = props;
const dispatch = useDispatch();

Expand All @@ -45,10 +46,6 @@ const ConfigurableRegistrationForm = (props) => {
confused and unable to create an account. So we added the United States entry in the dropdown list.
*/

const countryList = useMemo(() => (
getCountryList(getLocale()).concat([{ code: 'US', name: 'United States' }]).filter(country => country.code !== 'RU')
), []);

let showTermsOfServiceAndHonorCode = false;
let showCountryField = false;

Expand Down Expand Up @@ -82,6 +79,17 @@ const ConfigurableRegistrationForm = (props) => {
}
}, [autoSubmitRegistrationForm]); // eslint-disable-line react-hooks/exhaustive-deps

const removeDisabledCountries = useCallback((countryList) => {
if (!countries.length) {
return countryList;
}
const allowedCountries = new Set(countries.map(({ code }) => code));
return countryList.filter(({ code }) => allowedCountries.has(code));
}, [countries]);

const countryList = useMemo(() => removeDisabledCountries(
getCountryList(getLocale()).concat([{ code: 'US', name: 'United States' }]), []), [removeDisabledCountries]);

const handleErrorChange = (fieldName, error) => {
if (fieldName) {
setFieldErrors(prevErrors => ({
Expand Down Expand Up @@ -262,11 +270,16 @@ ConfigurableRegistrationForm.propTypes = {
setFieldErrors: PropTypes.func.isRequired,
setFormFields: PropTypes.func.isRequired,
autoSubmitRegistrationForm: PropTypes.bool,
countries: PropTypes.arrayOf(PropTypes.shape({
code: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
})),
};

ConfigurableRegistrationForm.defaultProps = {
fieldDescriptions: {},
autoSubmitRegistrationForm: false,
countries: [],
};

export default ConfigurableRegistrationForm;
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe('ConfigurableRegistrationForm', () => {
},
},
autoSubmitRegistrationForm: true,
countries: [{ code: 'AX', name: 'Åland Islands' }, { code: 'AL', name: 'Albania' }],
};

render(routerWrapper(reduxWrapper(
Expand Down