/
index.js
60 lines (53 loc) · 1.54 KB
/
index.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
import { Head, App } from '../components'
import {
findResultsState,
indexName,
searchClient,
} from '../components/instantsearch'
import { Component } from 'react'
import Router from 'next/router'
import qs from 'qs'
const updateAfter = 700
const searchStateToUrl = (searchState) =>
searchState ? `${window.location.pathname}?${qs.stringify(searchState)}` : ''
export default class Home extends Component {
constructor(props) {
super(props)
this.onSearchStateChange = this.onSearchStateChange.bind(this)
}
onSearchStateChange = (searchState) => {
clearTimeout(this.debouncedSetState)
this.debouncedSetState = setTimeout(() => {
const href = searchStateToUrl(searchState)
Router.push(href, href, {
shallow: true,
})
}, updateAfter)
this.setState({ searchState })
}
async componentDidMount() {
this.setState({
searchState: qs.parse(window.location.search.slice(1)),
resultsState: await findResultsState(App, { indexName, searchClient }),
})
}
UNSAFE_componentWillReceiveProps() {
this.setState({ searchState: qs.parse(window.location.search.slice(1)) })
}
render() {
return (
<div>
<Head title="Home" />
<div>
{this.state && this.state.resultsState && this.state.searchState && (
<App
resultsState={this.state.resultsState}
onSearchStateChange={this.onSearchStateChange}
searchState={this.state.searchState}
/>
)}
</div>
</div>
)
}
}