-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create blog page and display blog posts
- Loading branch information
1 parent
4fcda56
commit 4a858be
Showing
16 changed files
with
218 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { useEffect } from 'react'; | ||
|
||
import BlogPost from './components'; | ||
import { sortBlogPosts } from '../../utils'; | ||
|
||
import './style.scss'; | ||
|
||
const Blog = (props) => { | ||
const { | ||
blogPosts, | ||
getAllBlogPosts, | ||
} = props; | ||
|
||
useEffect(() => { | ||
getAllBlogPosts(); | ||
}, [getAllBlogPosts]); | ||
|
||
const sortedBlogPosts = sortBlogPosts(blogPosts); | ||
|
||
return ( | ||
<div className="blog-page-container"> | ||
<div id="overview-text"> | ||
<h1 id="title">Blog</h1> | ||
</div> | ||
{sortedBlogPosts.length > 0 | ||
? sortedBlogPosts.map((post) => <BlogPost post={post} />) | ||
: <div className="blog-page-no-posts">There are no blog posts yet</div>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Blog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
|
||
import { formatPostDates } from '../../../../utils'; | ||
|
||
import './style.scss'; | ||
|
||
const BlogPost = ({ post }) => { | ||
const { | ||
title, | ||
body, | ||
date_created: createdAt, | ||
date_edited: editedAt, | ||
image, | ||
author, | ||
} = post; | ||
|
||
return ( | ||
<div className="blog-page-blog-post-container page-container"> | ||
<div className="blog-page-blog-post-title">{title}</div> | ||
<div className="blog-page-blog-post-author">{`by ${author}`}</div> | ||
{image && <img className="blog-page-blog-post-picture" src={`${global.API_URL}${image}`} alt="whatever" />} | ||
<div className="blog-page-blog-post-body">{body}</div> | ||
<div className="blog-page-blog-post-date">{formatPostDates(createdAt, editedAt)}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default BlogPost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import BlogPost from './component'; | ||
|
||
export default BlogPost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
.blog-page { | ||
&-blog-post { | ||
&-container { | ||
margin: 30px auto; | ||
width: 100%; | ||
box-sizing: border-box; | ||
} | ||
|
||
&-title { | ||
font-size: 24px; | ||
font-weight: 600; | ||
} | ||
|
||
&-author { | ||
margin-bottom: 20px; | ||
margin-top: 6px; | ||
} | ||
|
||
&-picture { | ||
max-width: 100%; | ||
height: auto; | ||
max-height: 500px; | ||
margin: 30px auto 50px; | ||
} | ||
|
||
&-date { | ||
font-style: italic; | ||
font-size: 14px; | ||
margin-top: 30px; | ||
width: 100%; | ||
text-align: right; | ||
} | ||
|
||
&-date, | ||
&-author { | ||
font-style: italic; | ||
color: #73767e; | ||
} | ||
|
||
&-body { | ||
white-space: pre-line; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import BlogPost from './blog-post'; | ||
|
||
export default BlogPost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { connect } from 'react-redux'; | ||
import { getAllBlogPosts } from '../../state/actions/blog'; | ||
import Blog from './component'; | ||
|
||
const mapStateToProps = (state) => { | ||
const { | ||
blog: { | ||
blogPosts, | ||
}, | ||
} = state; | ||
|
||
return { blogPosts }; | ||
}; | ||
|
||
const mapDispatchToProps = (dispatch) => { | ||
return { | ||
getAllBlogPosts: () => dispatch(getAllBlogPosts()), | ||
}; | ||
}; | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(Blog); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.blog-page-container { | ||
max-width: 1000px; | ||
margin: auto; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
|
||
.blog-page-no-posts { | ||
font-size: 26px; | ||
width: 100%; | ||
text-align: center; | ||
color: #73767e; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Sort posts from the newest to the oldest | ||
const sortBlogPosts = (blogPosts) => blogPosts.sort((a, b) => { | ||
const dateA = new Date(a.date_created); | ||
const dateB = new Date(b.date_created); | ||
|
||
return dateB - dateA; | ||
}); | ||
|
||
// Return string with US date format | ||
const getDateToDisplay = (dateToParse) => { | ||
const date = new Date(dateToParse); | ||
return `${date.getMonth()}/${date.getDate()}/${date.getFullYear()}`; | ||
}; | ||
|
||
// Return string with blog post creation and editing dates | ||
const formatPostDates = (created, updated) => { | ||
const options = { | ||
year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', | ||
}; | ||
const createdDate = new Date(created).toLocaleString('en-US', options); | ||
const updatedDate = new Date(updated).toLocaleString('en-US', options); | ||
|
||
let dateString = `Posted on ${createdDate}`; | ||
if (updated && created !== updated) { | ||
dateString += `, Last updated on ${updatedDate}`; | ||
} | ||
|
||
return dateString; | ||
}; | ||
|
||
export { | ||
sortBlogPosts, getDateToDisplay, formatPostDates, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters