forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentInput.jsx
More file actions
60 lines (52 loc) · 1.76 KB
/
CommentInput.jsx
File metadata and controls
60 lines (52 loc) · 1.76 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
import { useState } from "react";
import { useAuth } from "../../context/AuthContext";
import { commentApi } from "../../api/apiService";
const CommentInput = ({ postId, onCommentAdded }) => {
const [content, setContent] = useState("");
const [isLoading, setIsLoading] = useState(false);
const { user } = useAuth();
const handleContentChange = (e) => {
setContent(e.target.value);
};
const handleSubmit = async (e) => {
e.preventDefault();
if (!content.trim() || !user) return;
setIsLoading(true);
try {
const response = await commentApi.createComment(
postId,
content,
user.username
);
setContent("");
if (onCommentAdded) {
onCommentAdded(response.data);
}
} catch (error) {
alert("An error occurred while adding the comment.");
} finally {
setIsLoading(false);
}
};
return (
<div className="w-full mt-4 pt-4 border-t border-gray-300">
<form className="flex gap-2 items-start" onSubmit={handleSubmit}>
<textarea
className="flex-1 min-h-[60px] bg-gray-100 dark:bg-gray-800 rounded-md p-3 text-sm text-gray-900 dark:text-white placeholder-gray-400 resize-vertical focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:opacity-70"
value={content}
onChange={handleContentChange}
placeholder="Add a comment"
disabled={isLoading || !user}
/>
<button
type="submit"
disabled={!content.trim() || isLoading || !user}
className="bg-blue-600 text-white rounded-md px-4 py-2 text-sm h-10 transition-opacity disabled:opacity-50 disabled:cursor-not-allowed"
>
Post
</button>
</form>
</div>
);
};
export default CommentInput;