-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComments.js
More file actions
71 lines (58 loc) · 1.77 KB
/
Comments.js
File metadata and controls
71 lines (58 loc) · 1.77 KB
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, useCallback } from "react";
import { useParams } from "react-router-dom";
import classes from "./Comments.module.css";
import NewCommentForm from "./NewCommentForm";
import useHttp from "../../components/hooks/use-http";
import { getAllComments } from "../../components/lib/api";
import LoadingSpinner from "../UI/LoadingSpinner";
import CommentsList from "./CommentsList";
const Comments = () => {
const [isAddingComment, setIsAddingComment] = useState(false);
const params = useParams();
const { quoteId } = params;
const { sendRequest, status, data: loadedComments } = useHttp(getAllComments);
useEffect(() => {
sendRequest(quoteId);
}, [quoteId, sendRequest]);
const startAddCommentHandler = () => {
setIsAddingComment(true);
};
const addedCommentHandler = useCallback(() => {
sendRequest(quoteId);
}, [sendRequest, quoteId]);
let comments;
if (status === "pending") {
comments = (
<div className="centered">
<LoadingSpinner />
</div>
);
}
if (status === "completed" && loadedComments && loadedComments.length > 0) {
comments = <CommentsList comments={loadedComments} />;
}
if (
status === "completed" &&
(!loadedComments || loadedComments.length === 0)
) {
comments = <p className="centered">No comments were added yet!</p>;
}
return (
<section className={classes.comments}>
<h2>User Comments</h2>
{!isAddingComment && (
<button className="btn" onClick={startAddCommentHandler}>
Add a Comment
</button>
)}
{isAddingComment && (
<NewCommentForm
quoteId={quoteId}
onAddedComment={addedCommentHandler}
/>
)}
{comments}
</section>
);
};
export default Comments;