SignUp.js 4.14 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";
이재연's avatar
aaa    
이재연 committed
5
import catchErrors from "../context/catchError";
seoyeon's avatar
0724    
seoyeon committed
6
// import auth from "../context/auth_context"
Kim, Chaerin's avatar
Kim, Chaerin committed
7
8
const INIT_USER = {
  name: "",
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
9
  email: "",
Kim, Chaerin's avatar
Kim, Chaerin committed
10
11
12
13
14
  password: "",
  checkpw: "",
  phone: "",
};

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

  useEffect(() => {
이재연's avatar
aaa    
이재연 committed
22
    setDisabled(!(user.name && user.email && user.password && user.checkpw));
Kim, Chaerin's avatar
Kim, Chaerin committed
23
24
25
26
27
28
29
30
31
  }, [user]);

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

  async function handleSubmit() {
    try {
이재연's avatar
aaa    
이재연 committed
32
33
      const data = await userApi.signup(user);
      console.log(data)
seoyeon's avatar
0726    
seoyeon committed
34
      setSuccess(true);
이재연's avatar
aaa    
이재연 committed
35
      setError("");
Kim, Chaerin's avatar
Kim, Chaerin committed
36
    } catch (error) {
이재연's avatar
aaa    
이재연 committed
37
      catchErrors(error, setError);
Kim, Chaerin's avatar
Kim, Chaerin committed
38
    } finally {
이재연's avatar
aaa    
이재연 committed
39
40
41
42
43
44
45
46
47
48
49
50
51
      // 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
52
53
    }
  }
seoyeon's avatar
0716    
seoyeon committed
54
  if (success) {
이재연's avatar
aaa    
이재연 committed
55
56
    alert("회원가입 되었습니다.");
    <Redirect to="/" />;
seoyeon's avatar
0716    
seoyeon committed
57
58
  }

이재연's avatar
aaa    
이재연 committed
59
  const { name, id, password, checkpw, phone } = user;
Kim, Chaerin's avatar
Kim, Chaerin committed
60
61
62
63
64
65
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
  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>
            <input
              className="form-control"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
91
              id="text"
Kim, Chaerin's avatar
Kim, Chaerin committed
92
              type="text"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
93
              name="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
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
              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"
130
              placeholder="'-'을 제외하고 입력하세요"
Kim, Chaerin's avatar
Kim, Chaerin committed
131
132
133
134
135
              value={phone}
              onChange={handleChange}
            />
          </div>
        </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
136
        {console.log(disabled)}
Kim, Chaerin's avatar
Kim, Chaerin committed
137
        <div className="modal-footer">
이재연's avatar
aaa    
이재연 committed
138
139
140
141
142
143
          <button
            type="submit"
            className="btn btn-primary"
            onClick={checkPassword}
            disabled={disabled}
          >
Kim, Chaerin's avatar
Kim, Chaerin committed
144
145
            회원가입
          </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
146
        </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
147
148
149
      </form>
    </div>
  );
Kim, Chaerin's avatar
Kim, Chaerin committed
150
151
152
};

export default Signup;