-
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.
add comments in articles with pagination
- Loading branch information
Showing
11 changed files
with
247 additions
and
39 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,54 @@ | ||
import { useState, useEffect } from "react"; | ||
import LoadingSpinner from "./LoadingSpinner"; | ||
import { getComments, getAuthorAvatar } from "./apiFunctions"; | ||
import CommentHistory from "./CommentHistory"; | ||
|
||
const Comment = ({ | ||
articleid, | ||
commentCount, | ||
setNumItems, | ||
commentPageNumber, | ||
}) => { | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [comments, setComments] = useState(null); | ||
const [authorAvatars, setAuthorAvatars] = useState({}); | ||
|
||
useEffect(() => { | ||
setIsLoading(true); | ||
setNumItems(null); | ||
getComments(articleid, commentPageNumber).then((comments) => { | ||
setComments(comments); | ||
const authorList = [ | ||
...new Set(comments.map((comment) => comment.author)), | ||
]; | ||
const promises = []; | ||
authorList.forEach((author) => | ||
promises.push(getAuthorAvatar(author)) | ||
); | ||
Promise.all(promises).then((data) => { | ||
setAuthorAvatars( | ||
data.reduce((authors, item) => { | ||
authors[item[0]] = item[1]; | ||
return authors; | ||
}, {}) | ||
); | ||
setNumItems(commentCount); | ||
setIsLoading(false); | ||
}); | ||
}); | ||
}, [commentPageNumber]); | ||
|
||
return isLoading ? ( | ||
<div id="commentsBox"> | ||
<LoadingSpinner id="commentLoadingSpinner" /> | ||
</div> | ||
) : ( | ||
<CommentHistory | ||
comments={comments} | ||
commentCount={commentCount} | ||
authorAvatars={authorAvatars} | ||
/> | ||
); | ||
}; | ||
|
||
export default Comment; |
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,19 @@ | ||
import { formatDate } from "./utils"; | ||
|
||
const CommentCard = ({ comment, authorAvatars }) => { | ||
return ( | ||
<div className="commentCard"> | ||
<img | ||
src={authorAvatars[comment.author]} | ||
alt={comment.author} | ||
className="commentAvatar" | ||
/> | ||
<p className="commentAuthor">{comment.author}</p> | ||
<p className="commentVotes">{comment.votes} likes</p> | ||
<p className="commentBody">{comment.body}</p> | ||
<p className="commentDate">{formatDate(comment.created_at)}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CommentCard; |
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,23 @@ | ||
import CommentCard from "./CommentCard"; | ||
|
||
const CommentHistory = ({ comments, authorAvatars, commentCount }) => { | ||
return comments.length === 0 ? ( | ||
<div id="commentsBox"> | ||
<h3 id="commentsTitle">No comments</h3> | ||
</div> | ||
) : ( | ||
<div id="commentsBox"> | ||
<h3 id="commentsTitle">Recent comments ({commentCount})</h3> | ||
{comments.map((comment) => ( | ||
<CommentCard | ||
comment={comment} | ||
authorAvatars={authorAvatars} | ||
key={comment.comment_id} | ||
/> | ||
))} | ||
<div id="bottomSpace"></div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CommentHistory; |
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
Oops, something went wrong.