1
0
mirror of https://github.com/kremalicious/blog.git synced 2025-02-14 21:10:25 +01:00
blog/src/components/Search/SearchResults.jsx

83 lines
2.2 KiB
React
Raw Normal View History

2018-11-18 19:34:55 +01:00
import React, { PureComponent } from 'react'
2018-08-28 23:28:42 +02:00
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
2018-11-18 19:34:55 +01:00
import { graphql, StaticQuery } from 'gatsby'
2018-08-28 23:28:42 +02:00
import Container from '../atoms/Container'
2018-11-18 19:34:55 +01:00
import PostTeaser from '../Post/PostTeaser'
2018-11-18 19:54:25 +01:00
import SearchResultsEmpty from './SearchResultsEmpty'
2018-08-28 23:28:42 +02:00
import styles from './SearchResults.module.scss'
2018-11-18 19:34:55 +01:00
const query = graphql`
query {
allMarkdownRemark {
edges {
node {
id
frontmatter {
title
image {
childImageSharp {
...ImageFluidThumb
}
}
}
fields {
slug
}
}
}
}
}
`
export default class SearchResults extends PureComponent {
static propTypes = {
results: PropTypes.array.isRequired,
searchQuery: PropTypes.string.isRequired,
toggleSearch: PropTypes.func.isRequired
}
render() {
const { searchQuery, results, toggleSearch } = this.props
return (
<StaticQuery
query={query}
render={data => {
const posts = data.allMarkdownRemark.edges
// creating portal to break out of DOM node we're in
// and render the results in content container
return ReactDOM.createPortal(
<div className={styles.searchResults}>
<Container>
{results.length > 0 ? (
<ul>
{results.map(page =>
posts
.filter(post => post.node.fields.slug === page.slug)
.map(({ node }) => (
<PostTeaser
key={page.slug}
post={node}
toggleSearch={toggleSearch}
/>
))
)}
</ul>
) : (
2018-11-18 19:54:25 +01:00
<SearchResultsEmpty
searchQuery={searchQuery}
results={results}
/>
2018-11-18 19:34:55 +01:00
)}
</Container>
</div>,
document.getElementById('document')
)
}}
/>
)
}
}