Skip to content

Commit

Permalink
Merge pull request #49 from saharshy29/blog-cms
Browse files Browse the repository at this point in the history
Integrate UI with GhostCMS
  • Loading branch information
Saharsh Yeruva authored Sep 2, 2020
2 parents b396b73 + 56f2432 commit ab7eb95
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 139 deletions.
21 changes: 19 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,24 @@
"react/jsx-props-no-spreading": "off",
"jsx-a11y/anchor-is-valid": "off",
"react/no-unescaped-entities": "off",
"react/jsx-indent": "warn"
"react/jsx-indent": "warn",
"no-return-await": "off",
"no-console": "warn",
"array-callback-return": "off",
"no-param-reassign": "off",
"no-plusplus": "off",
"prefer-const": "warn",
"prefer-destructuring": [
"warn",
{
"array": false,
"object": true
},
{
"enforceForRenamedProperties": false
}
],
"react/no-danger": "off"
},
"ignorePatterns": [
".DS_Store",
Expand All @@ -50,4 +67,4 @@
".github/workflows/lint.yml",
"./tsconfig.json"
]
}
}
36 changes: 25 additions & 11 deletions components/Blog/Featured.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import { useContext } from 'react';

import { FaBookmark } from 'react-icons/fa';
import { features } from 'process';
import featured from './Featured.module.scss';
import FeaturedPost from './FeaturedPost';
import featured from './Featured.module.scss';

import { PostsContext } from '../../pages/blog/index';

const Featured = () => {
const posts = useContext(PostsContext);
const featuredPosts = posts
.filter((post) => post.featured === true)
.sort(() => 0.5 - Math.random())
.slice(0, 3);

featuredPosts.map((post) => {
post.tagList = post.tags.map((tag) => tag.name).join(' ');
});

return (
<div className={featured.root}>
<div className={featured.wrapper}>
<h2>
<FaBookmark className={featured.icon} /> Featured
</h2>
<div className={featured.posts}>
<div className={featured.post}>
<FeaturedPost />
</div>
<div className={featured.post}>
<FeaturedPost />
</div>
<div className={featured.post}>
<FeaturedPost />
</div>
{featuredPosts.map((post) => (
<div className={featured.post}>
<FeaturedPost
key={post.uuid}
slug={post.slug}
title={post.title}
tags={post.tagList}
/>
</div>
))}
</div>
</div>
</div>
Expand Down
14 changes: 9 additions & 5 deletions components/Blog/FeaturedPost.module.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
@use 'styles/colors';

.root {
font-size: 0.8em;
font-size: 0.8em;

p {
color: colors.$gray;
}
}
p {
color: colors.$gray;
}

h3:hover {
text-decoration: underline;
}
}
16 changes: 11 additions & 5 deletions components/Blog/FeaturedPost.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import Link from 'next/link';

import styles from './FeaturedPost.module.scss';

const FeaturedPost = () => {
const FeaturedPost = (props) => {
return (
<div className={styles.root}>
<h3>Lorem ipsum dolor sit amet.</h3>
<p>#tag 1 #tag2 #tag3</p>
</div>
<Link as={`/blog/${props.slug}`} href="/blog/[slug]">
<a>
<div className={styles.root}>
<h3>{props.title}</h3>
<p>{props.tags}</p>
</div>
</a>
</Link>
);
};

Expand Down
19 changes: 14 additions & 5 deletions components/Blog/PostPreview.module.scss
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
@use 'styles/colors';

.root {
font-size: 0.8em;
max-width: 65ch;
margin: 1.5em auto;
font-size: 0.8em;
max-width: 65ch;
margin: 1.5em auto;

h2:hover {
text-decoration: underline;
}
}

.meta {
color: colors.$gray;
}
color: colors.$gray;
}

.icon {
font-size: 0.8em;
margin-left: 0.25em;
}
33 changes: 17 additions & 16 deletions components/Blog/PostPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import Link from 'next/link';
import { FaExternalLinkAlt } from 'react-icons/fa';
import postPreview from './PostPreview.module.scss';

const PostPreview = () => {
const PostPreview = (props) => {
return (
<div className={postPreview.root}>
<h2>Lorem ipsum dolor sit amet consectetur adipisicing elit.</h2>
<p className={postPreview.meta}>Aug 12 | 5 min | #stuff #things #life</p>
<hr />
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quibusdam
beatae nam repellendus ratione expedita possimus, sint dolore
necessitatibus quisquam natus, nisi laborum magni sequi placeat ab
perferendis, delectus reprehenderit similique ex veritatis illo! Quas
voluptates ad exercitationem temporibus quibusdam, corrupti ratione
dolorem optio alias. Animi adipisci quidem voluptates officiis provident
deleniti officia! Dolor molestiae debitis maxime porro tempora eius
architecto.
</p>
</div>
<Link as={`/blog/${props.slug}`} href="/blog/[slug]">
<a>
<div className={postPreview.root}>
<h2>
{props.title} <FaExternalLinkAlt className={postPreview.icon} />
</h2>
<p className={postPreview.meta}>
{props.pubDate} | {props.readTime} | {props.tags}
</p>
<hr />
<p>{props.excerpt}</p>
</div>
</a>
</Link>
);
};

Expand Down
22 changes: 0 additions & 22 deletions components/Blog/Tags.module.scss

This file was deleted.

24 changes: 0 additions & 24 deletions components/Blog/Tags.tsx

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"start": "next start"
},
"dependencies": {
"@tryghost/content-api": "^1.4.4",
"imagemin-optipng": "^8.0.0",
"next": "9.5.2",
"next-optimized-images": "^2.6.2",
Expand Down
22 changes: 22 additions & 0 deletions pages/api/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import GhostContentAPI from '@tryghost/content-api';

// Create API instance with site credentials
const api = new GhostContentAPI({
url: 'https://shuy.ghost.io',
key: '4b1a5b85b966c4f3072ca8b307', // This shouldn't be publicly exposed, but it's not a big deal since it will not work soon enough, and even now, it does not provide access to much.
version: 'v3',
});

export async function getPosts() {
return await api.posts
.browse({
include: 'tags',
limit: 'all',
})
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
});
}

export default getPosts();
55 changes: 55 additions & 0 deletions pages/blog/[slug].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { GetStaticProps, GetStaticPaths } from 'next';
import { useRouter } from 'next/router';
import Layout from '../../components/Layouts/Layout';
import Copy from '../../components/Copy';

import { getPosts } from '../api/posts';

const Post = (props) => {
const { slug } = useRouter().query;
let post = props.posts.filter((p) => p.slug === slug);
post = post[0];

return (
<Layout>
<h1>{post.title}</h1>
<p>{post.tagList}</p>
<hr />
<Copy>
<div dangerouslySetInnerHTML={{ __html: post.html }} />
</Copy>
</Layout>
);
};

export default Post;

export const getStaticProps = async () => {
const posts = await getPosts();

posts.map((post) => {
post.dateFormatted = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
}).format(new Date(post.published_at));
});

posts.map((post) => {
post.tagList = post.tags.map((tag) => tag.name).join(' ');
});

return {
props: {
posts,
},
};
};

export async function getStaticPaths() {
const posts = await getPosts();
return {
paths: posts?.map((post) => `/blog/${post.slug}`),
fallback: false,
};
}
Loading

0 comments on commit ab7eb95

Please sign in to comment.