From 3b137f99ae5dfc6e3b36e7b350e60d5331f9c7cb Mon Sep 17 00:00:00 2001 From: Fabien Motte Date: Wed, 2 Mar 2022 14:47:06 +0100 Subject: [PATCH] chore(two-columns-layout-example): add faq plugin --- examples/two-columns-layout/src/app.tsx | 4 + .../src/plugins/faqPlugin.tsx | 77 +++++++++++++++++++ .../two-columns-layout/src/types/FaqHit.ts | 9 +++ .../two-columns-layout/src/types/index.ts | 1 + examples/two-columns-layout/style.css | 8 +- 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 examples/two-columns-layout/src/plugins/faqPlugin.tsx create mode 100644 examples/two-columns-layout/src/types/FaqHit.ts diff --git a/examples/two-columns-layout/src/app.tsx b/examples/two-columns-layout/src/app.tsx index e8c5e7e62b..575c36a1a1 100644 --- a/examples/two-columns-layout/src/app.tsx +++ b/examples/two-columns-layout/src/app.tsx @@ -4,6 +4,7 @@ import { h, render } from 'preact'; import { brandsPlugin } from './plugins/brandsPlugin'; import { categoriesPlugin } from './plugins/categoriesPlugin'; +import { faqPlugin } from './plugins/faqPlugin'; import { productsPlugin } from './plugins/productsPlugin'; import { querySuggestionsPlugin } from './plugins/querySuggestionsPlugin'; import { recentSearchesPlugin } from './plugins/recentSearchesPlugin'; @@ -21,6 +22,7 @@ autocomplete({ querySuggestionsPlugin, categoriesPlugin, brandsPlugin, + faqPlugin, productsPlugin, ], render({ elements }, root) { @@ -29,6 +31,7 @@ autocomplete({ querySuggestionsPlugin: querySuggestions, categoriesPlugin: categories, brandsPlugin: brands, + faqPlugin: faq, productsPlugin: products, } = elements; @@ -46,6 +49,7 @@ autocomplete({ {querySuggestions} {categories} {brands} + {faq}
{productsSection}
diff --git a/examples/two-columns-layout/src/plugins/faqPlugin.tsx b/examples/two-columns-layout/src/plugins/faqPlugin.tsx new file mode 100644 index 0000000000..21b5d2e9a4 --- /dev/null +++ b/examples/two-columns-layout/src/plugins/faqPlugin.tsx @@ -0,0 +1,77 @@ +/** @jsx h */ +import { + AutocompleteComponents, + AutocompletePlugin, + getAlgoliaResults, +} from '@algolia/autocomplete-js'; +import { h } from 'preact'; + +import { Breadcrumb } from '../components/Breadcrumb'; +import { ALGOLIA_FAQ_INDEX_NAME } from '../constants'; +import { searchClient } from '../searchClient'; +import { FaqHit } from '../types'; + +export const faqPlugin: AutocompletePlugin = { + getSources({ query }) { + if (!query) { + return []; + } + + return [ + { + sourceId: 'faqPlugin', + getItems() { + return getAlgoliaResults({ + searchClient, + queries: [ + { + indexName: ALGOLIA_FAQ_INDEX_NAME, + query, + params: { + hitsPerPage: 1, + }, + }, + ], + }); + }, + templates: { + item({ item, components }) { + return ; + }, + }, + }, + ]; + }, +}; + +type FaqItemProps = { + hit: FaqHit; + components: AutocompleteComponents; +}; + +const FaqItem = ({ hit, components }: FaqItemProps) => { + const breadcrumbItems = hit.list_categories; + + return ( +
+
+
+ + + +
+
+
+ +
+
+
+ +
+ ); +}; diff --git a/examples/two-columns-layout/src/types/FaqHit.ts b/examples/two-columns-layout/src/types/FaqHit.ts new file mode 100644 index 0000000000..e1c10eaf47 --- /dev/null +++ b/examples/two-columns-layout/src/types/FaqHit.ts @@ -0,0 +1,9 @@ +import { Hit } from '@algolia/client-search'; + +type FaqRecord = { + list_categories: string[]; + title: string; + description: string; +}; + +export type FaqHit = Hit; diff --git a/examples/two-columns-layout/src/types/index.ts b/examples/two-columns-layout/src/types/index.ts index c52d13f255..3a328528ea 100644 --- a/examples/two-columns-layout/src/types/index.ts +++ b/examples/two-columns-layout/src/types/index.ts @@ -1,3 +1,4 @@ export * from './ProductHit'; export * from './CategoryHit'; export * from './BrandHit'; +export * from './FaqHit'; diff --git a/examples/two-columns-layout/style.css b/examples/two-columns-layout/style.css index 36edb22fc4..d847f4d8b0 100644 --- a/examples/two-columns-layout/style.css +++ b/examples/two-columns-layout/style.css @@ -78,15 +78,17 @@ body { transform-origin: center bottom; } -/* Category */ -.aa-ItemWrapper.aa-CategoryItem { +/* Category and faq */ +.aa-ItemWrapper.aa-CategoryItem, +.aa-ItemWrapper.aa-FaqItem { display: flex; flex-direction: column; align-items: flex-start; gap: 0; } -.aa-CategoryItem .aa-Breadcrumb { +.aa-CategoryItem .aa-Breadcrumb, +.aa-FaqItem .aa-Breadcrumb { margin-left: calc(var(--aa-icon-size) + var(--aa-spacing)); }