Skip to content

Commit

Permalink
Merge pull request #16388 from tienifr/fix/15753-recommended-address-…
Browse files Browse the repository at this point in the history
…does-not-populate
  • Loading branch information
francoisl authored Apr 1, 2023
2 parents 265f8bf + b309b34 commit 4ee21e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/components/AddressSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const AddressSearch = (props) => {
query.components = 'country:us';
}

const saveLocationDetails = (details) => {
const saveLocationDetails = (autocompleteData, details) => {
const addressComponents = details.address_components;
if (!addressComponents) {
return;
Expand Down Expand Up @@ -132,17 +132,22 @@ const 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 values = {
street: props.value ? props.value.trim() : '',

// When locality is not returned, many countries return the city as postalTown (e.g. 5 New Street
// Square, London), otherwise as sublocality (e.g. 384 Court Street Brooklyn). If postalTown is
// returned, the sublocality will be a city subdivision so shouldn't take precedence (e.g.
// Salagatan, Upssala, Sweden).
city: locality || postalTown || sublocality,
city: locality || postalTown || sublocality || cityAutocompleteFallback,
zipCode,
country: '',
state,
state: state || stateAutoCompleteFallback,
};

// If the address is not in the US, use the full length state name since we're displaying the address's
Expand Down Expand Up @@ -200,7 +205,7 @@ const AddressSearch = (props) => {
suppressDefaultStyles
enablePoweredByContainer={false}
onPress={(data, details) => {
saveLocationDetails(details);
saveLocationDetails(data, details);

// After we select an option, we set displayListViewBorder to false to prevent UI flickering
setDisplayListViewBorder(false);
Expand Down
18 changes: 18 additions & 0 deletions src/libs/GooglePlacesUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,25 @@ function getAddressComponents(addressComponents, fieldsToExtract) {
return result;
}

/**
* Finds an address term by type, and returns the value associated to key. Note that each term in the address must
* conform to the following ORDER: <street, city, state, country>
*
* @param {Array} addressTerms
* @returns {Object}
*/
function getPlaceAutocompleteTerms(addressTerms) {
const fieldsToExtract = ['country', 'state', 'city', 'street'];
const result = {};
_.each(fieldsToExtract, (fieldToExtract, index) => {
const fieldTermIndex = addressTerms.length - (index + 1);
result[fieldToExtract] = fieldTermIndex >= 0 ? addressTerms[fieldTermIndex].value : '';
});
return result;
}

export {
// eslint-disable-next-line import/prefer-default-export
getAddressComponents,
getPlaceAutocompleteTerms,
};

0 comments on commit 4ee21e9

Please sign in to comment.