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

Kim, Chaerin's avatar
Kim, Chaerin committed
11
const Login = () => {
seoyeon's avatar
0726    
seoyeon committed
12
13
14
15
  const [user, setUser] = useState(INIT_USER)
  const [disabled, setDisabled] = useState(true)
  const [error, setError] = useState('')
  const [success, setSuccess] = useState(false)
seoyeon's avatar
useRef    
seoyeon committed
16
17
  // const [modalopen, setmodalOpen] = useState(false)
  // const loginref = useRef()
Kim, Chaerin's avatar
Kim, Chaerin committed
18
19

  useEffect(() => {
seoyeon's avatar
0726    
seoyeon committed
20
21
    const isUser = Object.values(user).every((el) => Boolean(el))
    isUser ? setDisabled(false) : setDisabled(true)
seoyeon's avatar
useRef    
seoyeon committed
22
23
24
25
    // window.addEventListener('click', handleModal)
    // return () => {
    //   window.removeEventListener('click', handleModal)
    // }
seoyeon's avatar
0726    
seoyeon committed
26
  }, [user])
Kim, Chaerin's avatar
Kim, Chaerin committed
27
28

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

seoyeon's avatar
useRef    
seoyeon committed
33
34
35
36
37
38
39
40
  // const handleModal = (e) => {
  //   if (
  //     modalopen &&
  //     (!loginref.current || !loginref.current.contains(e.target))
  //   )
  //     setmodalOpen(false)
  // }

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
41
  async function handleSubmit(e) {
seoyeon's avatar
0726    
seoyeon committed
42
    e.preventDefault()
seoyeon's avatar
0724    
seoyeon committed
43
    console.log('로그인')
Kim, Chaerin's avatar
Kim, Chaerin committed
44
45
46
    try {
      // setLoading(true);
      // setError("");
seoyeon's avatar
0726    
seoyeon committed
47
48
      const data = await userApi.login(user)
      console.log(data)
seoyeon's avatar
seoyeon committed
49
      handleLogin(data.id)
seoyeon's avatar
0726    
seoyeon committed
50
      setSuccess(true)
Kim, Chaerin's avatar
Kim, Chaerin committed
51
    } catch (error) {
seoyeon's avatar
0726    
seoyeon committed
52
53
      console.log('error in login', error)
      catchErrors(error, setError)
Kim, Chaerin's avatar
Kim, Chaerin committed
54
55
56
57
    } finally {
      // setLoading(false);
    }
  }
seoyeon's avatar
0716    
seoyeon committed
58
59
  if (success) {
    alert('로그인 되었습니다')
seoyeon's avatar
0726    
seoyeon committed
60
    return <Redirect to="/user" />
seoyeon's avatar
0716    
seoyeon committed
61
62
  }

seoyeon's avatar
0726    
seoyeon committed
63
  const { email, password } = user
Kim, Chaerin's avatar
Kim, Chaerin committed
64
65

  return (
seoyeon's avatar
seoyeon committed
66
    <div className="container">
Kim, Chaerin's avatar
Kim, Chaerin committed
67
      <form onSubmit={handleSubmit}>
seoyeon's avatar
seoyeon committed
68
69
70
71
        <div className="m-3 d-flex justify-content-center">
          <Link to="/">
            <img src="/BORA.png" style={{ width: '160px' }} />
          </Link>
Kim, Chaerin's avatar
Kim, Chaerin committed
72
        </div>
seoyeon's avatar
seoyeon committed
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
        <div className="row mt-5 d-flex align-items-center">
          <h2 style={{ textAlign: 'center' }}>로그인</h2>
          {error && <div className="alert alert-danger">{error}</div>}
          <div className="form-group">
            <div className="mt-5">
              <label>아이디</label>
              <input
                className="form-control"
                id="email"
                type="text"
                name="email"
                placeholder="아이디를 입력하세요"
                value={email}
                onChange={handleChange}
              />
            </div>
            <div className="mt-3">
              <label>비밀번호</label>
              <input
                className="form-control"
                id="password"
                type="password"
                name="password"
                placeholder="비밀번호를 입력하세요"
                value={password}
                onChange={handleChange}
              />
            </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
101
          </div>
seoyeon's avatar
seoyeon committed
102
103
104
105
106
107
108
109
          <div className="mt-4 d-flex justify-content-center">
            <button
              type="submit"
              className="btn btn-primary"
              disabled={disabled}
            >
              로그인
            </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
110
111
112
113
          </div>
        </div>
      </form>
    </div>
seoyeon's avatar
0726    
seoyeon committed
114
115
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
116

seoyeon's avatar
0726    
seoyeon committed
117
export default Login