SignUp.js 4.24 KB
Newer Older
seoyeon's avatar
seoyeon 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 = {
seoyeon's avatar
seoyeon 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 = () => {
seoyeon's avatar
seoyeon 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(() => {
seoyeon's avatar
seoyeon 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) {
seoyeon's avatar
seoyeon committed
26
27
    const { name, value } = event.target
    setUser({ ...user, [name]: value })
Kim, Chaerin's avatar
Kim, Chaerin committed
28
29
  }

seoyeon's avatar
seoyeon committed
30
31
  async function handleSubmit(e) {
    e.preventDefault()
seoyeon's avatar
0802    
seoyeon committed
32
    console.log('회원가입')
Kim, Chaerin's avatar
Kim, Chaerin committed
33
    try {
seoyeon's avatar
seoyeon committed
34
35
36
37
      if (!checkPassword()) {
        return
      }
      const data = await userApi.signup(user)
이재연's avatar
aaa    
이재연 committed
38
      console.log(data)
seoyeon's avatar
seoyeon committed
39
40
      setSuccess(true)
      setError('')
Kim, Chaerin's avatar
Kim, Chaerin committed
41
    } catch (error) {
seoyeon's avatar
seoyeon committed
42
43
      console.log('error')
      catchErrors(error, setError)
Kim, Chaerin's avatar
Kim, Chaerin committed
44
    } finally {
이재연's avatar
aaa    
이재연 committed
45
46
47
48
      // setLoading(false);
    }
  }
  function checkPassword(event) {
seoyeon's avatar
seoyeon committed
49
50
    const p1 = user.password
    const p2 = user.checkpw
이재연's avatar
aaa    
이재연 committed
51
    if (p1 !== p2) {
seoyeon's avatar
seoyeon committed
52
53
54
      // event.preventDefault()
      alert('비밀번호가 일치하지 않습니다.')
      return false
이재연's avatar
aaa    
이재연 committed
55
    } else {
seoyeon's avatar
seoyeon committed
56
57
58
      alert('회원가입 되었습니다.')
      window.location.href = '/'
      return true
Kim, Chaerin's avatar
Kim, Chaerin committed
59
60
    }
  }
seoyeon's avatar
0716    
seoyeon committed
61

seoyeon's avatar
seoyeon committed
62
  const { name, id, password, checkpw, phone } = user
Kim, Chaerin's avatar
Kim, Chaerin committed
63
  return (
seoyeon's avatar
seoyeon committed
64
65
    <div className="container">
      {error && <div className="alert alert-danger">{error}</div>}
Kim, Chaerin's avatar
Kim, Chaerin committed
66
      <form onSubmit={handleSubmit}>
seoyeon's avatar
seoyeon committed
67
68
69
70
        <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
71
        </div>
seoyeon's avatar
seoyeon committed
72
73
74
75
        <div className="row mt-4">
          <h5 style={{ textAlign: 'center' }}>회원가입</h5>
        </div>
        <div className="form-group">
Kim, Chaerin's avatar
Kim, Chaerin committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
          <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
92
              id="text"
Kim, Chaerin's avatar
Kim, Chaerin committed
93
              type="text"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
94
              name="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
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
              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
125
            <label className="p-1">전화번호</label>
Kim, Chaerin's avatar
Kim, Chaerin committed
126
127
128
129
130
            <input
              className="form-control"
              id="phone"
              type="text"
              name="phone"
131
              placeholder="'-'을 제외하고 입력하세요"
Kim, Chaerin's avatar
Kim, Chaerin committed
132
133
134
135
              value={phone}
              onChange={handleChange}
            />
          </div>
seoyeon's avatar
seoyeon committed
136
137
          {/* {console.log(disabled)} */}
          <div className="mt-3 d-flex justify-content-center">
이재연's avatar
aaa    
이재연 committed
138
139
140
          <button
            type="submit"
            className="btn btn-primary"
seoyeon's avatar
seoyeon committed
141
            onClick={handleSubmit}
이재연's avatar
aaa    
이재연 committed
142
143
            disabled={disabled}
          >
Kim, Chaerin's avatar
Kim, Chaerin committed
144
145
            회원가입
          </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
146
        </div>
seoyeon's avatar
seoyeon committed
147
148
        </div>
      </form>
Kim, Chaerin's avatar
Kim, Chaerin committed
149
    </div>
seoyeon's avatar
seoyeon committed
150
151
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
152

seoyeon's avatar
seoyeon committed
153
export default Signup