From e69c47e2c42fdbc90a324eaea14bdf5a327f751b Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Sat, 7 Oct 2017 19:33:16 -0700 Subject: [PATCH] Add page object with top-level routes --- src/App.js | 23 +++------------ src/Article.js | 3 +- src/Page.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 21 deletions(-) create mode 100644 src/Page.js diff --git a/src/App.js b/src/App.js index fb1fe28..5892004 100644 --- a/src/App.js +++ b/src/App.js @@ -16,6 +16,7 @@ import { Header } from './Header'; import { NavList, TagNav } from './Nav'; import { NotFound } from './NotFound'; import { Post, Posts } from './Post'; +import { Page } from './Page'; class App extends Component { @@ -40,13 +41,14 @@ class App extends Component { - { /* Page routes */ } - { /* Post routes */ } + { /* Page routes */ } + + { /* 404 fallback */ } @@ -85,22 +87,5 @@ class ScrollToTop extends Component { } ScrollToTop = withRouter(ScrollToTop); -// Dummy About page just for playing around with ReactRouter -class About extends Component { - render () { - return ( -
-
-

About me

-
-
-

- Wowzers, this is an about page! -

-
-
- ) - } -} export default App; diff --git a/src/Article.js b/src/Article.js index 0183e40..1c20a6a 100644 --- a/src/Article.js +++ b/src/Article.js @@ -48,7 +48,6 @@ class Article extends Component { } meta() { - // TODO: Fill this in with real metadata. // Also should this be hidden for pages? That will require some CSS tweaks let time_text = "Posted" if (this.props.is_page) { @@ -65,7 +64,7 @@ class Article extends Component { tags() { let tags = this.props.article.tags; - if (tags.length === 0) { + if (!tags || tags.length === 0) { return; } else { return ( diff --git a/src/Page.js b/src/Page.js new file mode 100644 index 0000000..796a472 --- /dev/null +++ b/src/Page.js @@ -0,0 +1,77 @@ +import React, { Component } from 'react'; +import Markdown from 'react-markdown'; +import slugify from 'slugify'; + +import { NotFound } from './NotFound'; + + +class Page extends Component { + constructor(props) { + super(props); + this.props = props; + this.params = props.match.params; + + this.state = { + page: null, + notFound: false, + } + } + + fetchPage() { + fetch('/site.json') + .then(resp => resp.json()) + .then(blob => { + for (let page of blob.pages) { + let slug = page.slug; + if (!slug) { + slug = slugify(page.title).toLowerCase(); + } + + if (slug === this.params.slug) { + this.setState({ + page: page, + notFound: false, + }) + return + } + } + + this.setState({ + page: null, + notFound: true, + }); + }); + } + + componentDidMount() { + this.fetchPage(); + } + + componentWillReceiveProps(props) { + this.params = props.match.params; + this.fetchPage(); + } + + render() { + if (this.state.notFound) { + return + } + + if (this.state.page) { + return ( +
+
+

{ this.state.page.title }

+
+
+ +
+
+ ) + } else { + return

+ } + } +} + +export { Page };