/
Login.jsx
55 lines (51 loc) · 1.36 KB
/
Login.jsx
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
import axios from "axios";
import { useState } from "react";
const Login = () => {
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [user, setUser] = useState({});
const handleClick = async (e) => {
e.preventDefault();
setLoading(true);
try {
const { data } = await axios.get(
"https://jsonplaceholder.typicode.com/users/1"
);
setUser(data);
} catch {
setError(true);
}
setLoading(false)
};
return (
<div className="container">
<span className="user">{user.name}</span>
<form>
<input
type="text"
placeholder="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<input
type="password"
placeholder="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button disabled={!username || !password} onClick={handleClick}>
{loading ? "please wait" : "Login"}
</button>
<span
data-testid="error"
style={{ visibility: error ? "visible" : "hidden" }}
>
Something went wrong!
</span>
</form>
</div>
);
};
export default Login;