forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostingModal.jsx
More file actions
86 lines (78 loc) · 2.52 KB
/
PostingModal.jsx
File metadata and controls
86 lines (78 loc) · 2.52 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
import { useState } from "react";
import Modal from "./Modal";
import { postApi } from "../../api/apiService";
import { useAuth } from "../../context/AuthContext";
const PostingModal = ({ isOpen, onClose, onPostCreated }) => {
const [content, setContent] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const { user } = useAuth();
const handleContentChange = (e) => {
setContent(e.target.value);
if (error) setError("");
};
const handleSubmit = async () => {
if (!content.trim()) {
setError("Please enter content.");
return;
}
setIsLoading(true);
setError("");
try {
const response = await postApi.createPost(content, user.username);
setContent("");
onClose();
if (onPostCreated) {
onPostCreated(response.data);
}
} catch (error) {
setError("An error occurred while creating the post. Please try again.");
} finally {
setIsLoading(false);
}
};
const handleCancel = () => {
if (
content.trim() &&
!window.confirm("You have unsaved content. Are you sure you want to cancel?")
) {
return;
}
setContent("");
onClose();
};
return (
<Modal isOpen={isOpen} onClose={handleCancel}>
<div className="w-full mb-4">
<textarea
value={content}
onChange={handleContentChange}
placeholder="Enter your content."
disabled={isLoading}
autoFocus
className="w-full min-h-[150px] bg-gray-100 dark:bg-gray-800 rounded-md p-4 text-base text-gray-900 dark:text-white placeholder-gray-400 resize-vertical focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:opacity-70"
/>
</div>
{error && (
<p className="text-red-500 text-sm mb-4 text-center">{error}</p>
)}
<div className="flex justify-center gap-4">
<button
onClick={handleSubmit}
disabled={isLoading || !content.trim()}
className="bg-blue-600 text-white rounded-md px-8 py-3 text-sm transition-opacity disabled:opacity-70 disabled:cursor-not-allowed"
>
{isLoading ? "Processing..." : "Submit"}
</button>
<button
onClick={handleCancel}
disabled={isLoading}
className="bg-gray-200 text-gray-800 rounded-md px-8 py-3 text-sm transition-opacity disabled:opacity-70 disabled:cursor-not-allowed"
>
Cancel
</button>
</div>
</Modal>
);
};
export default PostingModal;