File size: 10,653 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
import React, { useRef } from 'react';
import {
InstantSearch,
HierarchicalMenu,
RefinementList,
SortBy,
Pagination,
ClearRefinements,
Highlight,
Hits,
HitsPerPage,
Panel,
Configure,
SearchBox,
Snippet,
ToggleRefinement,
} from 'react-instantsearch-dom';
import algoliasearch from 'algoliasearch/lite';
import {
ClearFiltersMobile,
PriceSlider,
NoResults,
Ratings,
ResultsNumberMobile,
SaveFiltersMobile,
} from './widgets';
import withURLSync from './URLSync';
import { formatNumber } from './utils';
import './Theme.css';
import './App.css';
import './App.mobile.css';
import './widgets/Pagination.css';
import AlgoliaSvg from './AlgoliaSvg';
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);
const Hit = ({ hit }) => (
<article className="hit">
<header className="hit-image-container">
<img src={hit.image} alt={hit.name} className="hit-image" />
</header>
<div className="hit-info-container">
<p className="hit-category">{hit.categories[0]}</p>
<h1>
<Highlight attribute="name" tagName="mark" hit={hit} />
</h1>
<p className="hit-description">
<Snippet attribute="description" tagName="mark" hit={hit} />
</p>
<footer>
<p>
<span className="hit-em">$</span>{' '}
<strong>{formatNumber(hit.price)}</strong>{' '}
<span className="hit-em hit-rating">
<svg
xmlns="http://www.w3.org/2000/svg"
width="8"
height="8"
viewBox="0 0 16 16"
>
<path
fill="#e2a400"
fillRule="evenodd"
d="M10.472 5.008L16 5.816l-4 3.896.944 5.504L8 12.616l-4.944 2.6L4 9.712 0 5.816l5.528-.808L8 0z"
/>
</svg>{' '}
{hit.rating}
</span>
</p>
</footer>
</div>
</article>
);
const App = (props) => {
const containerRef = useRef(null);
const headerRef = useRef(null);
function openFilters() {
document.body.classList.add('filtering');
window.scrollTo(0, 0);
window.addEventListener('keyup', onKeyUp);
window.addEventListener('click', onClick);
}
function closeFilters() {
document.body.classList.remove('filtering');
containerRef.current.scrollIntoView();
window.removeEventListener('keyup', onKeyUp);
window.removeEventListener('click', onClick);
}
function onKeyUp(event) {
if (event.key !== 'Escape') {
return;
}
closeFilters();
}
function onClick(event) {
if (event.target !== headerRef.current) {
return;
}
closeFilters();
}
return (
<InstantSearch
searchClient={searchClient}
indexName="instant_search"
searchState={props.searchState}
createURL={props.createURL}
onSearchStateChange={props.onSearchStateChange}
>
<header className="header" ref={headerRef}>
<p className="header-logo">
<AlgoliaSvg />
</p>
<p className="header-title">Stop looking for an item — find it.</p>
<SearchBox
translations={{
placeholder: 'Product, brand, color, …',
}}
submit={
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 18 18"
>
<g
fill="none"
fillRule="evenodd"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="1.67"
transform="translate(1 1)"
>
<circle cx="7.11" cy="7.11" r="7.11" />
<path d="M16 16l-3.87-3.87" />
</g>
</svg>
}
/>
</header>
<Configure
attributesToSnippet={['description:10']}
snippetEllipsisText="…"
removeWordsIfNoResults="allOptional"
/>
<main className="container" ref={containerRef}>
<div className="container-wrapper">
<section className="container-filters" onKeyUp={onKeyUp}>
<div className="container-header">
<h2>Filters</h2>
<div className="clear-filters" data-layout="desktop">
<ClearRefinements
translations={{
reset: (
<>
<svg
xmlns="http://www.w3.org/2000/svg"
width="11"
height="11"
viewBox="0 0 11 11"
>
<g fill="none" fillRule="evenodd" opacity=".4">
<path d="M0 0h11v11H0z" />
<path
fill="#000"
fillRule="nonzero"
d="M8.26 2.75a3.896 3.896 0 1 0 1.102 3.262l.007-.056a.49.49 0 0 1 .485-.456c.253 0 .451.206.437.457 0 0 .012-.109-.006.061a4.813 4.813 0 1 1-1.348-3.887v-.987a.458.458 0 1 1 .917.002v2.062a.459.459 0 0 1-.459.459H7.334a.458.458 0 1 1-.002-.917h.928z"
/>
</g>
</svg>
Clear filters
</>
),
}}
/>
</div>
<div className="clear-filters" data-layout="mobile">
<ResultsNumberMobile />
</div>
</div>
<div className="container-body">
<Panel header="Category">
<HierarchicalMenu
attributes={[
'hierarchicalCategories.lvl0',
'hierarchicalCategories.lvl1',
]}
/>
</Panel>
<Panel header="Brands">
<RefinementList
attribute="brand"
searchable={true}
translations={{
placeholder: 'Search for brands…',
}}
/>
</Panel>
<Panel header="Price">
<PriceSlider attribute="price" />
</Panel>
<Panel header="Free shipping">
<ToggleRefinement
attribute="free_shipping"
label="Display only items with free shipping"
value={true}
/>
</Panel>
<Panel header="Ratings">
<Ratings attribute="rating" />
</Panel>
</div>
</section>
<footer className="container-filters-footer" data-layout="mobile">
<div className="container-filters-footer-button-wrapper">
<ClearFiltersMobile containerRef={containerRef} />
</div>
<div className="container-filters-footer-button-wrapper">
<SaveFiltersMobile onClick={closeFilters} />
</div>
</footer>
</div>
<section className="container-results">
<header className="container-header container-options">
<SortBy
className="container-option"
defaultRefinement="instant_search"
items={[
{
label: 'Sort by featured',
value: 'instant_search',
},
{
label: 'Price ascending',
value: 'instant_search_price_asc',
},
{
label: 'Price descending',
value: 'instant_search_price_desc',
},
]}
/>
<HitsPerPage
className="container-option"
items={[
{
label: '16 hits per page',
value: 16,
},
{
label: '32 hits per page',
value: 32,
},
{
label: '64 hits per page',
value: 64,
},
]}
defaultRefinement={16}
/>
</header>
<Hits hitComponent={Hit} />
<NoResults />
<footer className="container-footer">
<Pagination
padding={2}
showFirst={false}
showLast={false}
translations={{
previous: (
<svg
xmlns="http://www.w3.org/2000/svg"
width="10"
height="10"
viewBox="0 0 10 10"
>
<g
fill="none"
fillRule="evenodd"
stroke="#000"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="1.143"
>
<path d="M9 5H1M5 9L1 5l4-4" />
</g>
</svg>
),
next: (
<svg
xmlns="http://www.w3.org/2000/svg"
width="10"
height="10"
viewBox="0 0 10 10"
>
<g
fill="none"
fillRule="evenodd"
stroke="#000"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="1.143"
>
<path d="M1 5h8M5 9l4-4-4-4" />
</g>
</svg>
),
}}
/>
</footer>
</section>
</main>
<aside data-layout="mobile">
<button
className="filters-button"
data-action="open-overlay"
onClick={openFilters}
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 14">
<path
d="M15 1H1l5.6 6.3v4.37L9.4 13V7.3z"
stroke="#fff"
strokeWidth="1.29"
fill="none"
fillRule="evenodd"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
Filters
</button>
</aside>
</InstantSearch>
);
};
export default withURLSync(App);
|