import React, { Fragment, PureComponent } from 'react' import PropTypes from 'prop-types' import { Link, graphql, StaticQuery } from 'gatsby' import Image from '../atoms/Image' import styles from './RelatedPosts.module.scss' const query = graphql` query { allMarkdownRemark(sort: { order: DESC, fields: [fields___date] }) { edges { node { id frontmatter { title type linkurl tags image { childImageSharp { ...ImageFluidThumb } } } fields { slug date(formatString: "MMMM DD, YYYY") } } } } } ` const postsWithDataFilter = (postsArray, key, valuesToFind) => { const newArray = postsArray.filter(post => { const frontmatterKey = post.node.frontmatter[key] if ( frontmatterKey !== null && frontmatterKey.some(r => valuesToFind.includes(r)) ) { return post } }) return newArray } const PostItem = ({ post }) => { return (
  • {post.node.frontmatter.image ? ( {post.node.frontmatter.title}

    {post.node.frontmatter.title}

    ) : (

    {post.node.frontmatter.title}

    )}
  • ) } PostItem.propTypes = { post: PropTypes.object.isRequired } class RelatedPosts extends PureComponent { shufflePosts = () => { this.forceUpdate() } render() { return ( { const posts = data.allMarkdownRemark.edges const filteredPosts = postsWithDataFilter( posts, 'tags', this.props.tags ) return ( ) }} /> ) } } RelatedPosts.propTypes = { tags: PropTypes.array.isRequired } export default RelatedPosts