-
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
Showing
6 changed files
with
159 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,28 @@ | ||
import { useRef, useState } from "react"; | ||
import { AutocompleteResult, useAnswersActions } from '@yext/answers-headless-react'; | ||
import { AutocompleteResponse, useAnswersActions } from '@yext/answers-headless-react'; | ||
|
||
export function useAutocomplete(isVertical: boolean): [AutocompleteResult[], () => Promise<void>] { | ||
export function useAutocomplete( | ||
isVertical: boolean | ||
): [ | ||
AutocompleteResponse|undefined, | ||
() => Promise<AutocompleteResponse|undefined> | ||
] | ||
{ | ||
const answersActions = useAnswersActions(); | ||
const autocompleteNetworkIds = useRef({ latestRequest: 0, responseInState: 0 }); | ||
const [ autocompleteResults, setAutocompleteResults ] = useState<AutocompleteResult[]>([]); | ||
async function executeAutocomplete () { | ||
const [ autocompleteResponse, setAutocompleteResponse ] = useState<AutocompleteResponse>(); | ||
async function executeAutocomplete (): Promise<AutocompleteResponse|undefined> { | ||
const requestId = ++autocompleteNetworkIds.current.latestRequest; | ||
const response = isVertical | ||
? await answersActions.executeVerticalAutocomplete() | ||
: await answersActions.executeUniversalAutocomplete(); | ||
if (requestId >= autocompleteNetworkIds.current.responseInState) { | ||
setAutocompleteResults(response?.results || []); | ||
autocompleteNetworkIds.current.responseInState = requestId; | ||
} | ||
return new Promise(async (resolve) => { | ||
const response = isVertical | ||
? await answersActions.executeVerticalAutocomplete() | ||
: await answersActions.executeUniversalAutocomplete(); | ||
if (requestId >= autocompleteNetworkIds.current.responseInState) { | ||
setAutocompleteResponse(response); | ||
autocompleteNetworkIds.current.responseInState = requestId; | ||
} | ||
resolve(response); | ||
}); | ||
} | ||
return [ autocompleteResults, executeAutocomplete ] | ||
return [ autocompleteResponse, executeAutocomplete ] | ||
}; |
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) && !answersActions.state.location.userLocation) { | ||
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); | ||
}, | ||
{ ...defaultGeolocationOptions, ...geolocationOptions } | ||
); | ||
} else { | ||
reject('No access to geolocation API. Unable to determine user\'s location.'); | ||
} | ||
}); | ||
} |