-
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 18, 2021
1 parent
b398b42
commit 4a4d600
Showing
6 changed files
with
128 additions
and
25 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,27 @@ | ||
import { useRef, useState } from "react"; | ||
import { AutocompleteResult, useAnswersActions } from '@yext/answers-headless-react'; | ||
import { MutableRefObject, useRef, useState } from "react"; | ||
import { AutocompleteResponse, useAnswersActions } from '@yext/answers-headless-react'; | ||
|
||
export function useAutocomplete(isVertical: boolean): [AutocompleteResult[], () => Promise<void>] { | ||
export function useAutocomplete( | ||
isVertical: boolean | ||
): [ AutocompleteResponse|undefined, MutableRefObject<Promise<void>>, () => Promise<void> ] { | ||
const answersActions = useAnswersActions(); | ||
const autocompleteNetworkIds = useRef({ latestRequest: 0, responseInState: 0 }); | ||
const [ autocompleteResults, setAutocompleteResults ] = useState<AutocompleteResult[]>([]); | ||
const [ autocompleteResponse, setAutocompleteResponse ] = useState<AutocompleteResponse>(); | ||
const latestAutocompleteResponseRef = useRef<Promise<void>>(Promise.resolve()); | ||
async function executeAutocomplete () { | ||
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; | ||
} | ||
latestAutocompleteResponseRef.current = new Promise(async (resolve) => { | ||
const response = isVertical | ||
? await answersActions.executeVerticalAutocomplete() | ||
: await answersActions.executeUniversalAutocomplete(); | ||
if (requestId >= autocompleteNetworkIds.current.responseInState) { | ||
setAutocompleteResponse(response); | ||
autocompleteNetworkIds.current.responseInState = requestId; | ||
if (requestId === autocompleteNetworkIds.current.latestRequest) { | ||
resolve(); | ||
} | ||
} | ||
}); | ||
} | ||
return [ autocompleteResults, executeAutocomplete ] | ||
return [ autocompleteResponse, latestAutocompleteResponseRef, 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,43 @@ | ||
import { AnswersActions } from "../../../lib"; | ||
|
||
|
||
const defaultGeolocationOptions: PositionOptions = { | ||
enableHighAccuracy: false, | ||
timeout: 6000, | ||
maximumAge: 300000, | ||
}; | ||
|
||
export default class SearchHandler { | ||
static async executeSearch(answersActions: AnswersActions, isVertical: boolean) { | ||
isVertical | ||
? answersActions.executeVerticalQuery() | ||
: answersActions.executeUniversalQuery(); | ||
return; | ||
} | ||
|
||
static async getSearchIntents(answersActions: AnswersActions, isVertical: boolean) { | ||
const results = isVertical | ||
? await answersActions.executeVerticalAutocomplete() | ||
: await answersActions.executeUniversalAutocomplete(); | ||
return results?.inputIntents; | ||
} | ||
|
||
static async getUserLocation(geolocationOptions?: PositionOptions): Promise<GeolocationPosition> { | ||
return new Promise((resolve, reject) => { | ||
if ('geolocation' in navigator) { | ||
navigator.geolocation.getCurrentPosition( | ||
position => resolve(position), | ||
err => { | ||
console.log(err); | ||
console.error('Unable to determine user\'s location.'); | ||
reject(); | ||
}, | ||
Object.assign(defaultGeolocationOptions, geolocationOptions) | ||
); | ||
} else { | ||
console.error('Unable to determine user\'s location.'); | ||
reject(); | ||
} | ||
}); | ||
} | ||
} |