forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameInputModal.jsx
More file actions
78 lines (67 loc) · 2.14 KB
/
NameInputModal.jsx
File metadata and controls
78 lines (67 loc) · 2.14 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 { useState } from "react";
import Modal from "./Modal";
import { useAuth } from "../../context/AuthContext";
const NameInputModal = ({ isOpen, onClose }) => {
const [username, setUsername] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const { login } = useAuth();
const handleUsernameChange = (e) => {
setUsername(e.target.value);
if (error) setError("");
};
const handleSubmit = async () => {
if (!username.trim()) {
setError("Please enter your name.");
return;
}
setIsLoading(true);
setError("");
try {
await login(username);
setUsername("");
onClose();
} catch (error) {
setError("An error occurred during login. Please try again.");
} finally {
setIsLoading(false);
}
};
const handleKeyPress = (e) => {
if (e.key === "Enter" && !isLoading && username.trim()) {
handleSubmit();
}
};
return (
<Modal isOpen={isOpen} onClose={onClose}>
<h2 className="text-xl font-semibold text-center mb-6 text-gray-900 dark:text-white">
Please enter your name
</h2>
<div className="w-full mb-4">
<input
type="text"
value={username}
onChange={handleUsernameChange}
onKeyPress={handleKeyPress}
placeholder="Name"
disabled={isLoading}
autoFocus
className="w-full bg-gray-100 dark:bg-gray-800 rounded-md p-4 text-base text-gray-900 dark:text-white placeholder-gray-400 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">
<button
onClick={handleSubmit}
disabled={isLoading || !username.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..." : "Done"}
</button>
</div>
</Modal>
);
};
export default NameInputModal;