Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen Truong committed Nov 22, 2021
1 parent 8b9fc4c commit 7198c9f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
13 changes: 6 additions & 7 deletions sample-app/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import '../sass/Autocomplete.scss';
import LoadingIndicator from './LoadingIndicator';
import { useAutocomplete } from '../hooks/useAutocomplete';
import { useRef } from 'react';
import { AutocompleteResponse } from '@yext/answers-headless';
import { executeSearch, executeSearchWithIntents } from '../utils/search-operations';
import { AutocompleteResponse, SearchIntent } 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 @@ -39,13 +39,12 @@ export default function SearchBar({
const [ autocompleteResponse, executeAutocomplete] = useAutocomplete(isVertical);

async function executeQuery () {
if (answersActions.state.location.userLocation) {
executeSearch(answersActions, isVertical);
} else {
let intents: SearchIntent[] = [];
if (!answersActions.state.location.userLocation) {
const responseToLatestRequest = await responseToLatestRequestRef.current;
const intents = responseToLatestRequest?.inputIntents || [];
executeSearchWithIntents(answersActions, isVertical, intents, geolocationOptions);
intents = responseToLatestRequest?.inputIntents || [];
}
executeSearchWithIntents(answersActions, isVertical, intents, geolocationOptions);
}

function renderSearchButton () {
Expand Down
10 changes: 5 additions & 5 deletions sample-app/src/pages/UniversalSearchPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UniversalResults from '../components/UniversalResults';
import { useLayoutEffect } from 'react';
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';
Expand All @@ -19,12 +20,11 @@ export default function UniversalSearchPage(props: { universalResultsConfig: Uni
})
answersActions.setVerticalKey('');
const executeQuery = async () => {
if(answersActions.state.location.userLocation) {
answersActions.executeUniversalQuery();
} else {
const searchIntents = await getSearchIntents(answersActions, false);
executeSearchWithIntents(answersActions, false, searchIntents || []);
let searchIntents: SearchIntent[] = [];
if (!answersActions.state.location.userLocation) {
searchIntents = await getSearchIntents(answersActions, false) || [];
}
executeSearchWithIntents(answersActions, false, searchIntents);
};
executeQuery();
}, [answersActions]);
Expand Down
10 changes: 5 additions & 5 deletions sample-app/src/pages/VerticalSearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import '../sass/VerticalSearchPage.scss';
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';

const countryFilterOptions = [
Expand Down Expand Up @@ -70,12 +71,11 @@ export default function VerticalSearchPage(props: {
});
answersActions.setVerticalKey(props.verticalKey);
const executeQuery = async () => {
if(answersActions.state.location.userLocation) {
answersActions.executeVerticalQuery();
} else {
const searchIntents = await getSearchIntents(answersActions, true);
executeSearchWithIntents(answersActions, true, searchIntents || []);
let searchIntents: SearchIntent[] = [];
if (!answersActions.state.location.userLocation) {
searchIntents = await getSearchIntents(answersActions, true) || [];
}
executeSearchWithIntents(answersActions, true, searchIntents);
};
executeQuery();
}, [answersActions, props.verticalKey]);
Expand Down

0 comments on commit 7198c9f

Please sign in to comment.