|
1 | 1 | import React from 'react'; |
2 | 2 | import styled from 'styled-components'; |
3 | 3 | import { InstantSearch, SearchBox, PoweredBy } from 'react-instantsearch/dom'; |
| 4 | +import qs from 'qs'; |
4 | 5 |
|
5 | 6 | import Title from 'app/components/text/Title'; |
6 | 7 | import MaxWidth from 'app/components/flex/MaxWidth'; |
@@ -38,37 +39,65 @@ const SEARCHABLE_THINGS = [ |
38 | 39 | 'github repository', |
39 | 40 | ]; |
40 | 41 |
|
| 42 | +const updateAfter = 700; |
| 43 | + |
41 | 44 | const getRandomSearch = () => |
42 | 45 | SEARCHABLE_THINGS[Math.floor(Math.random() * SEARCHABLE_THINGS.length)]; |
43 | 46 |
|
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 | +} |
0 commit comments