/
App.js
212 lines (197 loc) · 6.04 KB
/
App.js
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
/* eslint react/prop-types: 0 */
import React from 'react';
import {createConnector} from 'react-instantsearch';
import {
InstantSearch,
HierarchicalMenu,
RefinementList,
SortBy,
Stats,
ClearAll,
StarRating,
RangeInput,
Highlight,
Panel,
Configure,
} from 'react-instantsearch/dom';
import {
connectSearchBox,
connectRefinementList,
connectInfiniteHits,
} from 'react-instantsearch/connectors';
import 'react-instantsearch-theme-algolia/style.scss';
export default function App() {
return (
<InstantSearch
appId="latency"
apiKey="6be0576ff61c053d5f9a3225e2a90f76"
indexName="ikea"
>
<Configure hitsPerPage={16} />
<Header />
<div className="content-wrapper">
<Facets />
<CustomResults />
</div>
</InstantSearch>
);
}
const Header = () =>
<header className="content-wrapper">
<a href="https://community.algolia.com/instantsearch.js/react" className="is-logo"><img
src="https://res.cloudinary.com/hilnmyskv/image/upload/w_100,h_100,dpr_2.0//v1461180087/logo-instantsearchjs-avatar.png"
width={40}/></a>
<a href="./" className="logo">aeki</a>
<ConnectedSearchBox/>
</header>;
const Facets = () =>
<aside>
<ClearAll
translations={{
reset: 'Clear all filters',
}}
/>
<section className="facet-wrapper">
<div className="facet-category-title facet">Show results for</div>
<HierarchicalMenu
key="categories"
attributes={[
'category',
'sub_category',
'sub_sub_category',
]}
/>
</section>
<section className="facet-wrapper">
<div className="facet-category-title facet">RefineBy</div>
<Panel title="Type">
<RefinementList attributeName="type" operator="or" limitMin={5} withSearchBox/>
</Panel>
<Panel
title="Materials">
<RefinementList attributeName="materials" operator="or" limitMin={5} withSearchBox/>
</Panel>
<ConnectedColorRefinementList attributeName="colors" operator="or"/>
<Panel title="Rating">
<StarRating attributeName="rating" max={5}/>
</Panel>
<Panel title="Price">
<RangeInput key="price_input" attributeName="price" />
</Panel>
</section>
<div className="thank-you">Data courtesy of <a href="http://www.ikea.com/">ikea.com</a></div>
</aside>;
const CustomSearchBox = ({currentRefinement, refine}) =>
<div className="input-group">
<input type="text"
value={currentRefinement}
onChange={e => refine(e.target.value)}
autoComplete="off"
className="form-control"
id="q"/>
<span className="input-group-btn">
<button className="btn btn-default"><i className="fa fa-search"></i></button>
</span>
</div>
;
const ColorItem = ({item, createURL, refine}) => {
const active = item.isRefined ? 'checked' : '';
return (
<a
className={`${active} facet-color`}
href={createURL(item.value)}
onClick={e => {
e.preventDefault();
refine(item.value);
}}
data-facet-value={item.label}
>
</a>
);
};
const CustomColorRefinementList = ({items, refine, createURL}) =>
items.length > 0 ? <div>
<h5 className={'ais-Panel__Title'}>Colors</h5>
{items.map(item =>
<ColorItem
key={item.label}
item={item}
refine={refine}
createURL={createURL}
/>
)}
</div> : null;
function CustomHits({hits, refine, hasMore}) {
const loadMoreButton = hasMore ?
<button onClick={refine} className="btn btn-primary btn-block">Load more</button> :
<button disabled className="btn btn-primary btn-block">Load more</button>;
return (
<main id="hits">
{hits.map((hit, idx) =>
<Hit item={hit} key={idx}/>
)}
{loadMoreButton}
</main>
);
}
const Hit = ({item}) => {
const icons = [];
for (let i = 0; i < 5; i++) {
const suffix = i >= item.rating ? '_empty' : '';
icons.push(<label key={i} label className={`ais-StarRating__ratingIcon${suffix}`}></label>);
}
return (
<article className="hit">
<div className="product-picture-wrapper">
<div className="product-picture"><img src={`${item.image}`}/></div>
</div>
<div className="product-desc-wrapper">
<div className="product-name"><Highlight attributeName="name" hit={item} /></div>
<div className="product-type"><Highlight attributeName="type" hit={item} /></div>
<div className="ais-StarRating__ratingLink">
{icons}
<div className="product-price">${item.price}</div>
</div>
</div>
</article>);
};
const CustomResults = createConnector({
displayName: 'CustomResults',
getProvidedProps(props, searchState, searchResults) {
const noResults = searchResults.results ? searchResults.results.nbHits === 0 : false;
return {query: searchState.query, noResults};
},
})(({noResults, query}) => {
if (noResults) {
return (
<div className="results-wrapper">
<div className="no-results">
No results found matching <span className="query">{query}</span>
</div>
</div>
);
} else {
return (
<div className="results-wrapper">
<section id="results-topbar">
<div className="sort-by">
<label>Sort by</label>
<SortBy
items={[
{value: 'ikea', label: 'Featured'},
{value: 'ikea_price_asc', label: 'Price asc.'},
{value: 'ikea_price_desc', label: 'Price desc.'},
]}
defaultRefinement="ikea"
/>
</div>
<Stats />
</section>
<ConnectedHits/>
</div>
);
}
});
const ConnectedSearchBox = connectSearchBox(CustomSearchBox);
const ConnectedColorRefinementList = connectRefinementList(CustomColorRefinementList);
const ConnectedHits = connectInfiniteHits(CustomHits);