Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Revert "Revert "feat: Use country-state-city to populate state dropdown in StateProvinceFormInput"" #856

Merged
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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"bootstrap": "4.6.1",
"classnames": "^2.3.1",
"core-js": "^3.23.5",
"country-state-city": "^3.2.1",
"form-urlencoded": "^6.0.6",
"lodash.camelcase": "^4.3.0",
"lodash.snakecase": "^4.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/payment/checkout/payment-form/StateProvinceFormInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import PropTypes from 'prop-types';
import { Field } from 'redux-form';
import { injectIntl, intlShape, FormattedMessage } from '@edx/frontend-platform/i18n';

import { getCountryStatesMap } from './utils/form-validators';
import FormInput from './FormInput';
import FormSelect from './FormSelect';
import getStates from './utils/countryStatesMap';
import messages from './StateProvinceFormInput.messages';

class StateProvinceFormInput extends React.Component {
getOptions() {
const options = [];
const { country } = this.props;
const states = getStates(country);
const states = getCountryStatesMap(country);

if (states) {
options.push([(
Expand Down
25 changes: 22 additions & 3 deletions src/payment/checkout/payment-form/StripePaymentForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,35 @@ describe('<StripePaymentForm />', () => {
lastName: '',
address: '',
city: '',
country: 'GB',
country: 'AQ', // Antarctica does not have states, postal code not required
optionalField: '',
},
{
firstName: '',
lastName: '',
address: '',
city: '',
country: 'GB', // United Kingdom has states, state becomes required, postal code is required
postalCode: '',
state: '',
optionalField: '',
},
{
firstName: '',
lastName: '',
address: '',
city: '',
country: 'CA', // Canada state and postal code are required
postalCode: '',
state: '',
optionalField: '',
},
{
firstName: '',
lastName: '',
address: '',
city: '',
country: 'US',
country: 'US', // United States state and postal code are required
postalCode: '',
state: '',
optionalField: '',
Expand All @@ -144,7 +163,7 @@ describe('<StripePaymentForm />', () => {
lastName: '',
address: '',
city: '',
country: 'IN',
country: 'IN', // India state is required
state: '',
optionalField: '',
},
Expand Down
115 changes: 0 additions & 115 deletions src/payment/checkout/payment-form/utils/countryStatesMap.js

This file was deleted.

20 changes: 18 additions & 2 deletions src/payment/checkout/payment-form/utils/form-validators.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import getStates from './countryStatesMap';
// eslint-disable-next-line import/no-extraneous-dependencies
import { State } from 'country-state-city';

export const getCountryStatesMap = (country) => {
const states = State.getStatesOfCountry(country);

if (!states.length) {
return null;
}
const statesMap = {};
states.forEach((state) => {
statesMap[state.isoCode] = state.name;
});
return country && statesMap;
};

// eslint-disable-next-line import/prefer-default-export
export function isPostalCodeRequired(selectedCountry) {
Expand Down Expand Up @@ -39,7 +53,9 @@ export function getRequiredFields(fieldValues, isBulkOrder = false, enableStripe
requiredFields.postalCode = postalCode;
}

if (getStates(country)) {
// By using the country-state-city library to populate states, every country that
// has states from the ISO 3166-2 list will have states as a required field
if (getCountryStatesMap(country)) {
requiredFields.state = state;
}

Expand Down
Loading