Skip to content

Commit

Permalink
feat: added country disabling feature
Browse files Browse the repository at this point in the history
  • Loading branch information
awais-ansari committed Sep 14, 2024
1 parent b13fc58 commit 2fc0bd6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/profile/data/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const initialState = {
drafts: {},
isLoadingProfile: true,
isAuthenticatedUserProfile: false,
disabledCountries: ['RU'],
};

const profilePage = (state = initialState, action = {}) => {
Expand Down
7 changes: 6 additions & 1 deletion src/profile/data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const isLoadingProfileSelector = state => state.profilePage.isLoadingProf
export const currentlyEditingFieldSelector = state => state.profilePage.currentlyEditingField;
export const accountErrorsSelector = state => state.profilePage.errors;
export const isAuthenticatedUserProfileSelector = state => state.profilePage.isAuthenticatedUserProfile;
export const disabledCountriesSelector = state => state.profilePage.disabledCountries;

export const editableFormModeSelector = createSelector(
profileAccountSelector,
Expand Down Expand Up @@ -130,10 +131,14 @@ export const countrySelector = createSelector(
editableFormSelector,
sortedCountriesSelector,
countryMessagesSelector,
(editableForm, sortedCountries, countryMessages) => ({
disabledCountriesSelector,
profileAccountSelector,
(editableForm, sortedCountries, countryMessages, disabledCountries, account) => ({
...editableForm,
sortedCountries,
countryMessages,
disabledCountries,
committedCountry: account.country,
}),
);

Expand Down
35 changes: 32 additions & 3 deletions src/profile/forms/Country.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class Country extends React.Component {

handleSubmit(e) {
e.preventDefault();
this.props.submitHandler(this.props.formId);
const { country, disabledCountries, formId, submitHandler } = this.props;

Check failure on line 38 in src/profile/forms/Country.jsx

View workflow job for this annotation

GitHub Actions / tests (lint, 20)

Expected a line break after this opening brace

Check failure on line 38 in src/profile/forms/Country.jsx

View workflow job for this annotation

GitHub Actions / tests (lint, 20)

Expected a line break before this closing brace

if (!disabledCountries.includes(country)) {
submitHandler(formId);
}
}

handleClose() {
Expand All @@ -46,6 +50,26 @@ class Country extends React.Component {
this.props.openHandler(this.props.formId);
}

isDisabledCountry = (country) => {

Check failure on line 53 in src/profile/forms/Country.jsx

View workflow job for this annotation

GitHub Actions / tests (lint, 20)

Expected indentation of 2 spaces but found 1
const { disabledCountries } = this.props;
return disabledCountries.includes(country);
};

filteredCountries = (countryList) => {
const { disabledCountries, committedCountry } = this.props;

if (!disabledCountries.length) {
return countryList;
}

return countryList.filter(({ code }) => {
const isDisabled = this.isDisabledCountry(code);
const isUserCountry = code === committedCountry;

return !isDisabled || isUserCountry;
});
};

render() {
const {
formId,
Expand All @@ -58,6 +82,7 @@ class Country extends React.Component {
sortedCountries,
countryMessages,
} = this.props;
const filteredCountries = this.filteredCountries(sortedCountries);

return (
<SwitchContent
Expand All @@ -84,8 +109,8 @@ class Country extends React.Component {
onChange={this.handleChange}
>
<option value="">&nbsp;</option>
{sortedCountries.map(({ code, name }) => (
<option key={code} value={code}>{name}</option>
{filteredCountries.map(({ code, name }) => (
<option key={code} value={code} disabled={this.isDisabledCountry(code)}>{name}</option>
))}
</select>
{error !== null && (
Expand Down Expand Up @@ -157,7 +182,9 @@ Country.propTypes = {
code: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
})).isRequired,
disabledCountries: PropTypes.arrayOf(PropTypes.string),
countryMessages: PropTypes.objectOf(PropTypes.string).isRequired,
committedCountry: PropTypes.string,

// Actions
changeHandler: PropTypes.func.isRequired,
Expand All @@ -175,6 +202,8 @@ Country.defaultProps = {
country: null,
visibilityCountry: 'private',
error: null,
disabledCountries: [],
committedCountry: '',
};

export default connect(
Expand Down

0 comments on commit 2fc0bd6

Please sign in to comment.