forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateCommentForm.jsx
More file actions
75 lines (66 loc) · 1.98 KB
/
Copy pathCreateCommentForm.jsx
File metadata and controls
75 lines (66 loc) · 1.98 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
import { useState } from 'react';
import { useApp } from '../../context/AppContext';
import apiService from '../../services/api';
const CreateCommentForm = ({ postId, onCommentCreated }) => {
const [username, setUsername] = useState('');
const [content, setContent] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const { handleError, setServerOnline } = useApp();
const handleSubmit = async (e) => {
e.preventDefault();
if (!username.trim() || !content.trim()) {
handleError(new Error('Username and content are required'));
return;
}
setIsSubmitting(true);
try {
const newComment = await apiService.createComment(postId, {
username: username.trim(),
content: content.trim(),
});
setServerOnline();
setUsername('');
setContent('');
onCommentCreated?.(newComment);
} catch (error) {
handleError(error);
} finally {
setIsSubmitting(false);
}
};
return (
<form onSubmit={handleSubmit} className="create-comment-form">
<h4>Add a Comment</h4>
<div className="form-group">
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Your username"
disabled={isSubmitting}
required
className="comment-username-input"
/>
</div>
<div className="form-group">
<textarea
value={content}
onChange={(e) => setContent(e.target.value)}
placeholder="Write your comment..."
rows={3}
disabled={isSubmitting}
required
className="comment-content-input"
/>
</div>
<button
type="submit"
disabled={isSubmitting || !username.trim() || !content.trim()}
className="submit-comment-button"
>
{isSubmitting ? 'Adding...' : 'Add Comment'}
</button>
</form>
);
};
export default CreateCommentForm;