forked from vtex-apps/search-result
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchResult.js
249 lines (225 loc) · 6.59 KB
/
SearchResult.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
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
import React, { Component } from 'react'
import { Spinner } from 'vtex.styleguide'
import { ExtensionPoint, withRuntimeContext } from 'vtex.render-runtime'
import { compose, prop, last, isEmpty } from 'ramda'
import { generateBlockClass } from '@vtex/css-handles'
import LoadingOverlay from './LoadingOverlay'
import { searchResultPropTypes } from '../constants/propTypes'
import LayoutModeSwitcher, { LAYOUT_MODE } from './LayoutModeSwitcher'
import getFilters from '../utils/getFilters'
import styles from '../searchResult.css'
/**
* Search Result Component.
*/
class SearchResult extends Component {
static propTypes = searchResultPropTypes
static defaultProps = {
mobileLayout: {
mode1: LAYOUT_MODE[0].value,
mode2: LAYOUT_MODE[1].value,
},
}
state = {
mobileLayoutMode: this.props.mobileLayout.mode1,
showLoadingAsOverlay: false,
// The definitions bellow are required because
// on SSR the getDerivedStateFromProps isn't called
products: this.props.products,
recordsFiltered: this.props.recordsFiltered,
facetRecordsFiltered: this.props.facetRecordsFiltered,
brands: this.props.brands,
map: this.props.map,
params: this.props.params,
priceRange: this.props.priceRange,
priceRanges: this.props.priceRanges,
specificationFilters: this.props.specificationFilters,
tree: this.props.tree,
hiddenFacets: this.props.hiddenFacets,
}
handleMobileLayoutChange = e => {
e.preventDefault()
const modes = [this.props.mobileLayout.mode1, this.props.mobileLayout.mode2]
const modeIndex = (modes.indexOf(this.state.mobileLayoutMode) + 1) % 2
const currentMode = modes[modeIndex]
this.setState({
mobileLayoutMode: currentMode,
})
}
static getDerivedStateFromProps(props) {
// Do not use the props when the query is loading
// so we can show the previous products when the
// overlay is on the screen
if (!props.loading) {
if (!props.mobileLayout.mode1 && !props.mobileLayout.mode2) {
props.mobileLayout.mode1 = LAYOUT_MODE[0].value
props.mobileLayout.mode2 = LAYOUT_MODE[1].value
}
const {
products,
recordsFiltered,
facetRecordsFiltered,
brands,
map,
params,
priceRange,
priceRanges,
specificationFilters,
tree,
hiddenFacets,
} = props
return {
products,
recordsFiltered,
facetRecordsFiltered,
brands,
map,
params,
priceRange,
priceRanges,
specificationFilters,
tree,
hiddenFacets,
}
}
return null
}
componentDidMount() {
if (!this.props.loading) {
this.setState({
showLoadingAsOverlay: true,
})
}
}
componentDidUpdate(prevProps) {
if (prevProps.loading && !this.props.loading) {
this.setState({
showLoadingAsOverlay: true,
})
}
}
render() {
const {
children,
breadcrumbsProps,
loading,
fetchMoreLoading,
summary,
orderBy,
runtime: {
hints: { mobile },
},
} = this.props
const {
mobileLayoutMode,
recordsFiltered,
facetRecordsFiltered,
products = [],
brands,
map,
params,
priceRange,
priceRanges,
specificationFilters,
tree,
hiddenFacets,
showLoadingAsOverlay,
} = this.state
const term =
params && params.term ? decodeURIComponent(params.term) : undefined
// only show the not found page if the reason for it
// isn't because of some applied filter, but that we
// *really* don't have any products to show for the
// current query
if (facetRecordsFiltered === 0 && !loading) {
return <ExtensionPoint id="not-found" term={term} />
}
const hideFacets = !map || !map.length
const showLoading = loading && !fetchMoreLoading
const showContentLoader = showLoading && !showLoadingAsOverlay
const filters = getFilters({
specificationFilters,
priceRanges,
brands,
hiddenFacets,
})
const showCategories =
hiddenFacets &&
hiddenFacets.categories === false &&
tree &&
tree.length > 0
const showFacets = showCategories || (!hideFacets && !isEmpty(filters))
return (
<LoadingOverlay loading={showLoading && showLoadingAsOverlay}>
<div
className={`${generateBlockClass(
styles.container,
this.props.blockClass
)} w-100 mw9`}
>
{!mobile && (
<div className={styles.breadcrumb}>
<ExtensionPoint id="breadcrumb" {...breadcrumbsProps} />
</div>
)}
<ExtensionPoint id="search-title" breadcrumb={breadcrumbsProps.breadcrumb} />
{showFacets && (
<ExtensionPoint
id="filter-navigator"
brands={brands}
showFilters={!!map}
params={params}
priceRange={priceRange}
priceRanges={priceRanges}
specificationFilters={specificationFilters}
tree={tree}
loading={showContentLoader}
filters={filters}
hiddenFacets={hiddenFacets}
/>
)}
<ExtensionPoint
id="total-products"
recordsFiltered={recordsFiltered}
/>
<div className={styles.resultGallery}>
{showContentLoader ? (
<div className="w-100 flex justify-center">
<div className="w3 ma0">
<Spinner />
</div>
</div>
) : products.length > 0 ? (
<ExtensionPoint
id="gallery"
products={products}
summary={summary}
className="bn"
mobileLayoutMode={mobileLayoutMode}
showingFacets={showFacets}
/>
) : (
<div className={styles.gallery}>
<ExtensionPoint id="not-found" />
</div>
)}
{children}
</div>
<ExtensionPoint id="order-by" orderBy={orderBy} />
{mobile && (
<div
className={`${
styles.switch
} flex justify-center items-center`}
>
<LayoutModeSwitcher
activeMode={mobileLayoutMode}
onChange={this.handleMobileLayoutChange}
/>
</div>
)}
</div>
</LoadingOverlay>
)
}
}
export default withRuntimeContext(SearchResult)