Skip to content

Commit 426d671

Browse files
author
Ives van Hoorne
committed
1 parent 9a392a6 commit 426d671

File tree

3 files changed

+65
-31
lines changed

3 files changed

+65
-31
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"moment": "^2.18.1",
101101
"normalize.css": "^5.0.0",
102102
"normalizr": "^3.2.3",
103+
"qs": "^6.5.0",
103104
"rc-slider": "^8.2.0",
104105
"react": "^15.6.1",
105106
"react-addons-css-transition-group": "^15.6.0",

src/app/pages/Search/index.js

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import styled from 'styled-components';
33
import { InstantSearch, SearchBox, PoweredBy } from 'react-instantsearch/dom';
4+
import qs from 'qs';
45

56
import Title from 'app/components/text/Title';
67
import MaxWidth from 'app/components/flex/MaxWidth';
@@ -38,37 +39,65 @@ const SEARCHABLE_THINGS = [
3839
'github repository',
3940
];
4041

42+
const updateAfter = 700;
43+
4144
const getRandomSearch = () =>
4245
SEARCHABLE_THINGS[Math.floor(Math.random() * SEARCHABLE_THINGS.length)];
4346

44-
export default () => {
45-
document.title = 'Search - CodeSandbox';
46-
return (
47-
<MaxWidth>
48-
<Margin vertical={1.5} horizontal={1.5}>
49-
<Navigation title="Search" />
50-
<Content>
51-
<MaxWidth width={1024}>
52-
<InstantSearch
53-
appId={ALGOLIA_APPLICATION_ID}
54-
apiKey={ALGOLIA_API_KEY}
55-
indexName={ALGOLIA_DEFAULT_INDEX}
56-
>
57-
<StyledTitle>Sandbox Search</StyledTitle>
58-
<PoweredBy />
59-
<SearchBox
60-
translations={{
61-
placeholder: `Search for a ${getRandomSearch()}...`,
62-
}}
63-
/>
64-
<Row alignItems="flex-start">
65-
<Results />
66-
<Filters />
67-
</Row>
68-
</InstantSearch>
69-
</MaxWidth>
70-
</Content>
71-
</Margin>
72-
</MaxWidth>
73-
);
74-
};
47+
const createURL = state => `?${qs.stringify(state)}`;
48+
49+
const searchStateToUrl = (props, searchState) =>
50+
searchState ? `${props.location.pathname}${createURL(searchState)}` : '';
51+
52+
export default class Search extends React.PureComponent {
53+
constructor(props) {
54+
super(props);
55+
this.state = { searchState: qs.parse(props.location.search.slice(1)) };
56+
}
57+
58+
onSearchStateChange = searchState => {
59+
clearTimeout(this.debouncedSetState);
60+
this.debouncedSetState = setTimeout(() => {
61+
this.props.history.push(
62+
searchStateToUrl(this.props, searchState),
63+
searchState,
64+
);
65+
}, updateAfter);
66+
this.setState({ searchState });
67+
};
68+
69+
render() {
70+
document.title = 'Search - CodeSandbox';
71+
return (
72+
<MaxWidth>
73+
<Margin vertical={1.5} horizontal={1.5}>
74+
<Navigation title="Search" />
75+
<Content>
76+
<MaxWidth width={1024}>
77+
<InstantSearch
78+
appId={ALGOLIA_APPLICATION_ID}
79+
apiKey={ALGOLIA_API_KEY}
80+
indexName={ALGOLIA_DEFAULT_INDEX}
81+
searchState={this.state.searchState}
82+
onSearchStateChange={this.onSearchStateChange.bind(this)}
83+
createURL={createURL}
84+
>
85+
<StyledTitle>Sandbox Search</StyledTitle>
86+
<PoweredBy />
87+
<SearchBox
88+
translations={{
89+
placeholder: `Search for a ${getRandomSearch()}...`,
90+
}}
91+
/>
92+
<Row alignItems="flex-start">
93+
<Results />
94+
<Filters />
95+
</Row>
96+
</InstantSearch>
97+
</MaxWidth>
98+
</Content>
99+
</Margin>
100+
</MaxWidth>
101+
);
102+
}
103+
}

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6249,6 +6249,10 @@ [email protected], qs@^6.2.1, qs@~6.4.0:
62496249
version "6.4.0"
62506250
resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233"
62516251

6252+
qs@^6.5.0:
6253+
version "6.5.0"
6254+
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.0.tgz#8d04954d364def3efc55b5a0793e1e2c8b1e6e49"
6255+
62526256
query-string@^4.1.0:
62536257
version "4.3.4"
62546258
resolved "https://registry.yarnpkg.com/query-string/-/query-string-4.3.4.tgz#bbb693b9ca915c232515b228b1a02b609043dbeb"

0 commit comments

Comments
 (0)