/
SearchResults.js
86 lines (78 loc) · 2.26 KB
/
SearchResults.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
// @flow
import * as React from 'react'
import {withRouter} from 'next/router'
import theme from '../theme'
import {themeGet} from 'styled-system'
import {connectInfiniteHits, connectStateResults} from 'react-instantsearch/connectors'
import title from 'title'
import isPresent from 'is-present'
import Grid from '../components/Grid'
import Box from '../components/Box'
import Heading from '../components/Heading'
import LargeText from '../components/LargeText'
import Text from '../components/Text'
import Button from '../components/Button'
import ProductPreview from './ProductPreview'
const ProductGrid = Grid.extend`
grid-auto-rows: max-content;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
grid-gap: ${theme.space[4]};
z-index: 1;
width: 100%;
justify-content: center;
@media (min-width: ${themeGet('breakpoints.1')}) {
max-width: 65rem;
}
@media (min-width: ${themeGet('breakpoints.2')}) {
grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
max-width: 76rem;
}
`
type Props = {
hits: Object[],
hasMore: boolean,
refine: any => any,
router: Object,
searchState: Object,
searchResults: Object,
}
const SearchResults = ({
hits,
hasMore,
refine,
router: {
query: {ssr, q},
},
searchResults,
}: Props) => {
return (
<>
{ssr && (
<Box area="heading" mt={3}>
<Heading is="h1" fontSize={[4, 3]} fontStyle="italic" color="black">
List of All Certified Gluten-Free {title(q)}
</Heading>
<LargeText color="grays.3">
All {searchResults && `${searchResults.nbHits} `}products have been certified
gluten-free as of January 2017 or later.
</LargeText>
</Box>
)}
{isPresent(searchResults.query) && (
<Box area="main" alignItems="center">
<ProductGrid mb={4}>
{hits.map(hit => <ProductPreview product={hit} key={hit.objectID} />)}
</ProductGrid>
{hasMore ? (
<Button onClick={refine} alignSelf="center">
Load More
</Button>
) : (
<Text alignSelf="center">THE END</Text>
)}
</Box>
)}
</>
)
}
export default withRouter(connectInfiniteHits(connectStateResults(SearchResults)))