Login.js 2.92 KB
Newer Older
seoyeon's avatar
0726    
seoyeon committed
1
import { useEffect, useState } from 'react'
우지원's avatar
0802    
우지원 committed
2
import { Redirect} from 'react-router-dom'
seoyeon's avatar
0726    
seoyeon committed
3
4
import userApi from '../apis/user.api'
import catchErrors from "../context/catchError";
seoyeon's avatar
seoyeon committed
5
import { handleLogin } from '../context/auth';
Kim, Chaerin's avatar
Kim, Chaerin committed
6
const INIT_USER = {
우지원's avatar
0802    
우지원 committed
7
  userId: '9999',
seoyeon's avatar
0726    
seoyeon committed
8
9
10
  email: '',
  password: '',
}
Kim, Chaerin's avatar
Kim, Chaerin committed
11

Kim, Chaerin's avatar
Kim, Chaerin committed
12
const Login = () => {
seoyeon's avatar
0726    
seoyeon committed
13
14
15
16
17
  // const { loading, login, catchErrorAuth } = useAuth()
  const [user, setUser] = useState(INIT_USER)
  const [disabled, setDisabled] = useState(true)
  const [error, setError] = useState('')
  const [success, setSuccess] = useState(false)
우지원's avatar
우지원 committed
18
  const [id, setId] = useState('')
Kim, Chaerin's avatar
Kim, Chaerin committed
19
20

  useEffect(() => {
seoyeon's avatar
0726    
seoyeon committed
21
22
23
    const isUser = Object.values(user).every((el) => Boolean(el))
    isUser ? setDisabled(false) : setDisabled(true)
  }, [user])
Kim, Chaerin's avatar
Kim, Chaerin committed
24
25

  function handleChange(event) {
seoyeon's avatar
0726    
seoyeon committed
26
27
    const { name, value } = event.target
    setUser({ ...user, [name]: value })
Kim, Chaerin's avatar
Kim, Chaerin committed
28
29
  }

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
30
  async function handleSubmit(e) {
seoyeon's avatar
0726    
seoyeon committed
31
    e.preventDefault()
seoyeon's avatar
0724    
seoyeon committed
32
    console.log('로그인')
Kim, Chaerin's avatar
Kim, Chaerin committed
33
34
35
    try {
      // setLoading(true);
      // setError("");
seoyeon's avatar
0726    
seoyeon committed
36
37
      const data = await userApi.login(user)
      console.log(data)
우지원's avatar
우지원 committed
38
      setId(data.id)
seoyeon's avatar
seoyeon committed
39
      handleLogin(data.id)
seoyeon's avatar
0726    
seoyeon committed
40
      setSuccess(true)
Kim, Chaerin's avatar
Kim, Chaerin committed
41
    } catch (error) {
seoyeon's avatar
0726    
seoyeon committed
42
43
      console.log('error in login', error)
      catchErrors(error, setError)
Kim, Chaerin's avatar
Kim, Chaerin committed
44
45
46
47
    } finally {
      // setLoading(false);
    }
  }
seoyeon's avatar
0716    
seoyeon committed
48
49
  if (success) {
    alert('로그인 되었습니다')
우지원's avatar
우지원 committed
50
    return <Redirect to={`/user/${id}`} />
seoyeon's avatar
0716    
seoyeon committed
51
52
  }

seoyeon's avatar
0726    
seoyeon committed
53
  const { email, password } = user
Kim, Chaerin's avatar
Kim, Chaerin committed
54
55
56

  return (
    <div className="modal-content">
seoyeon's avatar
0726    
seoyeon committed
57
      {error && <div className="alert alert-danger">{error}</div>}
Kim, Chaerin's avatar
Kim, Chaerin committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
      <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
75
              id="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
76
              type="text"
Kim, Chaerin's avatar
Kim, Chaerin committed
77
              name="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
78
              placeholder="아이디를 입력하세요"
Kim, Chaerin's avatar
Kim, Chaerin committed
79
              value={email}
Kim, Chaerin's avatar
Kim, Chaerin committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
              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">
seoyeon's avatar
0726    
seoyeon committed
97
98
99
100
101
102
          <button
            type="submit"
            className="btn btn-primary"
            disabled={disabled}
            data-bs-dismiss="modal"
          >
Kim, Chaerin's avatar
Kim, Chaerin committed
103
104
            로그인
          </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
105
        </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
106
107
      </form>
    </div>
seoyeon's avatar
0726    
seoyeon committed
108
109
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
110

seoyeon's avatar
0726    
seoyeon committed
111
export default Login