Tag routes, complete with pagination!

This commit is contained in:
Nick Pegg 2017-10-11 20:14:32 -07:00
parent 89d0ac2a86
commit bfa8c704d7
2 changed files with 24 additions and 8 deletions

View file

@ -42,7 +42,10 @@ class App extends Component {
<Route exact path="/" component={Posts} /> <Route exact path="/" component={Posts} />
{ /* Post routes */ } { /* Post routes */ }
<Route path="/page/:page" component={Posts} /> <Route exact path="/page/:page" component={Posts} />
<Route exact path="/tag/:tag" component={Posts} />
<Route exact path="/tag/:tag/page/:page" component={Posts} />
<Route path="/:year/:month/:slug" component={Post} /> <Route path="/:year/:month/:slug" component={Post} />
{ /* Page routes */ } { /* Page routes */ }

View file

@ -78,7 +78,7 @@ class Posts extends Component {
// Number of Articles per page // Number of Articles per page
// TODO: Pull this from a configuration somehow // TODO: Pull this from a configuration somehow
this.perPage = 4; this.perPage = 5;
this.params = props.match.params; this.params = props.match.params;
this.state = { this.state = {
@ -97,33 +97,46 @@ class Posts extends Component {
fetch('/site.json') fetch('/site.json')
.then((resp) => resp.json()) .then((resp) => resp.json())
.then((blob) => { .then((blob) => {
let posts = blob.posts;
let offset = 0; let offset = 0;
let page = parseInt(this.params.page, 10); let page = parseInt(this.params.page, 10);
if (!page) { if (!page) {
page = 0; page = 0;
} }
offset = page * this.perPage; offset = page * this.perPage;
let posts = blob.posts.slice(offset, offset + this.perPage);
// if tag is set, filter posts down to that of that tag
if (this.params.tag) {
posts = posts.filter(p => (p.tags.includes(this.params.tag)));
}
// Grab the slice of posts for this page
let post_slice = posts.slice(offset, offset + this.perPage);
// Check to see if we have more posts after these // Check to see if we have more posts after these
let nextOffset = (page + 1) * this.perPage; let nextOffset = (page + 1) * this.perPage;
let nextPosts = blob.posts.slice(nextOffset, let nextPosts = posts.slice(nextOffset,
nextOffset + this.perPage); nextOffset + this.perPage);
let hasMore = nextPosts.length > 0; let hasMore = nextPosts.length > 0;
this.updateHistoryNav(page, hasMore); this.updateHistoryNav(page, hasMore);
this.setState({posts: posts}); this.setState({posts: post_slice});
}); });
} }
updateHistoryNav(page, hasMore) { updateHistoryNav(page, hasMore) {
let prefix = "";
if (this.params.tag) {
prefix = "/tag/" + this.params.tag;
}
// Update prev state // Update prev state
let prev = null; let prev = null;
if (page === 1) { if (page === 1) {
prev = "/"; prev = "/";
} else if (page > 1) { } else if (page > 1) {
prev = "/page/" + (page - 1); prev = prefix + "/page/" + (page - 1);
} else { } else {
prev = null; prev = null;
} }
@ -131,7 +144,7 @@ class Posts extends Component {
// Update next state // Update next state
let next = null; let next = null;
if (hasMore) { if (hasMore) {
next = "/page/" + (page + 1); next = prefix + "/page/" + (page + 1);
} else { } else {
next = null; next = null;
} }