-
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.
Add support for 'Near me' queries in search bar - added `search-operations` file that includes util functions for executing search, getting search intents, and user location - update LocationBias component to retrieve user location and execute search using SearchHandler. - updated `VerticalSearchPage` and `UniversalSearchPage` fetch searchIntents from autocomplete search, in the case of a default initial search with near me intent, get and update user's location if there is a near me intent, and execute query. - updated searchBar to rely on autocomplete response from latest request to retrieve the search intent, and update user's location before executing a search Note: there's a noticeable wait time when navigator.geolocation is used. and there isn't any indication that the results maybe in loading state. Should consider exposing a setLoadingStatus in answersHeadless, so it can be use when executeSearchWithUserLocation is triggered. J=SLAP-1706 TEST=manual - set location to washington DC in when vertical page is loaded, click update my location from `LocationBias` component, 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. - ran searches between non-nearMe and nearMe queries, see that setUserLocation is only on queries with nearMe intents
- Loading branch information
Showing
6 changed files
with
165 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,69 @@ | ||
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 and there's no existing | ||
* user's location in state, retrieve and store user's location in headless state. | ||
*/ | ||
export async function updateLocationIfNeeded( | ||
answersActions: AnswersActions, | ||
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); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Executes a universal/vertical search | ||
*/ | ||
export async function executeSearch(answersActions: AnswersActions, isVertical: boolean) { | ||
isVertical | ||
? answersActions.executeVerticalQuery() | ||
: answersActions.executeUniversalQuery(); | ||
} | ||
|
||
/** | ||
* 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.'); | ||
} | ||
}); | ||
} |