Login.js 2.56 KB
Newer Older
Kim, Chaerin's avatar
Kim, Chaerin committed
1
import { useEffect, useState } from "react";
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
2
import { Redirect } from "react-router-dom";
3
import userApi from "../apis/user.api";
Kim, Chaerin's avatar
Kim, Chaerin committed
4
const INIT_USER = {
Kim, Chaerin's avatar
Kim, Chaerin committed
5
  email: "",
Kim, Chaerin's avatar
Kim, Chaerin committed
6
7
8
  password: "",
};

Kim, Chaerin's avatar
Kim, Chaerin committed
9
const Login = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  //   const { error, loading, login } = useAuth();
  const [user, setUser] = useState(INIT_USER);
  const [disabled, setDisabled] = useState(true);
  const [error, setError] = useState("");
  const [success, setSuccess] = useState(false);

  useEffect(() => {
    const isUser = Object.values(user).every((el) => Boolean(el));
    isUser ? setDisabled(false) : setDisabled(true);
  }, [user]);

  function handleChange(event) {
    const { name, value } = event.target;
    setUser({ ...user, [name]: value });
  }

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
26
27
  async function handleSubmit(e) {
    e.preventDefault();
Kim, Chaerin's avatar
Kim, Chaerin committed
28
29
30
    try {
      // setLoading(true);
      // setError("");
31
      const data = await userApi.login(user);
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
32
33
      console.log(data);
      setSuccess(true);
Kim, Chaerin's avatar
Kim, Chaerin committed
34
35
36
37
38
39
    } catch (error) {
      // catchErrors(error, setError);
    } finally {
      // setLoading(false);
    }
  }
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
40
41
42
  if (success) {
    return <Redirect to="/user" />;
  }
Kim, Chaerin's avatar
Kim, Chaerin committed
43

seoyeon's avatar
0716    
seoyeon committed
44
45
46
47
  if (success) {
    alert('로그인 되었습니다')
  }

Kim, Chaerin's avatar
Kim, Chaerin committed
48
  const { email, password } = user;
Kim, Chaerin's avatar
Kim, Chaerin committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

  return (
    <div className="modal-content">
      <form onSubmit={handleSubmit}>
        <div className="modal-header">
          <h5 className="modal-title" id="loginModalLabel">
            로그인
          </h5>
          <button
            type="button"
            className="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div className="modal-body">
          <div>
            <label>아이디</label>
            <input
              className="form-control"
Kim, Chaerin's avatar
Kim, Chaerin committed
69
              id="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
70
              type="text"
Kim, Chaerin's avatar
Kim, Chaerin committed
71
              name="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
72
              placeholder="아이디를 입력하세요"
Kim, Chaerin's avatar
Kim, Chaerin committed
73
              value={email}
Kim, Chaerin's avatar
Kim, Chaerin committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
              onChange={handleChange}
            />
          </div>
          <div>
            <label>비밀번호</label>
            <input
              className="form-control"
              id="password"
              type="password"
              name="password"
              placeholder="비밀번호를 입력하세요"
              value={password}
              onChange={handleChange}
            />
          </div>
        </div>
        <div className="modal-footer">
91
          <button type="submit" className="btn btn-primary" disabled={disabled} data-bs-dismiss="modal">
Kim, Chaerin's avatar
Kim, Chaerin committed
92
93
            로그인
          </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
94
        </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
95
96
97
      </form>
    </div>
  );
Kim, Chaerin's avatar
Kim, Chaerin committed
98
99
100
};

export default Login;