Skip to content

Commit

Permalink
add refreshToken method to update session token for the autocomplete …
Browse files Browse the repository at this point in the history
…service
  • Loading branch information
ErrorPro committed Oct 31, 2021
1 parent f945d09 commit ab767fc
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ The hook returns an object with properties:
- `placesAutocompleteService`: Instance of [AutocompleteService](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteService)
- `placesService`: Instance of [PlacesService](https://developers.google.com/maps/documentation/javascript/reference/places-service#PlacesService)
- `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)
- `refreshSessionToken`: call this function if you need [to refresh the session token](https://developers.google.com/maps/documentation/places/web-service/session-tokens)
- `placePredictions`: an array of [AutocompletePrediction](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteResponse)
- `isPlacePredictionsLoading`: sets to true when a `getPlacePredictions` request is being sent and not yet resolved.
- `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).
Expand Down
1 change: 1 addition & 0 deletions lib/usePlacesAutocompleteService.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface usePlacesAutocompleteServiceResponse {
getQueryPredictions: (
opt: google.maps.places.QueryAutocompletionRequest
) => void;
refreshSessionToken: () => void;
}

export default function usePlacesAutocompleteService(
Expand Down
3 changes: 3 additions & 0 deletions lib/usePlacesAutocompleteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ function usePlacesAutocompleteService(_ref) {
setQueryInputValue(null);
debouncedQueryPredictions(opt);
setIsQueryPredsLoading(false);
},
refreshSessionToken: function refreshSessionToken() {
autocompleteSession.current = new google.maps.places.AutocompleteSessionToken();
}
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-google-autocomplete",
"version": "2.5.0",
"version": "2.6.0",
"description": "React component for google autocomplete.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/usePlacesAutocompleteService.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface usePlacesAutocompleteServiceResponse {
getQueryPredictions: (
opt: google.maps.places.QueryAutocompletionRequest
) => void;
refreshSessionToken: () => void;
}

export default function usePlacesAutocompleteService(
Expand Down
4 changes: 4 additions & 0 deletions src/usePlacesAutocompleteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,9 @@ export default function usePlacesAutocompleteService({
debouncedQueryPredictions(opt);
setIsQueryPredsLoading(false);
},
refreshSessionToken: () => {
autocompleteSession.current =
new google.maps.places.AutocompleteSessionToken();
},
};
}

0 comments on commit ab767fc

Please sign in to comment.