-
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.
- Loading branch information
Yen Truong
committed
Nov 19, 2021
1 parent
c04ba74
commit 6e9f573
Showing
6 changed files
with
84 additions
and
79 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,73 @@ | ||
import { AnswersActions } from "@yext/answers-headless-react"; | ||
import { SearchIntent } from "@yext/answers-headless"; | ||
|
||
const defaultGeolocationOptions: PositionOptions = { | ||
enableHighAccuracy: false, | ||
timeout: 6000, | ||
maximumAge: 300000, | ||
}; | ||
|
||
/** | ||
* If the provided search intents include a 'NEAR_ME' intent, retrieve and | ||
* store user's location in headless state. Then, execute a search with | ||
* user's location, if that's successfully retrieved, attached to the request. | ||
*/ | ||
export async function executeSearchWithIntents( | ||
answersActions: AnswersActions, | ||
isVertical: boolean, | ||
intents: SearchIntent[], | ||
geolocationOptions?: PositionOptions | ||
) { | ||
if (intents.includes(SearchIntent.NearMe)) { | ||
try { | ||
const position = await getUserLocation(geolocationOptions); | ||
answersActions.setUserLocation({ | ||
latitude: position.coords.latitude, | ||
longitude: position.coords.longitude, | ||
}); | ||
} catch(e) { | ||
console.error(e); | ||
} | ||
} | ||
executeSearch(answersActions, isVertical); | ||
} | ||
|
||
/** | ||
* Executes a universal/vertical search | ||
*/ | ||
export async function executeSearch(answersActions: AnswersActions, isVertical: boolean) { | ||
isVertical | ||
? answersActions.executeVerticalQuery() | ||
: answersActions.executeUniversalQuery(); | ||
return; | ||
} | ||
|
||
/** | ||
* Get search intents of the current query stored in headless using autocomplete request. | ||
*/ | ||
export async function getSearchIntents(answersActions: AnswersActions, isVertical: boolean) { | ||
const results = isVertical | ||
? await answersActions.executeVerticalAutocomplete() | ||
: await answersActions.executeUniversalAutocomplete(); | ||
return results?.inputIntents; | ||
} | ||
|
||
/** | ||
* Retrieves user's location using nagivator.geolocation API | ||
*/ | ||
export async function getUserLocation(geolocationOptions?: PositionOptions): Promise<GeolocationPosition> { | ||
return new Promise((resolve, reject) => { | ||
if ('geolocation' in navigator) { | ||
navigator.geolocation.getCurrentPosition( | ||
position => resolve(position), | ||
err => { | ||
console.error('Error occured using geolocation API. Unable to determine user\'s location.'); | ||
reject(err); | ||
}, | ||
Object.assign(defaultGeolocationOptions, geolocationOptions) | ||
); | ||
} else { | ||
reject('No access to geolocation API. Unable to determine user\'s location.'); | ||
} | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.