SignUp.js 5.27 KB
Newer Older
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
1
import axios from "axios";
Kim, Chaerin's avatar
Kim, Chaerin committed
2
import { useEffect, useState } from "react";
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
3
4
5
6
import { Redirect } from "react-router-dom";
import userApi from "../apis/user.api";
// import catchErrors from "../context/catchError";
// import auth from "../context/auth_context"
Kim, Chaerin's avatar
Kim, Chaerin committed
7
8
9
10
const INIT_USER = {
  name: "",
  idNumber1: "",
  idNumber2: "",
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
11
  email: "",
Kim, Chaerin's avatar
Kim, Chaerin committed
12
13
14
15
16
  password: "",
  checkpw: "",
  phone: "",
};

Kim, Chaerin's avatar
Kim, Chaerin committed
17
const Signup = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
18
19
  const [user, setUser] = useState(INIT_USER);
  const [error, setError] = useState("");
Kim, Chaerin's avatar
Kim, Chaerin committed
20
  const [disabled, setDisabled] = useState(false);
Kim, Chaerin's avatar
Kim, Chaerin committed
21
  const [success, setSuccess] = useState(false);
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
22
  const [loading, setLoading] = useState(false);
Kim, Chaerin's avatar
Kim, Chaerin committed
23
24

  useEffect(() => {
Kim, Chaerin's avatar
Kim, Chaerin committed
25
26
27
28
29
    setDisabled(
      !(
        user.name &&
        user.idNumber1 &&
        user.idNumber2 &&
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
30
        user.email &&
Kim, Chaerin's avatar
Kim, Chaerin committed
31
32
33
        user.password &&
        user.checkpw
      )
Kim, Chaerin's avatar
Kim, Chaerin committed
34
35
36
37
38
39
    );
  }, [user]);

  function handleChange(event) {
    const { name, value } = event.target;
    setUser({ ...user, [name]: value });
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
40
    // console.log(user);
Kim, Chaerin's avatar
Kim, Chaerin committed
41
42
43
  }

  async function handleSubmit() {
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
44
    console.log(user)
Kim, Chaerin's avatar
Kim, Chaerin committed
45
    try {
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
46
47
48
49
50
      const data = await userApi.signup(user)
      setLoading(true);
      setError("");
      // const success = await login(user.email, user.password);
      // const data = await axios.post("/api/room/1/1",user)
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
51
      console.log(data);
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
52
      setSuccess(true);
Kim, Chaerin's avatar
Kim, Chaerin committed
53
54
55
    } catch (error) {
      // catchErrors(error, setError);
    } finally {
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
56
      setLoading(false);
Kim, Chaerin's avatar
Kim, Chaerin committed
57
58
59
    }
  }

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


Kim, Chaerin's avatar
Kim, Chaerin committed
66
67
68
69
70
71
72
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
  const { name, idNumber1, idNumber2, id, password, checkpw, phone } = user;
  return (
    <div className="modal-content">
      <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 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>
            <div className="d-flex text-center">
              <div className="col-4">
                <input
99
                  className="mt-2 form-control"
Kim, Chaerin's avatar
Kim, Chaerin committed
100
101
102
103
                  id="idNumber1"
                  type="text"
                  name="idNumber1"
                  placeholder="●●●●●●"
104
                  maxLength='6'
Kim, Chaerin's avatar
Kim, Chaerin committed
105
106
107
108
                  value={idNumber1}
                  onChange={handleChange}
                />
              </div>
109
              <div className="col-1 pt-3 ps-2"></div>
Kim, Chaerin's avatar
Kim, Chaerin committed
110
111
              <div className="col-1">
                <input
112
                  className="mt-2 ms-2 form-control"
Kim, Chaerin's avatar
Kim, Chaerin committed
113
114
                  id="idNumber2"
                  type="text"
115
                  style={{width:'35px'}}
Kim, Chaerin's avatar
Kim, Chaerin committed
116
117
118
119
                  name="idNumber2"
                  placeholder=""
                  value={idNumber2}
                  onChange={handleChange}
120
                  maxLength='1'
Kim, Chaerin's avatar
Kim, Chaerin committed
121
122
                />
              </div>
123
              <div className="pt-3 ps-4">* * * * * *</div>
Kim, Chaerin's avatar
Kim, Chaerin committed
124
125
126
127
128
129
            </div>
          </div>
          <div className="p-2">
            <label className="p-1">아이디</label>
            <input
              className="form-control"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
130
              id="text"
Kim, Chaerin's avatar
Kim, Chaerin committed
131
              type="text"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
132
              name="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
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
163
164
165
166
167
168
              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"
169
              placeholder="'-'을 제외하고 입력하세요"
Kim, Chaerin's avatar
Kim, Chaerin committed
170
171
172
173
174
175
176
177
178
              value={phone}
              onChange={handleChange}
            />
          </div>
        </div>
        <div className="modal-footer">
          <button type="submit" className="btn btn-primary" disabled={disabled}>
            회원가입
          </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
179
        </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
180
181
182
      </form>
    </div>
  );
Kim, Chaerin's avatar
Kim, Chaerin committed
183
184
185
};

export default Signup;