-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added `executeSearchWithUserLocation` function J=SLAP-1706 TEST=manual - set location to washington DC in when vertical page is loaded, click update my location, and see that the label is updated to my device's location. - set default search query to 'engineers near me' and see that user location is set based on location near my device.
- Loading branch information
Yen Truong
committed
Nov 17, 2021
1 parent
ced7e2c
commit 032f446
Showing
5 changed files
with
72 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { AnswersActions } from "@yext/answers-headless-react"; | ||
import { SearchIntent } from "@yext/answers-headless"; | ||
|
||
const defaultGeolocationOptions: PositionOptions = { | ||
enableHighAccuracy: false, | ||
timeout: 6000, | ||
maximumAge: 300000, | ||
}; | ||
|
||
/** | ||
* Executes universal or vertical search with user location retrieved from nagivator.geolocation API. | ||
* If useLocationOnNearMeOnly is true, will attempt to get user location only if the query's searchIntents | ||
* (retrieved through autocomplete response) contains 'NEAR_ME'. If there's a failure in getting | ||
* user location, a normal search is performed. | ||
*/ | ||
export async function executeSearchWithUserLocation( | ||
answersActions: AnswersActions, | ||
isVertical: boolean, | ||
geolocationOptions?: PositionOptions, | ||
useLocationOnNearMeOnly?: boolean | ||
) { | ||
const executeSearch = () => { | ||
isVertical | ||
? answersActions.executeVerticalQuery() | ||
: answersActions.executeUniversalQuery(); | ||
return; | ||
} | ||
|
||
if (useLocationOnNearMeOnly) { | ||
const results = isVertical | ||
? await answersActions.executeVerticalAutocomplete() | ||
: await answersActions.executeUniversalAutocomplete(); | ||
if (!results?.inputIntents?.includes(SearchIntent.NearMe)) { | ||
executeSearch(); | ||
return; | ||
} | ||
} | ||
|
||
if ('geolocation' in navigator) { | ||
navigator.geolocation.getCurrentPosition((position: GeolocationPosition) => { | ||
answersActions.setUserLocation({ | ||
latitude: position.coords.latitude, | ||
longitude: position.coords.longitude, | ||
}) | ||
executeSearch(); | ||
}, | ||
(err) => { | ||
console.error(err); | ||
console.error('unable to determine user location.'); | ||
executeSearch(); | ||
}, | ||
Object.assign(defaultGeolocationOptions, geolocationOptions) | ||
); | ||
} else { | ||
console.warn('unable to determine user location.'); | ||
executeSearch(); | ||
} | ||
} |