Skip to content

Commit

Permalink
fix(geocoder): Propogate geocoder error to consumer
Browse files Browse the repository at this point in the history
closes ubilabs#486

Add a new prop to propogate errors from the Geocoder.geocode API to the consumer application
  • Loading branch information
prateek3255 committed Jun 12, 2021
1 parent 660b665 commit afdce17
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@rollup/plugin-replace": "^2.3.3",
"@types/chai": "^4.2.12",
"@types/classnames": "^2.2.10",
"@types/googlemaps": "^3.39.13",
"@types/google.maps": "^3.45.2",
"@types/jsdom": "^16.2.4",
"@types/lodash.debounce": "^4.0.6",
"@types/mocha": "^8.0.3",
Expand Down
50 changes: 28 additions & 22 deletions src/Geosuggest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ export default class extends React.Component<IProps, IState> {
this.placesService.getDetails(options, (results, status) => {
if (status === this.googleMaps.places.PlacesServiceStatus.OK) {
const gmaps = results;
const location = (gmaps.geometry &&
const location = (gmaps?.geometry &&
gmaps.geometry.location) as google.maps.LatLng;
const suggest = {
...suggestToGeocode,
Expand All @@ -560,35 +560,41 @@ export default class extends React.Component<IProps, IState> {
}
});
} else {
const country = Array.isArray(this.props.country)
? this.props.country[0]
: this.props.country;
const options: google.maps.GeocoderRequest = {
address: suggestToGeocode.label,
bounds: this.props.bounds,
componentRestrictions: this.props.country
? {country: this.props.country}
: // eslint-disable-next-line no-undefined
undefined,
componentRestrictions: {country},
location: this.props.location
};

this.geocoder.geocode(options, (results, status) => {
if (status === this.googleMaps.GeocoderStatus.OK) {
const gmaps = results[0];
const location = (gmaps.geometry &&
gmaps.geometry.location) as google.maps.LatLng;
const suggest = {
...suggestToGeocode,
gmaps,
location: {
lat: location.lat(),
lng: location.lng()
}
};
this.geocoder
.geocode(options, (results, status) => {
if (status === this.googleMaps.GeocoderStatus.OK) {
const gmaps = results?.[0];
const location = (gmaps?.geometry &&
gmaps.geometry.location) as google.maps.LatLng;
const suggest = {
...suggestToGeocode,
gmaps,
location: {
lat: location.lat(),
lng: location.lng()
}
};

if (this.props.onSuggestSelect) {
this.props.onSuggestSelect(suggest);
if (this.props.onSuggestSelect) {
this.props.onSuggestSelect(suggest);
}
}
}
});
})
?.catch((error) => {
if (this.props.handleGeocodingError) {
this.props.handleGeocodingError(error);
}
});
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/types/location.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type ILocation = ISuggest & {
lat: number;
lng: number;
};
readonly gmaps?: google.maps.GeocoderResult | google.maps.places.PlaceResult;
readonly gmaps?:
| google.maps.GeocoderResult
| google.maps.places.PlaceResult
| null;
};
export default ILocation;
1 change: 1 addition & 0 deletions src/types/props.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ export default interface IProps {
readonly minLength?: number;
readonly placeDetailFields?: string[] | null;
readonly inputType?: string;
readonly handleGeocodingError?: (error: Error) => void;
}
10 changes: 0 additions & 10 deletions test/fixtures/predictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
return [
{
description: 'New York, NY, United States',
id: '7eae6a016a9c6f58e2044573fb8f14227b6e1f96',
matched_substrings: [
{
length: 2,
offset: 0
}
],
place_id: 'ChIJOwg_06VPwokRYv534QaPC8g',
reference: '...',
structured_formatting: {
main_text: 'New York, NY, United States',
main_text_matched_substrings: [
Expand Down Expand Up @@ -41,15 +39,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
},
{
description: 'New York, IA, United States',
id: '329cb7144660f29514f351db26cef864634f748a',
matched_substrings: [
{
length: 2,
offset: 0
}
],
place_id: 'ChIJD_qB3F8X6YcRDraFbXmLUD4',
reference: '...',
structured_formatting: {
main_text: 'New York, IA, United States',
main_text_matched_substrings: [
Expand Down Expand Up @@ -78,15 +74,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
},
{
description: 'New York, United States',
id: '349c7fc49816ce54bb586cf8fa2cd79b255746b3',
matched_substrings: [
{
length: 2,
offset: 0
}
],
place_id: 'ChIJqaUj8fBLzEwRZ5UY3sHGz90',
reference: '...',
structured_formatting: {
main_text: 'New York, United States',
main_text_matched_substrings: [
Expand All @@ -111,15 +105,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
},
{
description: 'New Jersey, United States',
id: '10806aba84cf3520ebd83c6a3f749bad23c4e2e6',
matched_substrings: [
{
length: 2,
offset: 0
}
],
place_id: 'ChIJn0AAnpX7wIkRjW0_-Ad70iw',
reference: '...',
structured_formatting: {
main_text: 'New Jersey, United States',
main_text_matched_substrings: [
Expand All @@ -144,15 +136,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
},
{
description: 'Newark, NJ, United States',
id: 'c71040d6268e495203b4ca7ca4299893601f63fc',
matched_substrings: [
{
length: 2,
offset: 0
}
],
place_id: 'ChIJHQ6aMnBTwokRc-T-3CrcvOE',
reference: '...',
structured_formatting: {
main_text: 'Newark, NJ, United States',
main_text_matched_substrings: [
Expand Down

0 comments on commit afdce17

Please sign in to comment.