|
| 1 | +/** @jsx h */ |
| 2 | +import { autocomplete } from '@algolia/autocomplete-js'; |
| 3 | +import { h, render } from 'preact'; |
| 4 | +import { pipe } from 'ramda'; |
| 5 | + |
| 6 | +import { createFillWith, uniqBy } from './functions'; |
| 7 | +import { articlesPlugin } from './plugins/articlesPlugin'; |
| 8 | +import { brandsPlugin } from './plugins/brandsPlugin'; |
| 9 | +import { categoriesPlugin } from './plugins/categoriesPlugin'; |
| 10 | +import { faqPlugin } from './plugins/faqPlugin'; |
| 11 | +import { popularCategoriesPlugin } from './plugins/popularCategoriesPlugin'; |
| 12 | +import { popularPlugin } from './plugins/popularPlugin'; |
| 13 | +import { productsPlugin } from './plugins/productsPlugin'; |
| 14 | +import { querySuggestionsPlugin } from './plugins/querySuggestionsPlugin'; |
| 15 | +import { quickAccessPlugin } from './plugins/quickAccessPlugin'; |
| 16 | +import { recentSearchesPlugin } from './plugins/recentSearchesPlugin'; |
| 17 | +import { cx, hasSourceActiveItem, isDetached } from './utils'; |
| 18 | + |
| 19 | +import '@algolia/autocomplete-theme-classic'; |
| 20 | + |
| 21 | +const removeDuplicates = uniqBy(({ source, item }) => { |
| 22 | + const sourceIds = ['recentSearchesPlugin', 'querySuggestionsPlugin']; |
| 23 | + if (sourceIds.indexOf(source.sourceId) === -1) { |
| 24 | + return item; |
| 25 | + } |
| 26 | + |
| 27 | + return source.sourceId === 'querySuggestionsPlugin' ? item.query : item.label; |
| 28 | +}); |
| 29 | + |
| 30 | +const fillWith = createFillWith({ |
| 31 | + mainSourceId: 'querySuggestionsPlugin', |
| 32 | + limit: isDetached() ? 6 : 10, |
| 33 | +}); |
| 34 | + |
| 35 | +const combine = pipe(removeDuplicates, fillWith); |
| 36 | + |
| 37 | +autocomplete({ |
| 38 | + container: '#autocomplete', |
| 39 | + placeholder: 'Search products, articles, and FAQs', |
| 40 | + autoFocus: true, |
| 41 | + openOnFocus: true, |
| 42 | + plugins: [ |
| 43 | + recentSearchesPlugin, |
| 44 | + querySuggestionsPlugin, |
| 45 | + categoriesPlugin, |
| 46 | + brandsPlugin, |
| 47 | + faqPlugin, |
| 48 | + productsPlugin, |
| 49 | + articlesPlugin, |
| 50 | + popularPlugin, |
| 51 | + quickAccessPlugin, |
| 52 | + popularCategoriesPlugin, |
| 53 | + ], |
| 54 | + reshape({ sourcesBySourceId, sources, state }) { |
| 55 | + const { |
| 56 | + recentSearchesPlugin: recentSearches, |
| 57 | + querySuggestionsPlugin: querySuggestions, |
| 58 | + categoriesPlugin: categories, |
| 59 | + brandsPlugin: brands, |
| 60 | + faqPlugin: faq, |
| 61 | + popularPlugin: popular, |
| 62 | + popularCategoriesPlugin: popularCategories, |
| 63 | + ...rest |
| 64 | + } = sourcesBySourceId; |
| 65 | + |
| 66 | + const sourceIdsToExclude = ['popularPlugin', 'popularCategoriesPlugin']; |
| 67 | + const shouldDisplayPopularCategories = sources.every((source) => { |
| 68 | + if (sourceIdsToExclude.indexOf(source.sourceId) !== -1) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + return source.getItems().length === 0; |
| 72 | + }); |
| 73 | + |
| 74 | + return [ |
| 75 | + combine(recentSearches, querySuggestions, categories, brands, faq), |
| 76 | + [ |
| 77 | + !state.query && popular, |
| 78 | + ...Object.values(rest), |
| 79 | + shouldDisplayPopularCategories && popularCategories, |
| 80 | + ].filter(Boolean), |
| 81 | + ]; |
| 82 | + }, |
| 83 | + render({ elements, state, Fragment }, root) { |
| 84 | + const { |
| 85 | + recentSearchesPlugin: recentSearches, |
| 86 | + querySuggestionsPlugin: querySuggestions, |
| 87 | + categoriesPlugin: categories, |
| 88 | + brandsPlugin: brands, |
| 89 | + faqPlugin: faq, |
| 90 | + productsPlugin: products, |
| 91 | + articlesPlugin: articles, |
| 92 | + popularPlugin: popular, |
| 93 | + quickAccessPlugin: quickAccess, |
| 94 | + popularCategoriesPlugin: popularCategories, |
| 95 | + } = elements; |
| 96 | + |
| 97 | + const sourceIdsToExclude = ['popularPlugin', 'popularCategoriesPlugin']; |
| 98 | + const hasResults = |
| 99 | + state.collections |
| 100 | + .filter( |
| 101 | + ({ source }) => sourceIdsToExclude.indexOf(source.sourceId) === -1 |
| 102 | + ) |
| 103 | + .reduce((prev, curr) => prev + curr.items.length, 0) > 0; |
| 104 | + |
| 105 | + render( |
| 106 | + <div className="aa-PanelLayout aa-Panel--scrollable"> |
| 107 | + {!hasResults && ( |
| 108 | + <div className="aa-NoResultsQuery"> |
| 109 | + No results for "{state.query}". |
| 110 | + </div> |
| 111 | + )} |
| 112 | + |
| 113 | + <div className="aa-PanelSections"> |
| 114 | + <div className="aa-PanelSection--left"> |
| 115 | + {hasResults ? ( |
| 116 | + (!state.query && recentSearches && ( |
| 117 | + <Fragment> |
| 118 | + <div className="aa-SourceHeader"> |
| 119 | + <span className="aa-SourceHeaderTitle"> |
| 120 | + Recent searches |
| 121 | + </span> |
| 122 | + <div className="aa-SourceHeaderLine" /> |
| 123 | + </div> |
| 124 | + {recentSearches} |
| 125 | + </Fragment> |
| 126 | + )) || |
| 127 | + (state.query && ( |
| 128 | + <Fragment> |
| 129 | + <div className="aa-SourceHeader"> |
| 130 | + <span className="aa-SourceHeaderTitle">Suggestions</span> |
| 131 | + <div className="aa-SourceHeaderLine" /> |
| 132 | + </div> |
| 133 | + |
| 134 | + <div className="aa-PanelSectionSources"> |
| 135 | + {recentSearches} |
| 136 | + {querySuggestions} |
| 137 | + {categories} |
| 138 | + {brands} |
| 139 | + {faq} |
| 140 | + </div> |
| 141 | + </Fragment> |
| 142 | + )) |
| 143 | + ) : ( |
| 144 | + <div className="aa-NoResultsAdvices"> |
| 145 | + <ul className="aa-NoResultsAdvicesList"> |
| 146 | + <li>Double-check your spelling</li> |
| 147 | + <li>Use fewer keywords</li> |
| 148 | + <li>Search for a less specific item</li> |
| 149 | + <li>Try navigate using on the of the popular categories</li> |
| 150 | + </ul> |
| 151 | + </div> |
| 152 | + )} |
| 153 | + |
| 154 | + {!state.query && ( |
| 155 | + <div className="aa-PanelSection--popular">{popular}</div> |
| 156 | + )} |
| 157 | + </div> |
| 158 | + <div className="aa-PanelSection--right"> |
| 159 | + {products && ( |
| 160 | + <div className="aa-PanelSection--products"> |
| 161 | + <div className="aa-PanelSectionSource">{products}</div> |
| 162 | + </div> |
| 163 | + )} |
| 164 | + {articles && ( |
| 165 | + <div className="aa-PanelSection--articles"> |
| 166 | + <div className="aa-PanelSectionSource">{articles}</div> |
| 167 | + </div> |
| 168 | + )} |
| 169 | + |
| 170 | + {quickAccess && ( |
| 171 | + <div |
| 172 | + className={cx( |
| 173 | + 'aa-PanelSection--quickAccess aa-PanelSection--zoomable', |
| 174 | + hasSourceActiveItem('quickAccessPlugin', state) && |
| 175 | + 'aa-PanelSection--active' |
| 176 | + )} |
| 177 | + > |
| 178 | + {quickAccess} |
| 179 | + </div> |
| 180 | + )} |
| 181 | + |
| 182 | + {!hasResults && ( |
| 183 | + <div |
| 184 | + className={cx( |
| 185 | + 'aa-PanelSection--popularCategories aa-PanelSection--zoomable', |
| 186 | + hasSourceActiveItem('popularCategoriesPlugin', state) && |
| 187 | + 'aa-PanelSection--active' |
| 188 | + )} |
| 189 | + > |
| 190 | + {popularCategories} |
| 191 | + </div> |
| 192 | + )} |
| 193 | + </div> |
| 194 | + </div> |
| 195 | + </div>, |
| 196 | + root |
| 197 | + ); |
| 198 | + }, |
| 199 | +}); |
0 commit comments