Skip to content

Commit

Permalink
DirectAnswer Component (#59)
Browse files Browse the repository at this point in the history
- Added DirectAnswer component to display featured snippet and field value direct answer in universal and vertical search page.

J=SLAP-1725
TEST=manual

search for 'bryan phone number' and 'where/who is joe exotic' and see that both snippet and filterValue direct answer appears in universal and vertical page.
  • Loading branch information
yen-tt committed Nov 24, 2021
1 parent fefc014 commit 187cf6b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sample-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions sample-app/src/components/DirectAnswer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { useAnswersState } from "@yext/answers-headless-react";
import { DirectAnswerType } from '@yext/answers-headless';
import renderWithHighlighting from './utils/renderWithHighlighting';
import classNames from "classnames";
import '../sass/DirectAnswer.scss';

interface DirectAnswerProps {
cssClasses?: {
container?: string,
containerLoading?: string,
title?: string,
content?: string,
description?: string
}
}

const defaultCSSClasses = {
container: 'DirectAnswer',
containerLoading: 'DirectAnswer--loading',
title: 'DirectAnswer__title',
content: 'DirectAnswer__content',
description: 'DirectAnswer__contentDescription'
};

export default function DirectAnswer(props: DirectAnswerProps): JSX.Element | null {
const directAnswerResult = useAnswersState(state => state.directAnswer.result);
const isLoading = useAnswersState(state => state.searchStatus.isLoading);
if (!directAnswerResult) {
return null;
}
const { cssClasses: customCssClasses } = props;
const cssClasses = Object.assign(defaultCSSClasses, customCssClasses);
const containerCssClasses = classNames(cssClasses.container, { [cssClasses.containerLoading]: isLoading });

return (
<div className={containerCssClasses}>
<div className={cssClasses.title}>
{directAnswerResult.type === DirectAnswerType.FeaturedSnippet
? directAnswerResult.value
: `${directAnswerResult.entityName} / ${directAnswerResult.fieldName}`}
</div>
<div className={cssClasses.content}>
<div className={cssClasses.description}>
{directAnswerResult.type === DirectAnswerType.FeaturedSnippet
? renderWithHighlighting(directAnswerResult.snippet)
: directAnswerResult.value}
</div>
{directAnswerResult.relatedResult.link
&& <a href={directAnswerResult.relatedResult.link}>View More</a>}
</div>
</div>
)
}
2 changes: 2 additions & 0 deletions sample-app/src/pages/UniversalSearchPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UniversalResults from '../components/UniversalResults';
import DirectAnswer from '../components/DirectAnswer';
import { useLayoutEffect } from 'react';
import { useAnswersActions } from '@yext/answers-headless-react';
import { SearchIntent } from '@yext/answers-headless';
Expand Down Expand Up @@ -36,6 +37,7 @@ export default function UniversalSearchPage(props: { universalResultsConfig: Uni

return (
<div className='UniversalSearchPage'>
<DirectAnswer />
<UniversalResults
appliedFiltersConfig={universalResultsFilterConfig}
verticalConfigs={universalResultsConfig}
Expand Down
2 changes: 2 additions & 0 deletions sample-app/src/pages/VerticalSearchPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ResultsCount from '../components/ResultsCount';
import AlternativeVerticals from '../components/AlternativeVerticals';
import DecoratedAppliedFilters from '../components/DecoratedAppliedFilters';
import DirectAnswer from '../components/DirectAnswer';
import StaticFilters from '../components/StaticFilters';
import VerticalResults from '../components/VerticalResults';
import SpellCheck from '../components/SpellCheck';
Expand Down Expand Up @@ -108,6 +109,7 @@ export default function VerticalSearchPage(props: {
/>
</div>
<div className='end'>
<DirectAnswer />
<ResultsCount />
<DecoratedAppliedFilters
showFieldNames={true}
Expand Down
9 changes: 9 additions & 0 deletions sample-app/src/sass/DirectAnswer.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DirectAnswer {

border: 1px solid #dcdcdc;
padding: 10px;

&--loading {
opacity: 0.5;
}
}

0 comments on commit 187cf6b

Please sign in to comment.