forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateCommentModal.jsx
More file actions
78 lines (68 loc) · 2.39 KB
/
Copy pathCreateCommentModal.jsx
File metadata and controls
78 lines (68 loc) · 2.39 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
import React, { useState } from 'react';
import { api } from '../services/api';
import { useUsername } from '../context/UsernameContext';
const CreateCommentModal = ({ isOpen, postId, onClose, onCommentCreated }) => {
const [content, setContent] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const { username } = useUsername();
const handleSubmit = async (e) => {
e.preventDefault();
if (!content.trim()) return;
setLoading(true);
setError(null);
try {
const newComment = await api.createComment(postId, username, content.trim());
onCommentCreated(newComment);
setContent('');
onClose();
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
if (!isOpen) return null;
return (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-40">
<div className="bg-white rounded-lg p-6 w-full max-w-md">
<h2 className="text-2xl font-bold mb-4">Add Comment</h2>
{error && (
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded mb-4">
{error}
</div>
)}
<form onSubmit={handleSubmit}>
<textarea
value={content}
onChange={(e) => setContent(e.target.value)}
placeholder="What do you think? (1-1000 characters)"
maxLength="1000"
minLength="1"
rows="4"
className="w-full px-4 py-2 border border-gray-300 rounded-lg mb-4 focus:outline-none focus:ring-2 focus:ring-blue-500"
autoFocus
/>
<div className="flex justify-end gap-2">
<button
type="button"
onClick={onClose}
disabled={loading}
className="px-4 py-2 border border-gray-300 rounded-lg hover:bg-gray-50 disabled:opacity-50"
>
Cancel
</button>
<button
type="submit"
disabled={!content.trim() || loading}
className="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed"
>
{loading ? 'Posting...' : 'Comment'}
</button>
</div>
</form>
</div>
</div>
);
};
export default CreateCommentModal;