-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComments.jsx
More file actions
166 lines (151 loc) · 5.8 KB
/
Comments.jsx
File metadata and controls
166 lines (151 loc) · 5.8 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { useState, useEffect } from 'react';
import './Comments.css';
const API_BASE = 'https://auth.goygoyengine.com/api/easycomment/v1';
export const Comments = ({ theme = 'light' }) => {
const [comments, setComments] = useState([]);
const [username, setUsername] = useState('');
const [commentText, setCommentText] = useState('');
const [replyTo, setReplyTo] = useState(null);
useEffect(() => {
loadComments();
}, []);
const loadComments = async () => {
try {
const response = await fetch(
`${API_BASE}/comments?comment_url=${encodeURIComponent(window.location.href)}`
);
const data = await response.json();
// Organize comments and replies
const mainComments = data.filter(c => !c.answer_comment_id);
const replies = data.filter(c => c.answer_comment_id);
// Group replies with their parent comments
const organizedComments = mainComments.map(comment => ({
...comment,
replies: replies.filter(r => r.answer_comment_id === comment.comment_id)
}));
setComments(organizedComments);
} catch (error) {
console.error('Error loading comments:', error);
}
};
const submitComment = async (e, parentId = null) => {
e.preventDefault();
if (!commentText.trim()) return;
try {
const response = await fetch(`${API_BASE}/comments`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
comment_url: window.location.href,
username: username || 'Anonymous',
text: commentText,
answer_comment_id: parentId
})
});
if (response.ok) {
setCommentText('');
setReplyTo(null);
loadComments();
}
} catch (error) {
console.error('Error posting comment:', error);
}
};
const Comment = ({ comment, isReply }) => (
<div className={`gdb-comment ${isReply ? 'reply' : ''}`}>
<div className="gdb-comment-header">
<span className="gdb-comment-username">
{comment.username || 'Anonymous'}
</span>
<span className="gdb-comment-time">
{new Date(comment.timestamp).toLocaleString()}
</span>
</div>
<p className="gdb-comment-text">{comment.text}</p>
{!isReply && (
<button
className="gdb-reply-button"
onClick={() => setReplyTo(comment.comment_id)}
>
Reply
</button>
)}
{replyTo === comment.comment_id && (
<form
className="gdb-reply-form"
onSubmit={(e) => submitComment(e, comment.comment_id)}
>
<input
type="text"
placeholder="Your name (optional)"
value={username}
onChange={(e) => setUsername(e.target.value)}
className="gdb-name-input"
/>
<textarea
placeholder="Write your reply..."
value={commentText}
onChange={(e) => setCommentText(e.target.value)}
className="gdb-comment-input"
/>
<div className="gdb-button-group">
<button type="submit" className="gdb-submit-button">
Submit
</button>
<button
type="button"
className="gdb-cancel-button"
onClick={() => setReplyTo(null)}
>
Cancel
</button>
</div>
</form>
)}
{comment.replies?.map(reply => (
<Comment
key={reply.comment_id}
comment={reply}
isReply={true}
/>
))}
</div>
);
return (
<div className={`gdb-comments-container ${theme}`}>
<h3 className="gdb-comments-title">
Comments ({comments.reduce((acc, c) => acc + 1 + (c.replies?.length || 0), 0)})
</h3>
<form className="gdb-comment-form" onSubmit={(e) => submitComment(e)}>
<input
type="text"
placeholder="Your name (optional)"
value={username}
onChange={(e) => setUsername(e.target.value)}
className="gdb-name-input"
/>
<textarea
placeholder="Write your comment..."
value={commentText}
onChange={(e) => setCommentText(e.target.value)}
className="gdb-comment-input"
/>
<button type="submit" className="gdb-submit-button">
Post Comment
</button>
</form>
<div className="gdb-comments-list">
{comments.map(comment => (
<Comment
key={comment.comment_id}
comment={comment}
isReply={false}
/>
))}
</div>
</div>
);
};
export default Comments;