Skip to content

Commit

Permalink
updateLocationIfNeeded
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen Truong committed Nov 22, 2021
1 parent 858f785 commit 4f0ea01
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
5 changes: 3 additions & 2 deletions sample-app/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import LoadingIndicator from './LoadingIndicator';
import { useAutocomplete } from '../hooks/useAutocomplete';
import { useRef } from 'react';
import { AutocompleteResponse, SearchIntent } from '@yext/answers-headless';
import { executeSearchWithIntents } from '../utils/search-operations';
import { executeSearch, updateLocationIfNeeded } 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 @@ -43,8 +43,9 @@ export default function SearchBar({
if (!answersActions.state.location.userLocation) {
const autocompleteResponseBeforeSearch = await autocompletePromiseRef.current;
intents = autocompleteResponseBeforeSearch?.inputIntents || [];
await updateLocationIfNeeded(answersActions, intents, geolocationOptions);
}
executeSearchWithIntents(answersActions, isVertical, intents, geolocationOptions);
executeSearch(answersActions, isVertical);
}

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

const universalResultsFilterConfig = {
show: true
Expand All @@ -23,8 +27,9 @@ export default function UniversalSearchPage(props: { universalResultsConfig: Uni
let searchIntents: SearchIntent[] = [];
if (!answersActions.state.location.userLocation) {
searchIntents = await getSearchIntents(answersActions, false) || [];
updateLocationIfNeeded(answersActions, searchIntents);
}
executeSearchWithIntents(answersActions, false, searchIntents);
executeSearch(answersActions, false);
};
executeQuery();
}, [answersActions]);
Expand Down
9 changes: 7 additions & 2 deletions sample-app/src/pages/VerticalSearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { StandardCard } from '../components/cards/StandardCard';
import { useLayoutEffect } from 'react';
import { useAnswersActions } from '@yext/answers-headless-react';
import { SearchIntent } from '@yext/answers-headless';
import { executeSearchWithIntents, getSearchIntents } from '../utils/search-operations';
import {
executeSearch,
getSearchIntents,
updateLocationIfNeeded
} from '../utils/search-operations';

const countryFilterOptions = [
{
Expand Down Expand Up @@ -74,8 +78,9 @@ export default function VerticalSearchPage(props: {
let searchIntents: SearchIntent[] = [];
if (!answersActions.state.location.userLocation) {
searchIntents = await getSearchIntents(answersActions, true) || [];
await updateLocationIfNeeded(answersActions, searchIntents);
}
executeSearchWithIntents(answersActions, true, searchIntents);
executeSearch(answersActions, true);
};
executeQuery();
}, [answersActions, props.verticalKey]);
Expand Down
10 changes: 3 additions & 7 deletions sample-app/src/utils/search-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ const defaultGeolocationOptions: PositionOptions = {
};

/**
* 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.
* 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 executeSearchWithIntents(
export async function updateLocationIfNeeded(
answersActions: AnswersActions,
isVertical: boolean,
intents: SearchIntent[],
geolocationOptions?: PositionOptions
) {
Expand All @@ -29,7 +27,6 @@ export async function executeSearchWithIntents(
console.error(e);
}
}
executeSearch(answersActions, isVertical);
}

/**
Expand All @@ -39,7 +36,6 @@ export async function executeSearch(answersActions: AnswersActions, isVertical:
isVertical
? answersActions.executeVerticalQuery()
: answersActions.executeUniversalQuery();
return;
}

/**
Expand Down

0 comments on commit 4f0ea01

Please sign in to comment.