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

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

Kim, Chaerin's avatar
Kim, Chaerin committed
15
const Signup = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
16
17
18
19
  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
20
21

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

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

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

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

Kim, Chaerin's avatar
Kim, Chaerin committed
62
63
  if (success) {
    alert('회원가입이 완료되었습니다!')
64
    return <Redirect to="/" />;
Kim, Chaerin's avatar
Kim, Chaerin committed
65
  }
seoyeon's avatar
0716    
seoyeon committed
66

Kim, Chaerin's avatar
Kim, Chaerin committed
67
  const { name, id, password, checkpw, phone } = user
Kim, Chaerin's avatar
Kim, Chaerin committed
68
  return (
seoyeon's avatar
0804    
seoyeon committed
69
70
71
72
73
74
75
76
77
78
    <div>
      <div>
        <form
          style={{ backgroundColor: '#FCF4FF' }}
          className="flex-column align-items-center justify-content-center p-2"
        >
          <div className="d-flex justify-content-center">
            <Link to="/">
              <img src="/BORA.png" style={{ width: '160px' }} />
            </Link>
Kim, Chaerin's avatar
Kim, Chaerin committed
79
          </div>
seoyeon's avatar
0804    
seoyeon committed
80
81
82
83
84
85
86
87
88
        </form>
      </div>
      <div
        style={{ backgroundColor: '#262626', width: 'auto', height: '2px' }}
      ></div>
      <div className="container">
        <form onSubmit={handleSubmit}>
          <div className="row mt-4">
            <h5 style={{ textAlign: 'center' }}>회원가입</h5>
Kim, Chaerin's avatar
Kim, Chaerin committed
89
          </div>
seoyeon's avatar
0804    
seoyeon committed
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
          {error && <div className="alert alert-danger">{error}</div>}
          <div className="form-group">
            <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"
                id="text"
                type="text"
                name="email"
                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">
              <label className="p-1">전화번호</label>
              <input
                className="form-control"
                id="phone"
                type="text"
                name="phone"
                placeholder="'-'을 제외하고 입력하세요"
                value={phone}
                onChange={handleChange}
              />
            </div>
            {/* {console.log(disabled)} */}
            <div className="mt-3 d-flex justify-content-center">
              <button
                type="submit"
                className="btn btn-primary"
                onClick={handleSubmit}
                disabled={disabled}
              >
                회원가입
              </button>
            </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
163
          </div>
seoyeon's avatar
0804    
seoyeon committed
164
165
        </form>
      </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
166
    </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
167
168
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
169

Kim, Chaerin's avatar
Kim, Chaerin committed
170
export default Signup