SignUp.js 4.34 KB
Newer Older
seoyeon's avatar
seoyeon committed
1
import { useEffect, useState } from 'react'
2
import { Link } from 'react-router-dom'
seoyeon's avatar
seoyeon committed
3
4
5
import userApi from '../apis/user.api'
import catchErrors from '../context/catchError'

Kim, Chaerin's avatar
Kim, Chaerin committed
6
const INIT_USER = {
seoyeon's avatar
seoyeon committed
7
8
9
10
11
12
  name: '',
  email: '',
  password: '',
  checkpw: '',
  phone: '',
}
Kim, Chaerin's avatar
Kim, Chaerin committed
13

Kim, Chaerin's avatar
Kim, Chaerin committed
14
const Signup = () => {
seoyeon's avatar
seoyeon committed
15
16
17
18
  const [user, setUser] = useState(INIT_USER)
  const [error, setError] = useState('')
  const [disabled, setDisabled] = useState(false)
  const [success, setSuccess] = useState(false)
Kim, Chaerin's avatar
Kim, Chaerin committed
19
20

  useEffect(() => {
seoyeon's avatar
seoyeon committed
21
22
    setDisabled(!(user.name && user.email && user.password && user.checkpw))
  }, [user])
Kim, Chaerin's avatar
Kim, Chaerin committed
23
24

  function handleChange(event) {
seoyeon's avatar
seoyeon committed
25
26
    const { name, value } = event.target
    setUser({ ...user, [name]: value })
우지원's avatar
   
우지원 committed
27
    // console.log('user정보', user)
Kim, Chaerin's avatar
Kim, Chaerin committed
28
29
  }

seoyeon's avatar
seoyeon committed
30
31
  async function handleSubmit(e) {
    e.preventDefault()
Kim, Chaerin's avatar
Kim, Chaerin committed
32
    try {
우지원's avatar
   
우지원 committed
33
34
      // console.log('checkPassword:',checkPassword())
      // const passwordMatch = checkPassword()
우지원's avatar
ee    
우지원 committed
35
      if (checkPassword()) {
우지원's avatar
   
우지원 committed
36
37
38
        const res = await userApi.signup(user)
        console.log('서버연결됬나요', res)
        console.log('회원가입')
우지원's avatar
ee    
우지원 committed
39
40
        setSuccess(true)
        setError('')
seoyeon's avatar
seoyeon committed
41
      }
Kim, Chaerin's avatar
Kim, Chaerin committed
42
    } catch (error) {
우지원's avatar
   
우지원 committed
43
      console.log('에러발생')
seoyeon's avatar
seoyeon committed
44
      catchErrors(error, setError)
Kim, Chaerin's avatar
Kim, Chaerin committed
45
    } finally {
이재연's avatar
aaa    
이재연 committed
46
47
48
      // setLoading(false);
    }
  }
우지원's avatar
   
우지원 committed
49

이재연's avatar
aaa    
이재연 committed
50
  function checkPassword(event) {
seoyeon's avatar
seoyeon committed
51
52
    const p1 = user.password
    const p2 = user.checkpw
이재연's avatar
aaa    
이재연 committed
53
    if (p1 !== p2) {
seoyeon's avatar
seoyeon committed
54
55
      alert('비밀번호가 일치하지 않습니다.')
      return false
이재연's avatar
aaa    
이재연 committed
56
    } else {
seoyeon's avatar
seoyeon committed
57
      return true
Kim, Chaerin's avatar
Kim, Chaerin committed
58
59
    }
  }
seoyeon's avatar
0716    
seoyeon committed
60

우지원's avatar
   
우지원 committed
61
62
63
64
65
  if (success) {
    alert('회원가입이 완료되었습니다!')
    window.location.href = '/'
  }

seoyeon's avatar
seoyeon committed
66
  const { name, id, password, checkpw, phone } = user
Kim, Chaerin's avatar
Kim, Chaerin committed
67
  return (
seoyeon's avatar
seoyeon committed
68
69
    <div className="container">
      {error && <div className="alert alert-danger">{error}</div>}
Kim, Chaerin's avatar
Kim, Chaerin committed
70
      <form onSubmit={handleSubmit}>
seoyeon's avatar
seoyeon committed
71
72
73
74
        <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
75
        </div>
seoyeon's avatar
seoyeon committed
76
77
78
79
        <div className="row mt-4">
          <h5 style={{ textAlign: 'center' }}>회원가입</h5>
        </div>
        <div className="form-group">
Kim, Chaerin's avatar
Kim, Chaerin committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
          <div className="p-2">
            <label className="p-1">이름</label>
            <input
              className="form-control"
              id="name"
              type="text"
              name="name"
              placeholder="이름을 입력하세요"
              value={name}
              onChange={handleChange}
            />
          </div>
          <div className="p-2">
            <label className="p-1">아이디</label>
            <input
              className="form-control"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
96
              id="text"
Kim, Chaerin's avatar
Kim, Chaerin committed
97
              type="text"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
98
              name="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
              placeholder="아이디를 입력하세요"
              value={id}
              onChange={handleChange}
            />
          </div>
          <div className="p-2">
            <label className="p-1">비밀번호</label>
            <input
              className="form-control"
              id="password"
              type="password"
              name="password"
              placeholder="비밀번호를 입력하세요"
              value={password}
              onChange={handleChange}
            />
          </div>
          <div className="p-2">
            <label className="p-1">비밀번호확인</label>
            <input
              className="form-control"
              id="checkpw"
              type="password"
              name="checkpw"
              placeholder="비밀번호를 다시 입력하세요"
              value={checkpw}
              onChange={handleChange}
            />
          </div>
          <div className="p-2">
seoyeon's avatar
0802    
seoyeon committed
129
            <label className="p-1">전화번호</label>
Kim, Chaerin's avatar
Kim, Chaerin committed
130
131
132
133
134
            <input
              className="form-control"
              id="phone"
              type="text"
              name="phone"
135
              placeholder="'-'을 제외하고 입력하세요"
Kim, Chaerin's avatar
Kim, Chaerin committed
136
137
138
139
              value={phone}
              onChange={handleChange}
            />
          </div>
seoyeon's avatar
seoyeon committed
140
          <div className="mt-3 d-flex justify-content-center">
우지원's avatar
   
우지원 committed
141
142
143
144
145
146
147
148
149
            <button
              type="submit"
              className="btn btn-primary"
              onClick={handleSubmit}
              disabled={disabled}
            >
              회원가입
            </button>
          </div>
seoyeon's avatar
seoyeon committed
150
151
        </div>
      </form>
Kim, Chaerin's avatar
Kim, Chaerin committed
152
    </div>
seoyeon's avatar
seoyeon committed
153
154
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
155

seoyeon's avatar
seoyeon committed
156
export default Signup