Skip to content

Commit

Permalink
fix(blog): fix blog not displaying
Browse files Browse the repository at this point in the history
  • Loading branch information
abedzantout committed Sep 20, 2019
1 parent 9cfe698 commit df205a5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
58 changes: 30 additions & 28 deletions core/contentful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,34 @@ export async function getBlogPostEntries(
}
}

export function getPostBySlug(slug) {
return fetchPostBySlug(slug)
.then((entries: ContentfulCollection<any>) => {
if (entries.items.length) {
const content: { sys: any; fields: any } = entries.items[0];

const author = {
name: content.fields.author.fields.name,
title: content.fields.author.fields.title,
slug: content.fields.author.fields.slug,
image: content.fields.author.fields.image.fields.file.url
};

return {
id: content.sys.id,
slug: content.fields.slug,
body: content.fields.body,
title: content.fields.title,
description: content.fields.description,
heroImage: { url: content.fields.heroImage.fields.file.url },
author: { ...author, id: content.fields.author.sys.id },
publishedAt: content.fields.publishDate
? new Date(content.fields.publishDate)
: new Date(content.sys.createdAt)
};
}
})
.catch(err => null);
export async function getPostBySlug(slug) {
try {
const content: any = await fetchPostBySlug(slug);

const entry: { sys: any; fields: any } = content.items[0];

const author = {
name: entry.fields.author.fields.name,
title: entry.fields.author.fields.title,
company: entry.fields.author.fields.company,
shortBio: entry.fields.author.fields.shortBio
};

const article = {
id: entry.sys.id,
slug: entry.fields.slug,
body: entry.fields.body,
title: entry.fields.title,
description: entry.fields.description,
heroImage: { url: entry.fields.heroImage.fields.file.url },
author: { ...author, id: entry.fields.author.sys.id },
publishedAt: entry.fields.publishDate
? new Date(entry.fields.publishDate)
: new Date(entry.sys.createdAt)
};

return article;
} catch (error) {
console.error(error);
}
}
17 changes: 10 additions & 7 deletions pages/post/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,40 @@ import React from 'react';
import './styles.css';
import ReactMarkdown from 'react-markdown';

import Layout from '../../shared/components/layout';
import Layout from '../../shared/components/layout/layout.component';
import { getPostBySlug } from '../../core/contentful';
import { defaultMetaTags } from '../../core/constants';

import { BlogPost } from '../../interfaces/post';


type Props = {
article: BlogPost;
};
const PostPage: NextPage = (props: Props) => {
console.log(props.article)
return (
<Layout meta={defaultMetaTags}>
<div className="post-container">
<div className="post-container" id="post-container">
<div className="post-header">
<h1>{props.article.title}</h1>
<div className="author">
<p>Written by {props.article.author.name}</p>
</div>
</div>
<ReactMarkdown className="markdown" source={props.article.body}/>
<ReactMarkdown className="markdown" source={props.article.body} />

</div>
</Layout>
);
};

PostPage.getInitialProps = async ({query}) => {

const {post} = query;
PostPage.getInitialProps = async ({ query }) => {

const { post } = query;
const article = await getPostBySlug(post);
return {article};

return { article };
};

export default PostPage;

0 comments on commit df205a5

Please sign in to comment.