File size: 4,169 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import React from 'react';
import {
InstantSearch,
Hits,
HierarchicalMenu,
RefinementList,
SearchBox,
SortBy,
Stats,
Pagination,
Panel,
ClearRefinements,
RatingMenu,
RangeInput,
Highlight,
Configure,
connectStateResults,
} from 'react-instantsearch-dom';
import algoliasearch from 'algoliasearch/lite';
import withURLSync from './URLSync';
import './App.css';
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);
const App = (props) => (
<InstantSearch
searchClient={searchClient}
indexName="instant_search"
searchState={props.searchState}
createURL={props.createURL}
onSearchStateChange={props.onSearchStateChange}
>
<Configure hitsPerPage={16} />
<Header />
<div className="content-wrapper">
<Facets />
<CustomResults />
</div>
</InstantSearch>
);
const Header = () => (
<header className="content-wrapper header">
<a
href="https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/react/"
className="is-logo"
>
<img
alt="React InstantSearch"
className="logo"
src="https://res.cloudinary.com/hilnmyskv/image/upload/w_100,h_100,dpr_2.0//v1461180087/logo-instantsearchjs-avatar.png"
width={40}
height={40}
/>
</a>
<SearchBox />
</header>
);
const Facets = () => (
<aside>
<ClearRefinements
translations={{
reset: 'Clear all filters',
}}
/>
<Panel header="Categories">
<HierarchicalMenu
attributes={[
'hierarchicalCategories.lvl0',
'hierarchicalCategories.lvl1',
'hierarchicalCategories.lvl2',
]}
/>
</Panel>
<Panel header="Categories">
<RefinementList attribute="categories" operator="or" limit={10} />
</Panel>
<Panel header="Rating">
<RatingMenu attribute="rating" max={5} />
</Panel>
<Panel header="Price">
<RangeInput key="price_input" attribute="price" />
</Panel>
<div className="thank-you">
Data courtesy of <a href="https://developer.bestbuy.com/">Best Buy</a>
</div>
</aside>
);
const Hit = ({ hit }) => {
const icons = [];
for (let i = 0; i < 5; i++) {
const suffixClassName = i >= hit.rating ? '--empty' : '';
const suffixXlink = i >= hit.rating ? 'Empty' : '';
icons.push(
<svg
key={i}
className={`ais-RatingMenu-starIcon ais-RatingMenu-starIcon${suffixClassName}`}
aria-hidden="true"
width="24"
height="24"
>
<use xlinkHref={`#ais-RatingMenu-star${suffixXlink}Symbol`} />
</svg>
);
}
return (
<article className="hit">
<div className="product-desc-wrapper">
<div className="product-name">
<Highlight attribute="name" hit={hit} />
</div>
<div className="product-type">
<Highlight attribute="type" hit={hit} />
</div>
<div className="product-footer">
<div className="ais-RatingMenu-link">{icons}</div>
<div className="product-price">${hit.price}</div>
</div>
</div>
</article>
);
};
const CustomResults = connectStateResults(({ searchState, searchResults }) => (
<div className="results-wrapper">
<section className="results-topbar">
<Stats />
<div className="sort-by">
<label>Sort by</label>
<SortBy
items={[
{ value: 'instant_search', label: 'Featured' },
{ value: 'instant_search_price_asc', label: 'Price asc.' },
{ value: 'instant_search_price_desc', label: 'Price desc.' },
]}
defaultRefinement="instant_search"
/>
</div>
</section>
{searchResults && searchResults.nbHits ? (
<div>
<Hits hitComponent={Hit} />
<footer>
<Pagination showLast={true} />
</footer>
</div>
) : (
<div className="no-results">
No results found matching "
<span className="query">{searchState.query}</span>
"
</div>
)}
</div>
));
export default withURLSync(App);
|