-
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.
refactor to use useAutocomplete hook, remove async stuff for now, wil…
…l address in another item
- Loading branch information
Yen Truong
committed
Nov 16, 2021
1 parent
113ba49
commit 3807fa6
Showing
3 changed files
with
30 additions
and
22 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useRef, useState } from "react"; | ||
import { AutocompleteResult, useAnswersActions } from '@yext/answers-headless-react'; | ||
|
||
export function useAutocomplete(isVertical: boolean): [AutocompleteResult[], () => Promise<void>] { | ||
const answersActions = useAnswersActions(); | ||
const autocompleteNetworkIds = useRef({ latestRequest: 0, responseInState: 0 }); | ||
const [ autocompleteResults, setAutoCompleteResults ] = useState<AutocompleteResult[]>([]); | ||
async function executeAutocomplete () { | ||
const requestId = ++autocompleteNetworkIds.current.latestRequest; | ||
let response = null; | ||
isVertical | ||
? response = await answersActions.executeVerticalAutocomplete() | ||
: response = await answersActions.executeUniversalAutocomplete(); | ||
if (requestId >= autocompleteNetworkIds.current.responseInState) { | ||
setAutoCompleteResults(response?.results || []); | ||
autocompleteNetworkIds.current.responseInState = requestId; | ||
} | ||
} | ||
return [ autocompleteResults, executeAutocomplete ] | ||
}; |