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(geocoder): Propogate geocoder error to consumer #487

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ Default: `text`

The value for the `type` attribute on the html input element. Can be either `text` or `search`.

### handleGeocodingError
Type: `Function`
Default `undefined`

An optional error handler that can process the error returned by the [`Geocoder.geocode`](https://developers.google.com/maps/documentation/javascript/reference/geocoder#Geocoder-Methods) API.

#### Others

All [allowed attributes for `input[type="text"]`](https://github.com/ubilabs/react-geosuggest/blob/master/src/filter-input-attributes.ts#L4)
Expand Down
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;
Comment on lines +545 to 546
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const location = (gmaps?.geometry &&
gmaps.geometry.location) as google.maps.LatLng;
const location = 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;
Comment on lines +577 to +578
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const location = (gmaps?.geometry &&
gmaps.geometry.location) as google.maps.LatLng;
const location = 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