diff --git a/CHANGELOG.md b/CHANGELOG.md index de6d7356..8494632b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/react/geolocation/GetAddressByGeolocation.test.js b/react/geolocation/GetAddressByGeolocation.test.js index f5ac5b84..b73ecdd5 100644 --- a/react/geolocation/GetAddressByGeolocation.test.js +++ b/react/geolocation/GetAddressByGeolocation.test.js @@ -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), + ) }) }) diff --git a/react/geolocation/Utils.js b/react/geolocation/Utils.js index 9f826cee..331c8439 100644 --- a/react/geolocation/Utils.js +++ b/react/geolocation/Utils.js @@ -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}`) - } - }) + }, + ) }