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

Add Promise based versions to usePlacesAutocompleteService #212

Open
nzayatz14 opened this issue Mar 17, 2023 · 0 comments
Open

Add Promise based versions to usePlacesAutocompleteService #212

nzayatz14 opened this issue Mar 17, 2023 · 0 comments

Comments

@nzayatz14
Copy link

Hi @ErrorPro

Is there any way you could add Promise-based versions of placesAutocompleteService.current.getPlacePredictions and placesAutocompleteService.current.getQueryPredictions in your usePlacesAutocompleteService hook?

Currently they look like:

if (placesAutocompleteService.current && optionsArg.input)
        placesAutocompleteService.current.getPlacePredictions(
          {
            ...(sessionToken && autocompleteSession.current
              ? { sessionToken: autocompleteSession.current }
              : {}),
            ...options,
            ...optionsArg,
          },
          (r) => {
            setIsPlacePredsLoading(false);
            setPlacePredictions(r || []);
          }
        );

but they could be wrapped like this:

return new Promise((resolve, reject) => {
            if (placesAutocompleteService.current && optionsArg.input) {
                placesAutocompleteService.current.getPlacePredictions(
                    {
                        ...(sessionToken && autocompleteSession.current
                            ? { sessionToken: autocompleteSession.current }
                            : {}),
                        ...options,
                        ...optionsArg,
                    },
                    (r) => {
                        setIsPlacePredsLoading(false);
                        resolve(r || []);
                    }
                );
            } else if (!optionsArg.input) {
                reject(`Invalid "input" arg: ${optionsArg.input}`);
            } else {
                reject("Google places instance not yet loaded");
            }
        });

This way, users could use the usePlacesAutocompleteService hook inline with promises instead of having to listen to updates to the placePredictions variable inside of a useEffect like you have in your example:

useEffect(() => {
    // fetch place details for the first element in placePredictions array
    if (placePredictions.length)
      placesService?.getDetails(
        {
          placeId: placePredictions[0].place_id,
        },
        (placeDetails) => savePlaceDetailsToState(placeDetails)
      );
  }, [placePredictions]);

Thanks!
Nick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant