1
0
Fork 0

Trying to figure out comments

This commit is contained in:
Amarpreet Minhas 2020-01-26 01:25:58 -05:00
parent 1a5d37f116
commit c8ba3ebaf3
3 changed files with 46 additions and 19 deletions

View file

@ -9,7 +9,7 @@ export const fetchPosts = () => async (dispatch) => {
export const fetchPost = (slug) => async (dispatch) => {
const post = await sudoscientist.get('/blog/posts/by-slug/' + slug)
const comments = await sudoscientist.get('/blog/comments/' + post.data.id);
const response = { ...post.data, ...comments.data }
const response = { post: post.data, comments: comments.data }
dispatch({ type: 'FETCH_POST', payload: response })
};

View file

@ -18,15 +18,28 @@ class Post extends React.Component {
.then(() => this.setState({isLoading: false}))
}
render () {
const {posts} = this.props
const {isLoading} = this.state
renderComments() {
//if (comments.length) {
//return comments.map(comment => {
// return (
// <div className="item" key={comment.id}>
// <div className="content">
// <div className="markdown-body">
// <ReactMarkdown source={comment.content} />
// </div>
// </div>
// </div>
// );
//});
//}
}
if (isLoading) {
return <p>Loading</p>
} else {
renderPost() {
const {posts} = this.props
const post = posts.entities[posts.currentId]
return (
console.log(post)
return(
<div className="container">
<div className="item" key={post.id}>
<div className="content">
<div className="description">
@ -39,6 +52,21 @@ class Post extends React.Component {
</div>
</div>
</div>
{ this.renderComments(post.comments) }
</div>
)
}
render () {
const {isLoading} = this.state
if (isLoading) {
return <p>Loading</p>
} else {
return (
<div>
{ this.renderPost() }
</div>
);
}
}
@ -47,8 +75,7 @@ class Post extends React.Component {
const mapStateToProps = (state) => {
return {
posts: state.posts,
slug: state.slug,
comments: state.comments
slug: state.slug
};
}

View file

@ -17,8 +17,8 @@ export default (state = initialState, action) => {
return {...state, ...{entities: mergedEntities}}
case 'FETCH_POST':
mergedEntities = normalizeEntities(state.entities, [action.payload])
return {...state, ...{entities: mergedEntities, currentId: action.payload.id}}
mergedEntities = normalizeEntities(state.entities, [action.payload.post])
return {...state, ...{entities: mergedEntities, currentId: action.payload.post.id}}
default:
return state;
}