1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-06-29 00:58:00 +02:00
blog/src/templates/Posts.jsx

114 lines
3.0 KiB
React
Raw Normal View History

2018-07-22 04:27:37 +02:00
import React, { Fragment } from 'react'
2018-07-17 23:33:55 +02:00
import PropTypes from 'prop-types'
import { Link, graphql } from 'gatsby'
2018-07-18 00:24:11 +02:00
import Layout from '../components/Layout'
2018-10-11 20:35:10 +02:00
import PostImage from '../components/Post/PostImage'
import PostTitle from '../components/Post/PostTitle'
import PostLead from '../components/Post/PostLead'
import PostContent from '../components/Post/PostContent'
import PostMore from '../components/Post/PostMore'
import PostLinkActions from '../components/Post/PostLinkActions'
2018-09-24 23:50:39 +02:00
import SEO from '../components/atoms/SEO'
2018-09-06 20:27:18 +02:00
import Pagination from '../components/molecules/Pagination'
2018-09-06 22:28:28 +02:00
import Featured from '../components/molecules/Featured'
2018-09-04 02:57:39 +02:00
import styles from './Posts.module.scss'
2018-07-17 23:33:55 +02:00
2018-09-07 00:36:08 +02:00
const Posts = ({ data, location, pageContext }) => {
2018-07-17 23:33:55 +02:00
const edges = data.allMarkdownRemark.edges
2018-09-29 20:43:10 +02:00
const { tag, currentPageNumber, numPages } = pageContext
2018-07-22 04:27:37 +02:00
2018-09-07 00:36:08 +02:00
const PostsList = edges.map(({ node }) => {
2018-07-22 04:27:37 +02:00
const { type, linkurl, title, image } = node.frontmatter
const { slug } = node.fields
return (
2018-09-07 00:36:08 +02:00
<article className={styles.hentry} key={node.id}>
2018-09-07 01:00:13 +02:00
{type !== 'photo' && (
<PostTitle type={type} slug={slug} linkurl={linkurl} title={title} />
)}
2018-07-22 04:27:37 +02:00
{image && (
2018-12-01 23:54:45 +01:00
<Link to={slug} title={title}>
2018-09-25 23:16:05 +02:00
<PostImage
title={type === 'photo' ? title : null}
fluid={image.childImageSharp.fluid}
alt={title}
/>
</Link>
2018-07-22 04:27:37 +02:00
)}
2018-09-27 22:33:01 +02:00
{type === 'post' && (
<Fragment>
<PostLead post={node} index />
<PostMore to={slug}>Continue Reading</PostMore>
</Fragment>
)}
2018-07-22 04:27:37 +02:00
{type === 'link' && (
<Fragment>
<PostContent post={node} />
<PostLinkActions slug={slug} linkurl={linkurl} />
</Fragment>
)}
</article>
)
})
2018-09-06 20:27:18 +02:00
return (
<Layout location={location}>
2018-09-24 23:50:39 +02:00
<SEO />
2018-09-07 00:36:08 +02:00
{location.pathname === '/' && <Featured />}
2018-09-29 20:43:10 +02:00
{tag && <h1 className={styles.archiveTitle}>#{tag}</h1>}
{currentPageNumber > 1 && (
2018-09-07 00:36:08 +02:00
<h1
className={styles.archiveTitle}
2018-09-29 20:43:10 +02:00
>{`Page ${currentPageNumber} / ${numPages}`}</h1>
2018-09-07 00:36:08 +02:00
)}
{PostsList}
2018-09-06 20:27:18 +02:00
<Pagination pageContext={pageContext} />
</Layout>
)
2018-07-17 23:33:55 +02:00
}
2018-09-07 00:36:08 +02:00
Posts.propTypes = {
2018-07-20 15:52:17 +02:00
data: PropTypes.object.isRequired,
2018-09-04 02:57:39 +02:00
pageContext: PropTypes.object.isRequired,
2018-07-20 15:52:17 +02:00
location: PropTypes.object.isRequired
2018-07-17 23:33:55 +02:00
}
2018-09-07 00:36:08 +02:00
export default Posts
2018-07-17 23:33:55 +02:00
export const indexQuery = graphql`
2018-09-07 00:36:08 +02:00
query($tag: String, $skip: Int, $limit: Int) {
2018-09-04 02:57:39 +02:00
allMarkdownRemark(
2018-09-07 00:36:08 +02:00
filter: { frontmatter: { tags: { eq: $tag } } }
2018-09-04 02:57:39 +02:00
sort: { order: DESC, fields: [fields___date] }
skip: $skip
limit: $limit
) {
2018-07-17 23:33:55 +02:00
edges {
node {
id
2018-07-22 04:27:37 +02:00
html
2018-07-17 23:33:55 +02:00
excerpt(pruneLength: 250)
frontmatter {
title
2018-07-22 04:27:37 +02:00
type
linkurl
image {
childImageSharp {
...ImageFluid
}
}
2018-09-07 00:36:08 +02:00
tags
2018-07-17 23:33:55 +02:00
}
fields {
slug
date(formatString: "MMMM DD, YYYY")
}
}
}
}
}
`