Skip to content

Commit

Permalink
Add country restrictions when geolocation autocomplete address
Browse files Browse the repository at this point in the history
  • Loading branch information
omninando committed May 16, 2019
1 parent aadc005 commit b19f1d6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Country restrictions when geolocation autocomplete address

## [3.5.9] - 2019-05-08

### Fixed
Expand Down
10 changes: 8 additions & 2 deletions react/geolocation/GetAddressByGeolocation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ describe('Get Address by Geolocation', () => {
address: newAddress,
onChangeAddress: jest.fn(),
rules: useOneLevel,
googleMaps: {Geocoder: googleMaps(mockFn)},
googleMaps: { Geocoder: googleMaps(mockFn) },
}
const geolocationAddress = getAddressByGeolocation(geolocationProps)

expect(mockFn).toHaveBeenCalledWith({address: '321 Praia de Botafogo'}, expect.any(Function))
expect(mockFn).toHaveBeenCalledWith(
{
address: '321 Praia de Botafogo',
componentRestrictions: { country: 'EC' },
},
expect.any(Function),
)
})
})
61 changes: 32 additions & 29 deletions react/geolocation/Utils.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
import geolocationAutoCompleteAddress from './geolocationAutoCompleteAddress'

export default function getAddressByGeolocation(geolocationProps) {
const {
address,
onChangeAddress,
rules,
googleMaps,
} = geolocationProps
const { address, onChangeAddress, rules, googleMaps } = geolocationProps

if (!googleMaps || !address || !rules || !address['number'].value) {
return
}

const geocoder = new googleMaps.Geocoder()
geocoder.geocode({ address: `${address['number'].value} ${address['street'].value}` }, (results, status) => {
if (status === googleMaps.GeocoderStatus.OK) {
if (results[0]) {
const googleAddress = results[0]
const autoCompletedAddress = geolocationAutoCompleteAddress(
address,
googleAddress,
rules
)
geocoder.geocode(
{
componentRestrictions: {
country: rules.abbr,
},
address: `${address['number'].value} ${address['street'].value}`,
},
(results, status) => {
if (status === googleMaps.GeocoderStatus.OK) {
if (results[0]) {
const googleAddress = results[0]
const autoCompletedAddress = geolocationAutoCompleteAddress(
address,
googleAddress,
rules.geolocation,
)

onChangeAddress({
...autoCompletedAddress,
complement: {
value: null,
},
reference: {
value: null,
},
})
return autoCompletedAddress
onChangeAddress({
...autoCompletedAddress,
complement: {
value: null,
},
reference: {
value: null,
},
})
return autoCompletedAddress
}
} else {
console.warn(`Google Maps Error: ${status}`)
}
} else {
console.warn(`Google Maps Error: ${status}`)
}
})
},
)
}

0 comments on commit b19f1d6

Please sign in to comment.