Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/15753: suggested address not populate if unknown to google db #16388

Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@ -128,11 +128,16 @@ const AddressSearch = (props) => {
administrative_area_level_1: 'long_name',
});

const {
state: stateAutoCompleteFallback = '',
city: cityAutocompleteFallback = '',
} = GooglePlacesUtils.getPlaceAutocompleteTerms(autocompleteData.terms);

const values = {
street: props.value ? props.value.trim() : '',
city: city || cityFallback,
city: city || cityFallback || cityAutocompleteFallback,
zipCode,
state,
state: state || stateAutoCompleteFallback,
country: '',
};

Expand Down Expand Up @@ -191,7 +196,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 = _.map(fieldsToExtract, () => '');
tienifr marked this conversation as resolved.
Show resolved Hide resolved
_.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,
};