-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCommentTree.jsx
More file actions
83 lines (73 loc) · 2.57 KB
/
CommentTree.jsx
File metadata and controls
83 lines (73 loc) · 2.57 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
72
73
74
75
76
77
78
79
80
81
82
83
import React, { useState } from "react";
import RatingComponent from "./RatingComponent.jsx";
import { model } from "../../model.js"; // Adjust the path if needed
import { addReviewForCourse } from "../../firebase"; // Adjust the path if needed
function CommentTree({ courseCode, comment, level = 0 }) {
const [showReply, setShowReply] = useState(false);
const [replyText, setReplyText] = useState("");
const handleReplySubmit = async () => {
if (replyText.trim().length === 0) return;
const reply = {
userName: model.user?.displayName || "Anonymous",
text: replyText,
timestamp: Date.now(),
overallRating: 0,
difficultyRating: 0,
professorRating: 0,
professorName: "",
grade: "",
recommend: null,
};
await addReviewForCourse(courseCode, reply, comment.id);
window.location.reload(); // quick reload for now; optional optimization later
};
return (
<div className="ml-4 mt-4 border-l pl-4 border-gray-300">
<div className="bg-gray-50 p-3 rounded-md shadow-sm">
<div className="flex justify-between items-center mb-1">
<p className="font-semibold text-gray-800">{comment.userName}</p>
<p className="text-xs text-gray-500">
{new Date(comment.timestamp).toLocaleDateString()}
</p>
</div>
<p className="text-sm text-gray-700 mb-1">{comment.text}</p>
<button
className="text-blue-500 text-sm hover:underline"
onClick={() => setShowReply(!showReply)}
>
{showReply ? "Cancel" : "Reply"}
</button>
{showReply && (
<div className="mt-2">
<textarea
className="w-full border border-gray-300 rounded-md p-2 text-sm"
placeholder="Write your reply..."
value={replyText}
onChange={(e) => setReplyText(e.target.value)}
/>
<button
onClick={handleReplySubmit}
className="mt-1 px-3 py-1 text-sm bg-blue-600 text-white rounded hover:bg-blue-700"
>
Post Reply
</button>
</div>
)}
</div>
{/* Recursive rendering of replies */}
{comment.replies && comment.replies.length > 0 && (
<div className="mt-2 space-y-2">
{comment.replies.map((child) => (
<CommentTree
key={child.id}
courseCode={courseCode}
comment={child}
level={level + 1}
/>
))}
</div>
)}
</div>
);
}
export default CommentTree;