-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComment.jsx
71 lines (67 loc) · 1.73 KB
/
Comment.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { useState, useEffect, useContext } from "react";
import { UserContext } from "./contexts/UserContext";
import LoadingSpinner from "./LoadingSpinner";
import { getComments, getAuthorAvatar } from "./apiFunctions";
import CommentHistory from "./CommentHistory";
import NewComment from "./NewComment";
const Comment = ({
articleid,
commentCount,
setCommentCount,
setNumItems,
commentPageNumber,
}) => {
const [isLoading, setIsLoading] = useState(true);
const [comments, setComments] = useState(null);
const [authorAvatars, setAuthorAvatars] = useState({});
const { user } = useContext(UserContext);
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;
},
user ? { [user.username]: user.avatar_url } : {}
)
);
setNumItems(commentCount);
setIsLoading(false);
});
});
}, [commentPageNumber]);
return isLoading ? (
<div id="commentsBox">
<LoadingSpinner id="commentLoadingSpinner" />
</div>
) : (
<div id="commentsBox">
{user && (
<NewComment
setComments={setComments}
articleid={articleid}
setCommentCount={setCommentCount}
/>
)}
<CommentHistory
comments={comments}
commentCount={commentCount}
authorAvatars={authorAvatars}
setComments={setComments}
/>
</div>
);
};
export default Comment;