Skip to content

Commit 1e67d51

Browse files
feat: addd countries changes for embargo
1 parent 9593641 commit 1e67d51

File tree

6 files changed

+55
-5
lines changed

6 files changed

+55
-5
lines changed

src/profile/data/actions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ export const fetchProfileSuccess = (
2525
preferences,
2626
courseCertificates,
2727
isAuthenticatedUserProfile,
28+
countries,
2829
) => ({
2930
type: FETCH_PROFILE.SUCCESS,
3031
account,
3132
preferences,
3233
courseCertificates,
3334
isAuthenticatedUserProfile,
35+
countries,
3436
});
3537

3638
export const fetchProfileReset = () => ({

src/profile/data/reducers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const initialState = {
2222
drafts: {},
2323
isLoadingProfile: true,
2424
isAuthenticatedUserProfile: false,
25+
countries: [],
2526
};
2627

2728
const profilePage = (state = initialState, action = {}) => {
@@ -42,6 +43,7 @@ const profilePage = (state = initialState, action = {}) => {
4243
courseCertificates: action.courseCertificates,
4344
isLoadingProfile: false,
4445
isAuthenticatedUserProfile: action.isAuthenticatedUserProfile,
46+
countries: action.countries,
4547
};
4648
case SAVE_PROFILE.BEGIN:
4749
return {

src/profile/data/sagas.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function* handleFetchProfile(action) {
4141
let preferences = {};
4242
let account = userAccount;
4343
let courseCertificates = null;
44+
let countries = [];
4445

4546
try {
4647
yield put(fetchProfileBegin());
@@ -49,6 +50,7 @@ export function* handleFetchProfile(action) {
4950
const calls = [
5051
call(ProfileApiService.getAccount, username),
5152
call(ProfileApiService.getCourseCertificates, username),
53+
call(ProfileApiService.getCountryList),
5254
];
5355

5456
if (isAuthenticatedUserProfile) {
@@ -61,9 +63,9 @@ export function* handleFetchProfile(action) {
6163
const result = yield all(calls);
6264

6365
if (isAuthenticatedUserProfile) {
64-
[account, courseCertificates, preferences] = result;
66+
[account, courseCertificates, countries, preferences] = result;
6567
} else {
66-
[account, courseCertificates] = result;
68+
[account, courseCertificates, countries] = result;
6769
}
6870

6971
// Set initial visibility values for account
@@ -89,6 +91,7 @@ export function* handleFetchProfile(action) {
8991
preferences,
9092
courseCertificates,
9193
isAuthenticatedUserProfile,
94+
countries,
9295
));
9396

9497
yield put(fetchProfileReset());

src/profile/data/selectors.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const isLoadingProfileSelector = state => state.profilePage.isLoadingProf
2323
export const currentlyEditingFieldSelector = state => state.profilePage.currentlyEditingField;
2424
export const accountErrorsSelector = state => state.profilePage.errors;
2525
export const isAuthenticatedUserProfileSelector = state => state.profilePage.isAuthenticatedUserProfile;
26+
export const countriesSelector = state => state.profilePage.countries;
2627

2728
export const editableFormModeSelector = createSelector(
2829
profileAccountSelector,
@@ -112,7 +113,17 @@ export const sortedLanguagesSelector = createSelector(
112113

113114
export const sortedCountriesSelector = createSelector(
114115
localeSelector,
115-
locale => getCountryList(locale),
116+
countriesSelector,
117+
profileAccountSelector,
118+
(locale, countries, profileAccount) => {
119+
const countryList = getCountryList(locale);
120+
const countriesCodes = new Set(countries.map(country => country.code));
121+
122+
return countryList.filter(({ code }) => {
123+
const isUserCountry = code === profileAccount.country || countriesCodes.has(code);
124+
return isUserCountry;
125+
});
126+
},
116127
);
117128

118129
export const preferredLanguageSelector = createSelector(
@@ -130,10 +141,12 @@ export const countrySelector = createSelector(
130141
editableFormSelector,
131142
sortedCountriesSelector,
132143
countryMessagesSelector,
133-
(editableForm, sortedCountries, countryMessages) => ({
144+
countriesSelector,
145+
(editableForm, sortedCountries, countryMessages, countries) => ({
134146
...editableForm,
135147
sortedCountries,
136148
countryMessages,
149+
countries,
137150
}),
138151
);
139152

src/profile/data/services.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,21 @@ export async function getCourseCertificates(username) {
147147
return [];
148148
}
149149
}
150+
151+
function extractCountryList(data) {
152+
return data?.fields
153+
.find(({ name }) => name === 'country')
154+
?.options?.map(({ value, name }) => ({ code: value, name })) || [];
155+
}
156+
157+
export async function getCountryList() {
158+
const url = `${getConfig().LMS_BASE_URL}/user_api/v1/account/registration/`;
159+
160+
try {
161+
const { data } = await getHttpClient().get(url);
162+
return extractCountryList(data);
163+
} catch (e) {
164+
logError(e);
165+
return [];
166+
}
167+
}

src/profile/forms/Country.jsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Country extends React.Component {
2323
this.handleSubmit = this.handleSubmit.bind(this);
2424
this.handleClose = this.handleClose.bind(this);
2525
this.handleOpen = this.handleOpen.bind(this);
26+
this.isDisabledCountry = this.isDisabledCountry.bind(this);
2627
}
2728

2829
handleChange(e) {
@@ -46,6 +47,13 @@ class Country extends React.Component {
4647
this.props.openHandler(this.props.formId);
4748
}
4849

50+
isDisabledCountry = (country) => {
51+
const { countries } = this.props;
52+
const countriesCodes = new Set(countries.map(countryObj => countryObj.code));
53+
54+
return !countriesCodes.has(country);
55+
};
56+
4957
render() {
5058
const {
5159
formId,
@@ -85,7 +93,7 @@ class Country extends React.Component {
8593
>
8694
<option value="">&nbsp;</option>
8795
{sortedCountries.map(({ code, name }) => (
88-
<option key={code} value={code}>{name}</option>
96+
<option key={code} value={code} disabled={this.isDisabledCountry(code)}>{name}</option>
8997
))}
9098
</select>
9199
{error !== null && (
@@ -157,6 +165,10 @@ Country.propTypes = {
157165
code: PropTypes.string.isRequired,
158166
name: PropTypes.string.isRequired,
159167
})).isRequired,
168+
countries: PropTypes.arrayOf(PropTypes.shape({
169+
code: PropTypes.string.isRequired,
170+
name: PropTypes.string.isRequired,
171+
})).isRequired,
160172
countryMessages: PropTypes.objectOf(PropTypes.string).isRequired,
161173

162174
// Actions

0 commit comments

Comments
 (0)