Skip to content

Commit

Permalink
class => functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen Truong committed Nov 19, 2021
1 parent c04ba74 commit 6e9f573
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 79 deletions.
6 changes: 3 additions & 3 deletions sample-app/src/components/LocationBias.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAnswersActions, useAnswersState, LocationBiasMethod } from '@yext/answers-headless-react';
import SearchHandler from '../utils/searchhandler';
import { executeSearch, getUserLocation } from '../utils/search-operations';

interface Props {
isVertical: boolean,
Expand Down Expand Up @@ -35,15 +35,15 @@ export default function LocationBias(props: Props) {

async function handleGeolocationClick() {
try {
const position = await SearchHandler.getUserLocation(geolocationOptions);
const position = await getUserLocation(geolocationOptions);
answersActions.setUserLocation({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
} catch (e) {
console.error(e);
}
SearchHandler.executeSearch(answersActions, isVertical);
executeSearch(answersActions, isVertical);
}

return (
Expand Down
4 changes: 2 additions & 2 deletions sample-app/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import '../sass/SearchBar.scss';
import '../sass/Autocomplete.scss';
import LoadingIndicator from './LoadingIndicator';
import { useAutocomplete } from '../hooks/useAutocomplete';
import SearchHandler from '../utils/searchhandler';
import { useRef } from 'react';
import { AutocompleteResponse } from '@yext/answers-headless';
import { executeSearchWithIntents } from '../utils/search-operations';

const SCREENREADER_INSTRUCTIONS = 'When autocomplete results are available, use up and down arrows to review and enter to select.'

Expand Down Expand Up @@ -37,7 +37,7 @@ export default function SearchBar({
async function executeQuery () {
const responseToLatestRequest = await responseToLatestRequestRef.current;
const intents = responseToLatestRequest?.inputIntents || [];
SearchHandler.executeSearchWithIntents(answersActions, isVertical, intents, geolocationOptions);
executeSearchWithIntents(answersActions, isVertical, intents, geolocationOptions);
}

function renderSearchButton () {
Expand Down
6 changes: 3 additions & 3 deletions sample-app/src/pages/UniversalSearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useLayoutEffect } from 'react';
import { useAnswersActions } from '@yext/answers-headless-react';
import '../sass/UniversalSearchPage.scss';
import { UniversalResultsConfig } from '../universalResultsConfig';
import SearchHandler from '../utils/searchhandler';
import { executeSearchWithIntents, getSearchIntents } from '../utils/search-operations';

const universalResultsFilterConfig = {
show: true
Expand All @@ -19,8 +19,8 @@ export default function UniversalSearchPage(props: { universalResultsConfig: Uni
})
answersActions.setVerticalKey('');
const executeQuery = async () => {
const searchIntents = await SearchHandler.getSearchIntents(answersActions, false);
SearchHandler.executeSearchWithIntents(answersActions, false, searchIntents || []);
const searchIntents = await getSearchIntents(answersActions, false);
executeSearchWithIntents(answersActions, false, searchIntents || []);
};
executeQuery();
}, [answersActions]);
Expand Down
6 changes: 3 additions & 3 deletions sample-app/src/pages/VerticalSearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import '../sass/VerticalSearchPage.scss';
import { StandardCard } from '../components/cards/StandardCard';
import { useLayoutEffect } from 'react';
import { useAnswersActions } from '@yext/answers-headless-react';
import SearchHandler from '../utils/searchhandler';
import { executeSearchWithIntents, getSearchIntents } from '../utils/search-operations';

const countryFilterOptions = [
{
Expand Down Expand Up @@ -70,8 +70,8 @@ export default function VerticalSearchPage(props: {
});
answersActions.setVerticalKey(props.verticalKey);
const executeQuery = async () => {
const searchIntents = await SearchHandler.getSearchIntents(answersActions, false);
SearchHandler.executeSearchWithIntents(answersActions, true, searchIntents || []);
const searchIntents = await getSearchIntents(answersActions, false);
executeSearchWithIntents(answersActions, true, searchIntents || []);
};
executeQuery();
}, [answersActions, props.verticalKey]);
Expand Down
73 changes: 73 additions & 0 deletions sample-app/src/utils/search-operations.ts
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.');
}
});
}
68 changes: 0 additions & 68 deletions sample-app/src/utils/searchhandler.ts

This file was deleted.

0 comments on commit 6e9f573

Please sign in to comment.