Spaces:
Sleeping
Sleeping
File size: 5,470 Bytes
0f95125 | 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 | import React, { useState } from "react";
import axios from "axios";
import "./App.css";
function App() {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [action, setAction] = useState("login");
const [message, setMessage] = useState("");
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [selectedOption, setSelectedOption] = useState("");
const [language, setLanguage] = useState("");
const [codePrompt, setCodePrompt] = useState("");
const [modifyCode, setModifyCode] = useState("");
const [modifyLogic, setModifyLogic] = useState("");
const [userId, setUserId] = useState(null);
const [loading, setLoading] = useState(false); // New loading state
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true); // Show loader
try {
const response = await axios.post("http://localhost:5003/auth", {
username,
password,
action,
});
setLoading(false); // Hide loader
if (response.data.success) {
setMessage(response.data.message);
setIsLoggedIn(true);
setUserId(response.data.userId);
} else {
setMessage(response.data.message);
}
} catch (error) {
setLoading(false);
setMessage("Error connecting to the server.");
}
};
const handleOptionSubmit = async () => {
setLoading(true); // Show loader before request
try {
let finalPrompt = "";
if (selectedOption === "Generate Code") {
finalPrompt = Generate code for ${codePrompt};
if (language) {
finalPrompt += ` in ${language}`;
}
} else if (selectedOption === "Modify Code") {
if (modifyLogic && modifyCode) {
finalPrompt = ${modifyLogic} the following code:\n\n${modifyCode};
} else {
finalPrompt = Modify the following code:\n\n${modifyCode};
}
}
const response = await axios.post("http://localhost:5003/store-option", {
userId,
option: selectedOption,
language,
codePrompt: finalPrompt,
modifyCode: finalPrompt,
modifyLogic,
});
setLoading(false); // Hide loader after response
setMessage(response.data.message);
} catch (error) {
setLoading(false);
setMessage("Error storing data.");
}
};
return (
<div className="container">
{!isLoggedIn ? (
<div className="login-container">
<h1>{action === "login" ? "Login" : "Sign Up"}</h1>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="Username"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
<button type="submit">{action === "login" ? "Login" : "Sign Up"}</button>
</form>
<button onClick={() => setAction(action === "login" ? "signup" : "login")}>
{action === "login" ? "Create an account" : "Already have an account? Login"}
</button>
<p>{message}</p>
</div>
) : (
<div className="options-container">
<h1>Choose an Option</h1>
<button onClick={() => setSelectedOption("Generate Code")}>Generate Code</button>
<button onClick={() => setSelectedOption("Modify Code")}>Modify Code</button>
{selectedOption === "Generate Code" && (
<div>
<h2>Generate Code</h2>
<select onChange={(e) => setLanguage(e.target.value)}>
<option value="">Select Language</option>
<option value="Python">Python</option>
<option value="JavaScript">JavaScript</option>
<option value="C++">C++</option>
<option value="Java">Java</option>
</select>
<textarea
placeholder="Enter code prompt"
value={codePrompt}
onChange={(e) => setCodePrompt(e.target.value)}
/>
<br />
<button onClick={handleOptionSubmit}>Submit</button>
</div>
)}
{selectedOption === "Modify Code" && (
<div>
<h2>Modify Code</h2>
<textarea
placeholder="Enter existing code"
value={modifyCode}
onChange={(e) => setModifyCode(e.target.value)}
/>
<select value={modifyLogic} onChange={(e) => setModifyLogic(e.target.value)}>
<option value="">Select Modification Type</option>
<option value="Refactor">Refactor</option>
<option value="Modernize">Modernize</option>
<option value="Error Correction">Error Correction</option>
</select>
<br />
<button onClick={handleOptionSubmit}>Submit</button>
</div>
)}
<button onClick={() => setIsLoggedIn(false)}>Logout</button>
<p>{message}</p>
</div>
)}
{/* Show loader while waiting for response */}
{loading && <div className="loader"></div>}
</div>
);
}
export default App; |