From 9bc95d967046ac0b7c25b711297198684a58d6d9 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sat, 11 Nov 2017 20:00:36 +0100 Subject: [PATCH] Post API is fully working --- src/components/Blog.js | 12 +++++------- src/components/Post.js | 14 +++++++++++--- src/utils/api.js | 26 ++++++++++++++++++-------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/components/Blog.js b/src/components/Blog.js index 0befe32..d4bf888 100644 --- a/src/components/Blog.js +++ b/src/components/Blog.js @@ -31,15 +31,13 @@ export default class Blog extends Component { ) return ( -
-
-

Blog

- -
- {posts} -
+
+

Blog

+
+ {posts}
+
); } diff --git a/src/components/Post.js b/src/components/Post.js index 0dee37d..b1a3f4f 100644 --- a/src/components/Post.js +++ b/src/components/Post.js @@ -2,10 +2,18 @@ import React, {Component} from 'react'; export default class Post extends Component { render() { + + if (this.props.isLoading) { + return ( +

Loading

+ ); + } + return ( -
-
-

Post

+
+

{this.props.post.title}

+

{this.props.post.published}

+
) diff --git a/src/utils/api.js b/src/utils/api.js index 12c249e..d7877e5 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -1,22 +1,32 @@ const data = require('./data.json'); const api = require('express').Router(); +const fs = require('fs'); +const path = require('path'); api.get('/blog', (req, res) => { res.json(data.posts); }); api.get('/post/:postname', (req, res) => { + const postname = req.params.postname; const post = data.posts.find((el) => { - el.filename === req.params.postname + return el.filename === postname }); - if (post) { - res.json(post); - } else { - res.json({ - error: 404 - }); - } + const renderPath = path.join(process.cwd(), '/renders', postname + '.html'); + fs.readFile(renderPath, 'utf8', (err, data) => { + if (err) { + res.json({ + error: 404 + }); + } else { + res.json({ + published: post.published, + title: post.title, + body: data, + }); + } + }); });