From 24316ab8230342fdd7cdf07e4bcdfc263da623d1 Mon Sep 17 00:00:00 2001 From: Nishan Chhetri <52850000+nishancx@users.noreply.github.com> Date: Thu, 29 Jun 2023 11:12:28 +0545 Subject: [PATCH 1/6] Use fallback for country --- src/components/AddressSearch/index.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/components/AddressSearch/index.js b/src/components/AddressSearch/index.js index 9699eb9aab94..4f49f64f2b6c 100644 --- a/src/components/AddressSearch/index.js +++ b/src/components/AddressSearch/index.js @@ -4,6 +4,7 @@ import PropTypes from 'prop-types'; import {LogBox, ScrollView, View} from 'react-native'; import {GooglePlacesAutocomplete} from 'react-native-google-places-autocomplete'; import lodashGet from 'lodash/get'; +import findKey from 'lodash/findKey'; import withLocalize, {withLocalizePropTypes} from '../withLocalize'; import styles from '../../styles/styles'; import themeColors from '../../styles/themes/default'; @@ -142,7 +143,15 @@ function AddressSearch(props) { // Make sure that the order of keys remains such that the country is always set above the state. // Refer to https://github.com/Expensify/App/issues/15633 for more information. - const {state: stateAutoCompleteFallback = '', city: cityAutocompleteFallback = ''} = GooglePlacesUtils.getPlaceAutocompleteTerms(autocompleteData.terms); + const { + country: countryAutoCompleteFallback = '', + state: stateAutoCompleteFallback = '', + city: cityAutocompleteFallback = '', + } = GooglePlacesUtils.getPlaceAutocompleteTerms(autocompleteData.terms); + + const autoCompleteFallbackCountryCode = findKey(CONST.ALL_COUNTRIES, (country) => country === countryAutoCompleteFallback); + + const countryWithFallback = country || autoCompleteFallbackCountryCode; const values = { street: `${streetNumber} ${streetName}`.trim(), @@ -162,13 +171,13 @@ function AddressSearch(props) { // If the address is not in the US, use the full length state name since we're displaying the address's // state / province in a TextInput instead of in a picker. - if (country !== CONST.COUNTRY.US) { + if (countryWithFallback !== CONST.COUNTRY.US) { values.state = longStateName; } // UK addresses return countries (e.g. England) in the state field (administrative_area_level_1) // So we use a secondary field (administrative_area_level_2) as a fallback - if (country === CONST.COUNTRY.GB) { + if (countryWithFallback === CONST.COUNTRY.GB) { values.state = stateFallback; } @@ -178,9 +187,10 @@ function AddressSearch(props) { values.street += `, ${subpremise}`; } - const isValidCountryCode = lodashGet(CONST.ALL_COUNTRIES, country); + const isValidCountryCode = lodashGet(CONST.ALL_COUNTRIES, countryWithFallback); + if (isValidCountryCode) { - values.country = country; + values.country = countryWithFallback; } if (props.inputID) { From 7e50eb6a280a4c14f7a6c8f8e4c4d57851ea1768 Mon Sep 17 00:00:00 2001 From: Nishan Chhetri <52850000+nishancx@users.noreply.github.com> Date: Fri, 30 Jun 2023 05:40:54 +0545 Subject: [PATCH 2/6] Fix lint error --- src/components/AddressSearch/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/components/AddressSearch/index.js b/src/components/AddressSearch/index.js index 4f49f64f2b6c..1275b26d5836 100644 --- a/src/components/AddressSearch/index.js +++ b/src/components/AddressSearch/index.js @@ -121,7 +121,7 @@ function AddressSearch(props) { postal_code: zipCode, administrative_area_level_1: state, administrative_area_level_2: stateFallback, - country, + country: countryPrimary, } = GooglePlacesUtils.getAddressComponents(addressComponents, { street_number: 'long_name', route: 'long_name', @@ -144,14 +144,14 @@ function AddressSearch(props) { // Make sure that the order of keys remains such that the country is always set above the state. // Refer to https://github.com/Expensify/App/issues/15633 for more information. const { - country: countryAutoCompleteFallback = '', + country: countryFallbackLongName = '', state: stateAutoCompleteFallback = '', city: cityAutocompleteFallback = '', } = GooglePlacesUtils.getPlaceAutocompleteTerms(autocompleteData.terms); - const autoCompleteFallbackCountryCode = findKey(CONST.ALL_COUNTRIES, (country) => country === countryAutoCompleteFallback); + const countryFallback = findKey(CONST.ALL_COUNTRIES, (country) => country === countryFallbackLongName); - const countryWithFallback = country || autoCompleteFallbackCountryCode; + const country = countryPrimary || countryFallback; const values = { street: `${streetNumber} ${streetName}`.trim(), @@ -171,13 +171,13 @@ function AddressSearch(props) { // If the address is not in the US, use the full length state name since we're displaying the address's // state / province in a TextInput instead of in a picker. - if (countryWithFallback !== CONST.COUNTRY.US) { + if (country !== CONST.COUNTRY.US) { values.state = longStateName; } // UK addresses return countries (e.g. England) in the state field (administrative_area_level_1) // So we use a secondary field (administrative_area_level_2) as a fallback - if (countryWithFallback === CONST.COUNTRY.GB) { + if (country === CONST.COUNTRY.GB) { values.state = stateFallback; } @@ -187,10 +187,10 @@ function AddressSearch(props) { values.street += `, ${subpremise}`; } - const isValidCountryCode = lodashGet(CONST.ALL_COUNTRIES, countryWithFallback); + const isValidCountryCode = lodashGet(CONST.ALL_COUNTRIES, countryFallback); if (isValidCountryCode) { - values.country = countryWithFallback; + values.country = country; } if (props.inputID) { From 180edef82d47382a021f336282f4c10c6c3df465 Mon Sep 17 00:00:00 2001 From: Nishan Chhetri <52850000+nishancx@users.noreply.github.com> Date: Fri, 30 Jun 2023 05:43:22 +0545 Subject: [PATCH 3/6] Check if country is valid --- src/components/AddressSearch/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/AddressSearch/index.js b/src/components/AddressSearch/index.js index 1275b26d5836..9f7ab913380b 100644 --- a/src/components/AddressSearch/index.js +++ b/src/components/AddressSearch/index.js @@ -187,7 +187,7 @@ function AddressSearch(props) { values.street += `, ${subpremise}`; } - const isValidCountryCode = lodashGet(CONST.ALL_COUNTRIES, countryFallback); + const isValidCountryCode = lodashGet(CONST.ALL_COUNTRIES, country); if (isValidCountryCode) { values.country = country; From 85c81b64fa67e8d74405cb526444df5d4f1fcb9c Mon Sep 17 00:00:00 2001 From: Nishan Chhetri <52850000+nishancx@users.noreply.github.com> Date: Fri, 30 Jun 2023 05:44:23 +0545 Subject: [PATCH 4/6] Cleanup --- src/components/AddressSearch/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/AddressSearch/index.js b/src/components/AddressSearch/index.js index 9f7ab913380b..34f96bd8745d 100644 --- a/src/components/AddressSearch/index.js +++ b/src/components/AddressSearch/index.js @@ -188,7 +188,6 @@ function AddressSearch(props) { } const isValidCountryCode = lodashGet(CONST.ALL_COUNTRIES, country); - if (isValidCountryCode) { values.country = country; } From ba65be5b2ddab640f694d76612e35ad793299b76 Mon Sep 17 00:00:00 2001 From: Nishan Chhetri <52850000+nishancx@users.noreply.github.com> Date: Fri, 30 Jun 2023 07:58:23 +0545 Subject: [PATCH 5/6] Add test for getPlaceAutocompleteTerms --- tests/unit/GooglePlacesUtilsTest.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/unit/GooglePlacesUtilsTest.js b/tests/unit/GooglePlacesUtilsTest.js index ef7a4491fec0..1bb27bdd9f2f 100644 --- a/tests/unit/GooglePlacesUtilsTest.js +++ b/tests/unit/GooglePlacesUtilsTest.js @@ -129,6 +129,12 @@ const addressComponents = [ types: ['postal_code'], }, ]; + +const autoCompleteTerms = [ + {offset: 0, value: 'Bangladesh Border Road'}, + {offset: 24, value: 'Bangladesh'}, +]; + describe('GooglePlacesUtilsTest', () => { describe('getAddressComponents', () => { it('should find address components by type', () => { @@ -189,4 +195,14 @@ describe('GooglePlacesUtilsTest', () => { expect(executionTime).toBeLessThan(5.0); }); }); + describe('getPlaceAutocompleteTerms', () => { + it('should find auto complete terms', () => { + expect(GooglePlacesUtils.getPlaceAutocompleteTerms(autoCompleteTerms)).toStrictEqual({ + country: 'Bangladesh', + state: 'Bangladesh Border Road', + city: '', + street: '', + }); + }); + }); }); From 3004da3c78b40dca621b7f8a9ce99808216e0902 Mon Sep 17 00:00:00 2001 From: Nishan Chhetri <52850000+nishancx@users.noreply.github.com> Date: Fri, 30 Jun 2023 23:07:13 +0545 Subject: [PATCH 6/6] Use underscore --- src/components/AddressSearch/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/AddressSearch/index.js b/src/components/AddressSearch/index.js index 34f96bd8745d..795e45c6f892 100644 --- a/src/components/AddressSearch/index.js +++ b/src/components/AddressSearch/index.js @@ -4,7 +4,6 @@ import PropTypes from 'prop-types'; import {LogBox, ScrollView, View} from 'react-native'; import {GooglePlacesAutocomplete} from 'react-native-google-places-autocomplete'; import lodashGet from 'lodash/get'; -import findKey from 'lodash/findKey'; import withLocalize, {withLocalizePropTypes} from '../withLocalize'; import styles from '../../styles/styles'; import themeColors from '../../styles/themes/default'; @@ -149,7 +148,7 @@ function AddressSearch(props) { city: cityAutocompleteFallback = '', } = GooglePlacesUtils.getPlaceAutocompleteTerms(autocompleteData.terms); - const countryFallback = findKey(CONST.ALL_COUNTRIES, (country) => country === countryFallbackLongName); + const countryFallback = _.findKey(CONST.ALL_COUNTRIES, (country) => country === countryFallbackLongName); const country = countryPrimary || countryFallback;