Login.js 2.4 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 login from "../apis/user.api";
Kim, Chaerin's avatar
Kim, Chaerin committed
3
const INIT_USER = {
Kim, Chaerin's avatar
Kim, Chaerin committed
4
  email: "",
Kim, Chaerin's avatar
Kim, Chaerin committed
5
6
7
  password: "",
};

Kim, Chaerin's avatar
Kim, Chaerin committed
8
const Login = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  //   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 });
    console.log(user);
  }

  async function handleSubmit() {
    try {
      // setLoading(true);
      // setError("");
Kim, Chaerin's avatar
Kim, Chaerin committed
30
      const success = await login(user.email, user.password);
Kim, Chaerin's avatar
Kim, Chaerin committed
31
32
33
34
35
36
37
38
39
      console.log(user);
      setSuccess(success);
    } catch (error) {
      // catchErrors(error, setError);
    } finally {
      // setLoading(false);
    }
  }

Kim, Chaerin's avatar
Kim, Chaerin committed
40
  const { email, password } = user;
Kim, Chaerin's avatar
Kim, Chaerin committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

  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
61
              id="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
62
              type="text"
Kim, Chaerin's avatar
Kim, Chaerin committed
63
              name="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
64
              placeholder="아이디를 입력하세요"
Kim, Chaerin's avatar
Kim, Chaerin committed
65
              value={email}
Kim, Chaerin's avatar
Kim, Chaerin committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
              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">
          <button type="submit" className="btn btn-primary" disabled={disabled}>
            로그인
          </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
86
        </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
87
88
89
      </form>
    </div>
  );
Kim, Chaerin's avatar
Kim, Chaerin committed
90
91
92
};

export default Login;