From 9dc662f88c961b8f4ea3e80aefe8f07b3fadf096 Mon Sep 17 00:00:00 2001 From: Juliana Kang Date: Tue, 27 Feb 2024 17:12:50 -0500 Subject: [PATCH] =?UTF-8?q?Revert=20"feat:=20Use=20country-state-city=20to?= =?UTF-8?q?=20populate=20state=20dropdown=20in=20StateProvi=E2=80=A6"=20(#?= =?UTF-8?q?855)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 58cde5d5eefc226ea2bb2935f6e7008fd08d4633. --- package-lock.json | 6 - package.json | 1 - .../payment-form/StateProvinceFormInput.jsx | 4 +- .../payment-form/StripePaymentForm.test.jsx | 25 +--- .../payment-form/utils/countryStatesMap.js | 115 ++++++++++++++++++ .../payment-form/utils/form-validators.js | 20 +-- 6 files changed, 122 insertions(+), 49 deletions(-) create mode 100644 src/payment/checkout/payment-form/utils/countryStatesMap.js diff --git a/package-lock.json b/package-lock.json index dd436b84e..4d1027de5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,6 @@ "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", @@ -7359,11 +7358,6 @@ "js-yaml": "bin/js-yaml.js" } }, - "node_modules/country-state-city": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/country-state-city/-/country-state-city-3.2.1.tgz", - "integrity": "sha512-kxbanqMc6izjhc/EHkGPCTabSPZ2G6eG4/97akAYHJUN4stzzFEvQPZoF8oXDQ+10gM/O/yUmISCR1ZVxyb6EA==" - }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", diff --git a/package.json b/package.json index 1acc3a48d..8bb334f0c 100755 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "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", diff --git a/src/payment/checkout/payment-form/StateProvinceFormInput.jsx b/src/payment/checkout/payment-form/StateProvinceFormInput.jsx index 63565053f..8252dc1dc 100644 --- a/src/payment/checkout/payment-form/StateProvinceFormInput.jsx +++ b/src/payment/checkout/payment-form/StateProvinceFormInput.jsx @@ -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 = getCountryStatesMap(country); + const states = getStates(country); if (states) { options.push([( diff --git a/src/payment/checkout/payment-form/StripePaymentForm.test.jsx b/src/payment/checkout/payment-form/StripePaymentForm.test.jsx index 937faf803..c39fed99e 100644 --- a/src/payment/checkout/payment-form/StripePaymentForm.test.jsx +++ b/src/payment/checkout/payment-form/StripePaymentForm.test.jsx @@ -128,27 +128,8 @@ describe('', () => { lastName: '', address: '', city: '', - 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 + country: 'GB', postalCode: '', - state: '', - optionalField: '', - }, - { - firstName: '', - lastName: '', - address: '', - city: '', - country: 'CA', // Canada state and postal code are required - postalCode: '', - state: '', optionalField: '', }, { @@ -156,7 +137,7 @@ describe('', () => { lastName: '', address: '', city: '', - country: 'US', // United States state and postal code are required + country: 'US', postalCode: '', state: '', optionalField: '', @@ -166,7 +147,7 @@ describe('', () => { lastName: '', address: '', city: '', - country: 'IN', // India state is required + country: 'IN', state: '', optionalField: '', }, diff --git a/src/payment/checkout/payment-form/utils/countryStatesMap.js b/src/payment/checkout/payment-form/utils/countryStatesMap.js new file mode 100644 index 000000000..339ef782a --- /dev/null +++ b/src/payment/checkout/payment-form/utils/countryStatesMap.js @@ -0,0 +1,115 @@ +const COUNTRY_STATES_MAP = { + CA: { + AB: 'Alberta', + BC: 'British Columbia', + MB: 'Manitoba', + NB: 'New Brunswick', + NL: 'Newfoundland and Labrador', + NS: 'Nova Scotia', + NT: 'Northwest Territories', + NU: 'Nunavut', + ON: 'Ontario', + PE: 'Prince Edward Island', + QC: 'Québec', + SK: 'Saskatchewan', + YT: 'Yukon', + }, + IN: { + AN: 'Andaman and Nicobar Islands', + AP: 'Andhra Pradesh', + AR: 'Arunachal Pradesh', + AS: 'Assam', + BR: 'Bihar', + CH: 'Chandigarh', + CT: 'Chhattisgarh', + DN: 'Dadra and Nagar Haveli', + DD: 'Daman and Diu', + DL: 'Delhi', + GA: 'Goa', + GJ: 'Gujarat', + HR: 'Haryana', + HP: 'Himachal Pradesh', + JK: 'Jammu and Kashmir', + JH: 'Jharkhand', + KA: 'Karnataka', + KL: 'Kerala', + LD: 'Lakshadweep', + MP: 'Madhya Pradesh', + MH: 'Maharashtra', + MN: 'Manipur', + ML: 'Meghalaya', + MZ: 'Mizoram', + NL: 'Nagaland', + OR: 'Odisha', + PY: 'Puducherry', + PB: 'Punjab', + RJ: 'Rajasthan', + SK: 'Sikkim', + TN: 'Tamil Nadu', + TG: 'Telangana', + TR: 'Tripura', + UP: 'Uttar Pradesh', + UT: 'Uttarakhand', + WB: 'West Bengal', + }, + US: { + AL: 'Alabama', + AK: 'Alaska', + AZ: 'Arizona', + AR: 'Arkansas', + AA: 'Armed Forces Americas', + AE: 'Armed Forces Europe', + AP: 'Armed Forces Pacific', + CA: 'California', + CO: 'Colorado', + CT: 'Connecticut', + DE: 'Delaware', + DC: 'District Of Columbia', + FL: 'Florida', + GA: 'Georgia', + HI: 'Hawaii', + ID: 'Idaho', + IL: 'Illinois', + IN: 'Indiana', + IA: 'Iowa', + KS: 'Kansas', + KY: 'Kentucky', + LA: 'Louisiana', + ME: 'Maine', + MD: 'Maryland', + MA: 'Massachusetts', + MI: 'Michigan', + MN: 'Minnesota', + MS: 'Mississippi', + MO: 'Missouri', + MT: 'Montana', + NE: 'Nebraska', + NV: 'Nevada', + NH: 'New Hampshire', + NJ: 'New Jersey', + NM: 'New Mexico', + NY: 'New York', + NC: 'North Carolina', + ND: 'North Dakota', + OH: 'Ohio', + OK: 'Oklahoma', + OR: 'Oregon', + PA: 'Pennsylvania', + RI: 'Rhode Island', + SC: 'South Carolina', + SD: 'South Dakota', + TN: 'Tennessee', + TX: 'Texas', + UT: 'Utah', + VT: 'Vermont', + VA: 'Virginia', + WA: 'Washington', + WV: 'West Virginia', + WI: 'Wisconsin', + WY: 'Wyoming', + }, +}; + +export default function getStates(country) { + return country && COUNTRY_STATES_MAP[country.toUpperCase()]; +} diff --git a/src/payment/checkout/payment-form/utils/form-validators.js b/src/payment/checkout/payment-form/utils/form-validators.js index 5e2bc208f..249a15266 100644 --- a/src/payment/checkout/payment-form/utils/form-validators.js +++ b/src/payment/checkout/payment-form/utils/form-validators.js @@ -1,18 +1,4 @@ -// 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; -}; +import getStates from './countryStatesMap'; // eslint-disable-next-line import/prefer-default-export export function isPostalCodeRequired(selectedCountry) { @@ -53,9 +39,7 @@ export function getRequiredFields(fieldValues, isBulkOrder = false, enableStripe requiredFields.postalCode = postalCode; } - // 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)) { + if (getStates(country)) { requiredFields.state = state; }