Skip to content

Commit

Permalink
docs(examples): improve playground types
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Mar 4, 2021
1 parent b397149 commit 6f22e12
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
13 changes: 2 additions & 11 deletions examples/js/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from '@algolia/autocomplete-plugin-algolia-insights';
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
import { Hit } from '@algolia/client-search';
import algoliasearch from 'algoliasearch';
import { h, Fragment } from 'preact';
import insightsClient from 'search-insights';
Expand All @@ -19,15 +18,7 @@ import '@algolia/autocomplete-theme-classic';

import { createCategoriesPlugin } from './categoriesPlugin';
import { shortcutsPlugin } from './shortcutsPlugin';

type Product = {
name: string;
image: string;
description: string;
__autocomplete_indexName: string;
__autocomplete_queryID: string;
};
type ProductHit = Hit<Product>;
import { ProductHit, ProductRecord } from './types';

const appId = 'latency';
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76';
Expand Down Expand Up @@ -78,7 +69,7 @@ autocomplete({
{
sourceId: 'products',
getItems() {
return getAlgoliaHits<Product>({
return getAlgoliaHits<ProductRecord>({
searchClient,
queries: [
{
Expand Down
10 changes: 7 additions & 3 deletions examples/js/categoriesPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ import {
import { SearchClient } from 'algoliasearch/lite';
import { h, Fragment } from 'preact';

type CategoryItem = {
import { Highlighted } from './types';

type CategoryRecord = {
label: string;
count: number;
};

type CategoryHit = Highlighted<CategoryRecord>;

type CreateCategoriesPluginProps = {
searchClient: SearchClient;
};

export function createCategoriesPlugin({
searchClient,
}: CreateCategoriesPluginProps): AutocompletePlugin<CategoryItem, undefined> {
}: CreateCategoriesPluginProps): AutocompletePlugin<CategoryHit, undefined> {
return {
getSources({ query }) {
return [
Expand Down Expand Up @@ -73,7 +77,7 @@ export function createCategoriesPlugin({
</div>
<div className="aa-ItemContent">
<div className="aa-ItemContentTitle">
{highlightHit<CategoryItem>({
{highlightHit<CategoryHit>({
hit: item,
attribute: 'label',
})}
Expand Down
4 changes: 3 additions & 1 deletion examples/js/shortcutsPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ export const shortcutsPlugin: AutocompletePlugin<DarkModeItem, undefined> = {
},
];
},
onSelect({ setIsOpen }) {
onSelect({ setIsOpen, setQuery, refresh }) {
toggleTheme();
setQuery('');
setIsOpen(true);
refresh();
},
templates: {
header({ createElement, Fragment }) {
Expand Down
5 changes: 5 additions & 0 deletions examples/js/types/Highlighted.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { HighlightResult } from '@algolia/client-search';

export type Highlighted<TRecord> = TRecord & {
_highlightResult: HighlightResult<TRecord>;
};
31 changes: 31 additions & 0 deletions examples/js/types/ProductHit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Hit } from '@algolia/client-search';

export type ProductRecord = {
brand: string;
categories: string[];
description: string;
free_shipping: boolean;
hierarchicalCategories: {
lvl0: string;
lvl1?: string;
lvl2?: string;
lvl3?: string;
lvl4?: string;
lvl5?: string;
lvl6?: string;
};
image: string;
name: string;
popularity: number;
price: number;
prince_range: string;
rating: number;
type: string;
};

type WithAutocompleteAnalytics<THit> = THit & {
__autocomplete_indexName: string;
__autocomplete_queryID: string;
};

export type ProductHit = WithAutocompleteAnalytics<Hit<ProductRecord>>;
2 changes: 2 additions & 0 deletions examples/js/types/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Highlighted';
export * from './ProductHit';

0 comments on commit 6f22e12

Please sign in to comment.