Skip to content

Commit afdce17

Browse files
committed
fix(geocoder): Propogate geocoder error to consumer
closes ubilabs#486 Add a new prop to propogate errors from the Geocoder.geocode API to the consumer application
1 parent 660b665 commit afdce17

File tree

6 files changed

+38
-38
lines changed

6 files changed

+38
-38
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"@rollup/plugin-replace": "^2.3.3",
2929
"@types/chai": "^4.2.12",
3030
"@types/classnames": "^2.2.10",
31-
"@types/googlemaps": "^3.39.13",
31+
"@types/google.maps": "^3.45.2",
3232
"@types/jsdom": "^16.2.4",
3333
"@types/lodash.debounce": "^4.0.6",
3434
"@types/mocha": "^8.0.3",

src/Geosuggest.tsx

+28-22
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ export default class extends React.Component<IProps, IState> {
542542
this.placesService.getDetails(options, (results, status) => {
543543
if (status === this.googleMaps.places.PlacesServiceStatus.OK) {
544544
const gmaps = results;
545-
const location = (gmaps.geometry &&
545+
const location = (gmaps?.geometry &&
546546
gmaps.geometry.location) as google.maps.LatLng;
547547
const suggest = {
548548
...suggestToGeocode,
@@ -560,35 +560,41 @@ export default class extends React.Component<IProps, IState> {
560560
}
561561
});
562562
} else {
563+
const country = Array.isArray(this.props.country)
564+
? this.props.country[0]
565+
: this.props.country;
563566
const options: google.maps.GeocoderRequest = {
564567
address: suggestToGeocode.label,
565568
bounds: this.props.bounds,
566-
componentRestrictions: this.props.country
567-
? {country: this.props.country}
568-
: // eslint-disable-next-line no-undefined
569-
undefined,
569+
componentRestrictions: {country},
570570
location: this.props.location
571571
};
572572

573-
this.geocoder.geocode(options, (results, status) => {
574-
if (status === this.googleMaps.GeocoderStatus.OK) {
575-
const gmaps = results[0];
576-
const location = (gmaps.geometry &&
577-
gmaps.geometry.location) as google.maps.LatLng;
578-
const suggest = {
579-
...suggestToGeocode,
580-
gmaps,
581-
location: {
582-
lat: location.lat(),
583-
lng: location.lng()
584-
}
585-
};
573+
this.geocoder
574+
.geocode(options, (results, status) => {
575+
if (status === this.googleMaps.GeocoderStatus.OK) {
576+
const gmaps = results?.[0];
577+
const location = (gmaps?.geometry &&
578+
gmaps.geometry.location) as google.maps.LatLng;
579+
const suggest = {
580+
...suggestToGeocode,
581+
gmaps,
582+
location: {
583+
lat: location.lat(),
584+
lng: location.lng()
585+
}
586+
};
586587

587-
if (this.props.onSuggestSelect) {
588-
this.props.onSuggestSelect(suggest);
588+
if (this.props.onSuggestSelect) {
589+
this.props.onSuggestSelect(suggest);
590+
}
589591
}
590-
}
591-
});
592+
})
593+
?.catch((error) => {
594+
if (this.props.handleGeocodingError) {
595+
this.props.handleGeocodingError(error);
596+
}
597+
});
592598
}
593599
}
594600

src/types/location.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ type ILocation = ISuggest & {
88
lat: number;
99
lng: number;
1010
};
11-
readonly gmaps?: google.maps.GeocoderResult | google.maps.places.PlaceResult;
11+
readonly gmaps?:
12+
| google.maps.GeocoderResult
13+
| google.maps.places.PlaceResult
14+
| null;
1215
};
1316
export default ILocation;

src/types/props.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ export default interface IProps {
5858
readonly minLength?: number;
5959
readonly placeDetailFields?: string[] | null;
6060
readonly inputType?: string;
61+
readonly handleGeocodingError?: (error: Error) => void;
6162
}

test/fixtures/predictions.ts

-10
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
44
return [
55
{
66
description: 'New York, NY, United States',
7-
id: '7eae6a016a9c6f58e2044573fb8f14227b6e1f96',
87
matched_substrings: [
98
{
109
length: 2,
1110
offset: 0
1211
}
1312
],
1413
place_id: 'ChIJOwg_06VPwokRYv534QaPC8g',
15-
reference: '...',
1614
structured_formatting: {
1715
main_text: 'New York, NY, United States',
1816
main_text_matched_substrings: [
@@ -41,15 +39,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
4139
},
4240
{
4341
description: 'New York, IA, United States',
44-
id: '329cb7144660f29514f351db26cef864634f748a',
4542
matched_substrings: [
4643
{
4744
length: 2,
4845
offset: 0
4946
}
5047
],
5148
place_id: 'ChIJD_qB3F8X6YcRDraFbXmLUD4',
52-
reference: '...',
5349
structured_formatting: {
5450
main_text: 'New York, IA, United States',
5551
main_text_matched_substrings: [
@@ -78,15 +74,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
7874
},
7975
{
8076
description: 'New York, United States',
81-
id: '349c7fc49816ce54bb586cf8fa2cd79b255746b3',
8277
matched_substrings: [
8378
{
8479
length: 2,
8580
offset: 0
8681
}
8782
],
8883
place_id: 'ChIJqaUj8fBLzEwRZ5UY3sHGz90',
89-
reference: '...',
9084
structured_formatting: {
9185
main_text: 'New York, United States',
9286
main_text_matched_substrings: [
@@ -111,15 +105,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
111105
},
112106
{
113107
description: 'New Jersey, United States',
114-
id: '10806aba84cf3520ebd83c6a3f749bad23c4e2e6',
115108
matched_substrings: [
116109
{
117110
length: 2,
118111
offset: 0
119112
}
120113
],
121114
place_id: 'ChIJn0AAnpX7wIkRjW0_-Ad70iw',
122-
reference: '...',
123115
structured_formatting: {
124116
main_text: 'New Jersey, United States',
125117
main_text_matched_substrings: [
@@ -144,15 +136,13 @@ export default function predictions(): google.maps.places.AutocompletePrediction
144136
},
145137
{
146138
description: 'Newark, NJ, United States',
147-
id: 'c71040d6268e495203b4ca7ca4299893601f63fc',
148139
matched_substrings: [
149140
{
150141
length: 2,
151142
offset: 0
152143
}
153144
],
154145
place_id: 'ChIJHQ6aMnBTwokRc-T-3CrcvOE',
155-
reference: '...',
156146
structured_formatting: {
157147
main_text: 'Newark, NJ, United States',
158148
main_text_matched_substrings: [

0 commit comments

Comments
 (0)