Skip to content

Commit

Permalink
feat: implemented restricted countries functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sundasnoreen12 committed Feb 19, 2025
1 parent c109f6e commit 6e633dc
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
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
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
4 changes: 3 additions & 1 deletion src/common-components/data/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
THIRD_PARTY_AUTH_CONTEXT,
} from './actions';
import {
getCountryList,
getThirdPartyAuthContext,
} from './service';
import { setCountryFromThirdPartyAuthContext } from '../../register/data/actions';
Expand All @@ -18,9 +19,10 @@ export function* fetchThirdPartyAuthContext(action) {
const {
fieldDescriptions, optionalFields, thirdPartyAuthContext,
} = yield call(getThirdPartyAuthContext, action.payload.urlParams);
const countries = yield call(getCountryList);

yield put(setCountryFromThirdPartyAuthContext(thirdPartyAuthContext.countryCode));
yield put(getThirdPartyAuthContextSuccess(fieldDescriptions, optionalFields, thirdPartyAuthContext));
yield put(getThirdPartyAuthContextSuccess(fieldDescriptions, optionalFields, thirdPartyAuthContext, countries));
} catch (e) {
yield put(getThirdPartyAuthContextFailure());
logError(e);
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 '../../data/constants';

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

function extractCountryList(data) {
return data?.fields
.find(({ name }) => name === FIELD_LABELS.COUNTRY)
?.options?.map(({ value, name }) => ({ code: value, name })) || [];
}

export async function getCountryList() {
try {
const requestConfig = {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
isPublic: true,
};

const { data } = await getAuthenticatedHttpClient()
.get(
`${getConfig().LMS_BASE_URL}/user_api/v1/account/registration/`,
requestConfig,
);
return extractCountryList(data);
} catch (e) {
logError(e);
return [];
}
}
3 changes: 3 additions & 0 deletions src/data/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ export const VALID_EMAIL_REGEX = '(^[-!#$%&\'*+/=?^_`{}|~0-9A-Z]+(\\.[-!#$%&\'*+
// things like auto-enrollment upon login and registration.
export const AUTH_PARAMS = ['course_id', 'enrollment_action', 'course_mode', 'email_opt_in', 'purchase_workflow', 'next', 'register_for_free', 'track', 'is_account_recovery', 'variant', 'host', 'cta'];
export const REDIRECT = 'redirect';
export const FIELD_LABELS = {
COUNTRY: 'country',
};
2 changes: 2 additions & 0 deletions src/register/RegistrationPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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 @@ -358,6 +359,7 @@ const RegistrationPage = (props) => {
setFormFields={setConfigurableFormFields}
autoSubmitRegisterForm={autoSubmitRegForm}
fieldDescriptions={fieldDescriptions}
countries={countries}
/>
<StatefulButton
id="register-user"
Expand Down
20 changes: 18 additions & 2 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 { getConfig } from '@edx/frontend-platform';
import { getCountryList, getLocale, useIntl } from '@edx/frontend-platform/i18n';
Expand Down Expand Up @@ -31,13 +31,13 @@ const ConfigurableRegistrationForm = (props) => {
setFieldErrors,
setFormFields,
autoSubmitRegistrationForm,
countries,
} = props;

/** The reason for adding the entry 'United States' is that Chrome browser aut-fill the form with the 'Unites
States' instead of 'United States of America' which does not exist in country dropdown list and gets the user
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' }]), []);

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

const removeDisabledCountries = useCallback((countryList) => {
if (!countries.length) {
return countryList;
}

return countryList.filter((country) => new Set(countries.map(({ code }) => code)).has(country.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 @@ -231,11 +242,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;

0 comments on commit 6e633dc

Please sign in to comment.