SignUp.js 4.24 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";
seoyeon's avatar
0724    
seoyeon committed
3
import { Redirect } from "react-router-dom";
이재연's avatar
s    
이재연 committed
4
import userApi from "../apis/user.api";
seoyeon's avatar
0802    
seoyeon committed
5
import catchErrors from "../context/catchError";
Kim, Chaerin's avatar
Kim, Chaerin committed
6
7
const INIT_USER = {
  name: "",
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
8
  email: "",
Kim, Chaerin's avatar
Kim, Chaerin committed
9
10
11
12
13
  password: "",
  checkpw: "",
  phone: "",
};

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

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

  function handleChange(event) {
    const { name, value } = event.target;
    setUser({ ...user, [name]: value });
  }

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

seoyeon's avatar
0802    
seoyeon committed
66
  const { name, id, password, checkpw, phone } = user;
Kim, Chaerin's avatar
Kim, Chaerin committed
67
68
  return (
    <div className="modal-content">
seoyeon's avatar
0802    
seoyeon committed
69
        {error && <div className="alert alert-danger">{error}</div>}
Kim, Chaerin's avatar
Kim, Chaerin committed
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
      <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>
            <input
              className="form-control"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
99
              id="text"
Kim, Chaerin's avatar
Kim, Chaerin committed
100
              type="text"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
101
              name="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
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
              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
132
            <label className="p-1">전화번호</label>
Kim, Chaerin's avatar
Kim, Chaerin committed
133
134
135
136
137
            <input
              className="form-control"
              id="phone"
              type="text"
              name="phone"
138
              placeholder="'-'을 제외하고 입력하세요"
Kim, Chaerin's avatar
Kim, Chaerin committed
139
140
141
142
143
              value={phone}
              onChange={handleChange}
            />
          </div>
        </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
144
        {console.log(disabled)}
Kim, Chaerin's avatar
Kim, Chaerin committed
145
        <div className="modal-footer">
이재연's avatar
aaa    
이재연 committed
146
147
148
149
150
151
          <button
            type="submit"
            className="btn btn-primary"
            onClick={checkPassword}
            disabled={disabled}
          >
Kim, Chaerin's avatar
Kim, Chaerin committed
152
153
            회원가입
          </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
154
        </div>
seoyeon's avatar
0802    
seoyeon committed
155
      </form> 
Kim, Chaerin's avatar
Kim, Chaerin committed
156
157
    </div>
  );
Kim, Chaerin's avatar
Kim, Chaerin committed
158
159
160
};

export default Signup;