Skip to content

Commit d06b9f1

Browse files
committed
Add places services into usePlacesAutocompleteService and expose sessionToken to outside
1 parent ad6302e commit d06b9f1

6 files changed

+56
-18
lines changed

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ It has only one config argument which has propperties: `apiKey`, `ref`, `onPlace
116116

117117
This hook returns object with only two properties:
118118

119-
- `ref` - is a react ref which you can assign to any input you would like.
120-
- `autocompleteRef` - is the [autocomplete](https://developers.google.com/maps/documentation/javascript/reference/places-widget#Autocomplete) instance
119+
- `ref` - is a react ref which you can assign to any input you would like.
120+
- `autocompleteRef` - is the [autocomplete](https://developers.google.com/maps/documentation/javascript/reference/places-widget#Autocomplete) instance
121121

122122
## usePlacesAutocompleteService
123123

@@ -130,10 +130,25 @@ This is an initial implementation of debounced google places autocomplete servic
130130
import usePlacesService from "react-google-autocomplete/lib/usePlacesAutocompleteService";
131131

132132
export default () => {
133-
const { placePredictions, getPlacePredictions, isPlacePredictionsLoading } =
134-
usePlacesService({
135-
apiKey: process.env.REACT_APP_GOOGLE,
136-
});
133+
const {
134+
placesService,
135+
placePredictions,
136+
getPlacePredictions,
137+
isPlacePredictionsLoading,
138+
} = usePlacesService({
139+
apiKey: process.env.REACT_APP_GOOGLE,
140+
});
141+
142+
useEffect(() => {
143+
// fetch place details for the first element in placePredictions array
144+
if (placePredictions.length)
145+
service.placesService?.getDetails(
146+
{
147+
placeId: placePredictions[0].place_id,
148+
},
149+
(placeDetails) => savePlaceDetailsToState(placeDetails)
150+
);
151+
}, [placePredictions]);
137152

138153
return (
139154
<>
@@ -169,6 +184,8 @@ The hook has only one config argument.
169184
The hook returns an object with properties:
170185

171186
- `placesAutocompleteService`: Instance of [AutocompleteService](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteService)
187+
- `placesService`: Instance of [PlacesService](https://developers.google.com/maps/documentation/javascript/reference/places-service#PlacesService)
188+
- `autocompleteSessionToken`: Instance of [AutocompleteSessionToken](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteSessionToken). You can use this to [group several requests into a single session](https://developers.google.com/maps/documentation/places/web-service/session-tokens)
172189
- `placePredictions`: an array of [AutocompletePrediction](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteResponse)
173190
- `isPlacePredictionsLoading`: sets to true when a `getPlacePredictions` request is being sent and not yet resolved.
174191
- `getPlacePredictions: (opt: `[Options](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompletionRequest)`): void`: a function which you call whenever you want to request places predictions. Takes one [argument](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteResponse).

lib/usePlacesAutocompleteService.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ interface usePlacesAutocompleteServiceConfig {
88
}
99

1010
interface usePlacesAutocompleteServiceResponse {
11+
placesService: google.maps.places.PlacesService | null;
12+
autocompleteSessionToken:
13+
| google.maps.places.AutocompleteSessionToken
14+
| undefined;
1115
placesAutocompleteService: google.maps.places.AutocompleteService | null;
1216
placePredictions: google.maps.places.AutocompletePrediction[];
1317
isPlacePredictionsLoading: boolean;

lib/usePlacesAutocompleteService.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function usePlacesAutocompleteService(_ref) {
4343
options = _ref$options === void 0 ? {} : _ref$options,
4444
sessionToken = _ref.sessionToken,
4545
language = _ref.language;
46-
var languageQueryParam = language ? "&language=".concat(language) : '';
46+
var languageQueryParam = language ? "&language=".concat(language) : "";
4747
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?key=").concat(apiKey, "&libraries=places").concat(languageQueryParam);
4848

4949
var _useState = (0, _react.useState)([]),
@@ -77,22 +77,23 @@ function usePlacesAutocompleteService(_ref) {
7777
setQueryPredictions = _useState12[1];
7878

7979
var placesAutocompleteService = (0, _react.useRef)(null);
80+
var placesService = (0, _react.useRef)(null);
8081
var autocompleteSession = (0, _react.useRef)(null);
8182
var handleLoadScript = (0, _react.useCallback)(function () {
8283
return (0, _utils.loadGoogleMapScript)(googleMapsScriptBaseUrl, googleMapsScriptUrl);
8384
}, [googleMapsScriptBaseUrl, googleMapsScriptUrl]);
8485
var debouncedPlacePredictions = (0, _react.useCallback)((0, _lodash.default)(function (optionsArg) {
85-
if (placesAutocompleteService.current && optionsArg.input) placesAutocompleteService.current.getPlacePredictions(_objectSpread(_objectSpread(_objectSpread({}, options), optionsArg), sessionToken && autocompleteSession.current ? {
86+
if (placesAutocompleteService.current && optionsArg.input) placesAutocompleteService.current.getPlacePredictions(_objectSpread(_objectSpread(_objectSpread({}, sessionToken && autocompleteSession.current ? {
8687
sessionToken: autocompleteSession.current
87-
} : {}), function (r) {
88+
} : {}), options), optionsArg), function (r) {
8889
setIsPlacePredsLoading(false);
8990
setPlacePredictions(r || []);
9091
});
9192
}, debounce), [debounce]);
9293
var debouncedQueryPredictions = (0, _react.useCallback)((0, _lodash.default)(function (optionsArg) {
93-
if (placesAutocompleteService.current && optionsArg.input) placesAutocompleteService.current.getQueryPredictions(_objectSpread(_objectSpread(_objectSpread({}, options), optionsArg), sessionToken && autocompleteSession.current ? {
94+
if (placesAutocompleteService.current && optionsArg.input) placesAutocompleteService.current.getQueryPredictions(_objectSpread(_objectSpread(_objectSpread({}, sessionToken && autocompleteSession.current ? {
9495
sessionToken: autocompleteSession.current
95-
} : {}), function (r) {
96+
} : {}), options), optionsArg), function (r) {
9697
setIsQueryPredsLoading(false);
9798
setQueryPredictions(r || []);
9899
});
@@ -104,7 +105,9 @@ function usePlacesAutocompleteService(_ref) {
104105
// eslint-disable-next-line no-undef
105106
if (!google) return console.error("Google has not been found. Make sure your provide apiKey prop."); // eslint-disable-next-line no-undef
106107

107-
placesAutocompleteService.current = new google.maps.places.AutocompleteService();
108+
placesAutocompleteService.current = new google.maps.places.AutocompleteService(); // eslint-disable-next-line no-undef
109+
110+
placesService.current = new google.maps.places.PlacesService(document.createElement("div"));
108111
if (sessionToken) autocompleteSession.current = new google.maps.places.AutocompleteSessionToken();
109112
};
110113

@@ -117,6 +120,8 @@ function usePlacesAutocompleteService(_ref) {
117120
}
118121
}, []);
119122
return {
123+
placesService: placesService.current,
124+
autocompleteSessionToken: autocompleteSession.current,
120125
placesAutocompleteService: placesAutocompleteService.current,
121126
placePredictions: placeInputValue ? placePredictions : [],
122127
isPlacePredictionsLoading: isPlacePredsLoading,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-google-autocomplete",
3-
"version": "2.3.2",
3+
"version": "2.4.0",
44
"description": "React component for google autocomplete.",
55
"main": "index.js",
66
"types": "index.d.ts",

src/usePlacesAutocompleteService.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ interface usePlacesAutocompleteServiceConfig {
88
}
99

1010
interface usePlacesAutocompleteServiceResponse {
11+
placesService: google.maps.places.PlacesService | null;
12+
autocompleteSessionToken:
13+
| google.maps.places.AutocompleteSessionToken
14+
| undefined;
1115
placesAutocompleteService: google.maps.places.AutocompleteService | null;
1216
placePredictions: google.maps.places.AutocompletePrediction[];
1317
isPlacePredictionsLoading: boolean;

src/usePlacesAutocompleteService.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default function usePlacesAutocompleteService({
1212
sessionToken,
1313
language,
1414
}) {
15-
const languageQueryParam = language ? `&language=${language}` : '';
15+
const languageQueryParam = language ? `&language=${language}` : "";
1616
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?key=${apiKey}&libraries=places${languageQueryParam}`;
1717
const [placePredictions, setPlacePredictions] = useState([]);
1818
const [isPlacePredsLoading, setIsPlacePredsLoading] = useState(false);
@@ -21,6 +21,7 @@ export default function usePlacesAutocompleteService({
2121
const [queryInputValue, setQueryInputValue] = useState(false);
2222
const [queryPredictions, setQueryPredictions] = useState([]);
2323
const placesAutocompleteService = useRef(null);
24+
const placesService = useRef(null);
2425
const autocompleteSession = useRef(null);
2526
const handleLoadScript = useCallback(
2627
() => loadGoogleMapScript(googleMapsScriptBaseUrl, googleMapsScriptUrl),
@@ -32,11 +33,11 @@ export default function usePlacesAutocompleteService({
3233
if (placesAutocompleteService.current && optionsArg.input)
3334
placesAutocompleteService.current.getPlacePredictions(
3435
{
35-
...options,
36-
...optionsArg,
3736
...(sessionToken && autocompleteSession.current
3837
? { sessionToken: autocompleteSession.current }
3938
: {}),
39+
...options,
40+
...optionsArg,
4041
},
4142
(r) => {
4243
setIsPlacePredsLoading(false);
@@ -52,11 +53,11 @@ export default function usePlacesAutocompleteService({
5253
if (placesAutocompleteService.current && optionsArg.input)
5354
placesAutocompleteService.current.getQueryPredictions(
5455
{
55-
...options,
56-
...optionsArg,
5756
...(sessionToken && autocompleteSession.current
5857
? { sessionToken: autocompleteSession.current }
5958
: {}),
59+
...options,
60+
...optionsArg,
6061
},
6162
(r) => {
6263
setIsQueryPredsLoading(false);
@@ -81,6 +82,11 @@ export default function usePlacesAutocompleteService({
8182
placesAutocompleteService.current =
8283
new google.maps.places.AutocompleteService();
8384

85+
// eslint-disable-next-line no-undef
86+
placesService.current = new google.maps.places.PlacesService(
87+
document.createElement("div")
88+
);
89+
8490
if (sessionToken)
8591
autocompleteSession.current =
8692
new google.maps.places.AutocompleteSessionToken();
@@ -94,6 +100,8 @@ export default function usePlacesAutocompleteService({
94100
}, []);
95101

96102
return {
103+
placesService: placesService.current,
104+
autocompleteSessionToken: autocompleteSession.current,
97105
placesAutocompleteService: placesAutocompleteService.current,
98106
placePredictions: placeInputValue ? placePredictions : [],
99107
isPlacePredictionsLoading: isPlacePredsLoading,

0 commit comments

Comments
 (0)