Add page object with top-level routes
This commit is contained in:
parent
687b378a8c
commit
e69c47e2c4
3 changed files with 82 additions and 21 deletions
23
src/App.js
23
src/App.js
|
|
@ -16,6 +16,7 @@ import { Header } from './Header';
|
||||||
import { NavList, TagNav } from './Nav';
|
import { NavList, TagNav } from './Nav';
|
||||||
import { NotFound } from './NotFound';
|
import { NotFound } from './NotFound';
|
||||||
import { Post, Posts } from './Post';
|
import { Post, Posts } from './Post';
|
||||||
|
import { Page } from './Page';
|
||||||
|
|
||||||
|
|
||||||
class App extends Component {
|
class App extends Component {
|
||||||
|
|
@ -40,13 +41,14 @@ class App extends Component {
|
||||||
<Column width="eight">
|
<Column width="eight">
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route exact path="/" component={Posts} />
|
<Route exact path="/" component={Posts} />
|
||||||
{ /* Page routes */ }
|
|
||||||
<Route path="/about" component={About} />
|
|
||||||
|
|
||||||
{ /* Post routes */ }
|
{ /* Post routes */ }
|
||||||
<Route path="/page/:page" component={Posts} />
|
<Route path="/page/:page" component={Posts} />
|
||||||
<Route path="/:year/:month/:slug" component={Post} />
|
<Route path="/:year/:month/:slug" component={Post} />
|
||||||
|
|
||||||
|
{ /* Page routes */ }
|
||||||
|
<Route path="/:slug" component={Page} />
|
||||||
|
|
||||||
{ /* 404 fallback */ }
|
{ /* 404 fallback */ }
|
||||||
<Route component={NotFound} />
|
<Route component={NotFound} />
|
||||||
</Switch>
|
</Switch>
|
||||||
|
|
@ -85,22 +87,5 @@ class ScrollToTop extends Component {
|
||||||
}
|
}
|
||||||
ScrollToTop = withRouter(ScrollToTop);
|
ScrollToTop = withRouter(ScrollToTop);
|
||||||
|
|
||||||
// Dummy About page just for playing around with ReactRouter
|
|
||||||
class About extends Component {
|
|
||||||
render () {
|
|
||||||
return (
|
|
||||||
<article>
|
|
||||||
<header>
|
|
||||||
<h1>About me</h1>
|
|
||||||
</header>
|
|
||||||
<section>
|
|
||||||
<p>
|
|
||||||
Wowzers, this is an about page!
|
|
||||||
</p>
|
|
||||||
</section>
|
|
||||||
</article>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,6 @@ class Article extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
meta() {
|
meta() {
|
||||||
// TODO: Fill this in with real metadata.
|
|
||||||
// Also should this be hidden for pages? That will require some CSS tweaks
|
// Also should this be hidden for pages? That will require some CSS tweaks
|
||||||
let time_text = "Posted"
|
let time_text = "Posted"
|
||||||
if (this.props.is_page) {
|
if (this.props.is_page) {
|
||||||
|
|
@ -65,7 +64,7 @@ class Article extends Component {
|
||||||
|
|
||||||
tags() {
|
tags() {
|
||||||
let tags = this.props.article.tags;
|
let tags = this.props.article.tags;
|
||||||
if (tags.length === 0) {
|
if (!tags || tags.length === 0) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
77
src/Page.js
Normal file
77
src/Page.js
Normal file
|
|
@ -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 <NotFound />
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.state.page) {
|
||||||
|
return (
|
||||||
|
<article>
|
||||||
|
<header>
|
||||||
|
<h1>{ this.state.page.title }</h1>
|
||||||
|
</header>
|
||||||
|
<section>
|
||||||
|
<Markdown source={ this.state.page.body } />
|
||||||
|
</section>
|
||||||
|
</article>
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return <p></p>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { Page };
|
||||||
Loading…
Add table
Add a link
Reference in a new issue