Merge pull request #21 from captbaritone/refactor

Replace `access` with `lodash.get`, make more declarative.
This commit is contained in:
Kyle Mathews 2017-02-02 09:39:23 -08:00 committed by GitHub
commit a313342afa
2 changed files with 18 additions and 23 deletions

View file

@ -15,7 +15,6 @@
"react-helmet": "^3.2.2", "react-helmet": "^3.2.2",
"react-responsive-grid": "^0.3.3", "react-responsive-grid": "^0.3.3",
"react-typography": "^0.15.0", "react-typography": "^0.15.0",
"safe-access": "^0.1.0",
"typography": "^0.15.0", "typography": "^0.15.0",
"typography-theme-wordpress-2016": "^0.15.0", "typography-theme-wordpress-2016": "^0.15.0",
"underscore.string": "^3.2.3" "underscore.string": "^3.2.3"

View file

@ -1,37 +1,22 @@
import React from 'react' import React from 'react'
import { Link } from 'react-router' import { Link } from 'react-router'
import sortBy from 'lodash/sortBy' import sortBy from 'lodash/sortBy'
import get from 'lodash/get'
import { prefixLink } from 'gatsby-helpers' import { prefixLink } from 'gatsby-helpers'
import { rhythm } from 'utils/typography' import { rhythm } from 'utils/typography'
import Helmet from "react-helmet" import Helmet from "react-helmet"
import access from 'safe-access'
import { config } from 'config' import { config } from 'config'
import include from 'underscore.string/include' import include from 'underscore.string/include'
import Bio from 'components/Bio' import Bio from 'components/Bio'
class BlogIndex extends React.Component { class BlogIndex extends React.Component {
render () { render () {
const pageLinks = []
// Sort pages. // Sort pages.
const sortedPages = sortBy(this.props.route.pages, (page) => const sortedPages = sortBy(this.props.route.pages, 'data.date')
access(page, 'data.date')
).reverse()
sortedPages.forEach((page) => {
// Posts are those with md extension that are not 404 pages OR have a date (meaning they're a react component post). // Posts are those with md extension that are not 404 pages OR have a date (meaning they're a react component post).
if (access(page, 'file.ext') === 'md' && !include(page.path, '/404') || access(page, 'data.date')) { const visiblePages = sortedPages.filter(page => (
const title = access(page, 'data.title') || page.path get(page, 'file.ext') === 'md' && !include(page.path, '/404') || get(page, 'data.date')
pageLinks.push( ))
<li
key={page.path}
style={{
marginBottom: rhythm(1/4),
}}
>
<Link style={{boxShadow: 'none'}} to={prefixLink(page.path)}>{title}</Link>
</li>
)
}
})
return ( return (
<div> <div>
<Helmet <Helmet
@ -43,7 +28,18 @@ class BlogIndex extends React.Component {
/> />
<Bio /> <Bio />
<ul> <ul>
{pageLinks} {visiblePages.map((page) => (
<li
key={page.path}
style={{
marginBottom: rhythm(1/4),
}}
>
<Link style={{boxShadow: 'none'}} to={prefixLink(page.path)}>
{get(page, 'data.title', page.path)}
</Link>
</li>
))}
</ul> </ul>
</div> </div>
) )