SignUp.js 4.04 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
6
import catchErrors from "../context/catchError";

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
  const [success, setSuccess] = useState(false);
seoyeon's avatar
0724    
seoyeon committed
20
  const [loading, setLoading] = useState(false);
Kim, Chaerin's avatar
Kim, Chaerin committed
21
22

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

  function handleChange(event) {
    const { name, value } = event.target;
    setUser({ ...user, [name]: value });
이재연's avatar
z    
이재연 committed
36
    // console.log(user);
Kim, Chaerin's avatar
Kim, Chaerin committed
37
38
39
  }

  async function handleSubmit() {
seoyeon's avatar
0802    
seoyeon committed
40
    console.log('회원가입')
Kim, Chaerin's avatar
Kim, Chaerin committed
41
    try {
이재연's avatar
s    
이재연 committed
42
43
44
      const data = await userApi.signup(user)
      setLoading(true);
      setError("");
seoyeon's avatar
0726    
seoyeon committed
45
      // const success = await login(user.email, user.password);
Kim, Chaerin's avatar
.    
Kim, Chaerin committed
46
      console.log(data);
seoyeon's avatar
0726    
seoyeon committed
47
      setSuccess(true);
Kim, Chaerin's avatar
Kim, Chaerin committed
48
    } catch (error) {
seoyeon's avatar
0802    
seoyeon committed
49
      catchErrors(error, setError);
Kim, Chaerin's avatar
Kim, Chaerin committed
50
    } finally {
이재연's avatar
s    
이재연 committed
51
      setLoading(false);
Kim, Chaerin's avatar
Kim, Chaerin committed
52
53
    }
  }
seoyeon's avatar
0716    
seoyeon committed
54
55
  if (success) {
    alert('회원가입 되었습니다.')
seoyeon's avatar
0724    
seoyeon committed
56
    return <Redirect to="/" />;
seoyeon's avatar
0716    
seoyeon committed
57
58
59
  }


seoyeon's avatar
0802    
seoyeon committed
60
  const { name, id, password, checkpw, phone } = user;
Kim, Chaerin's avatar
Kim, Chaerin committed
61
62
  return (
    <div className="modal-content">
seoyeon's avatar
0802    
seoyeon committed
63
        {error && <div className="alert alert-danger">{error}</div>}
Kim, Chaerin's avatar
Kim, Chaerin committed
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
91
92
      <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
93
              id="text"
Kim, Chaerin's avatar
Kim, Chaerin committed
94
              type="text"
Kim, Chaerin's avatar
서버    
Kim, Chaerin committed
95
              name="email"
Kim, Chaerin's avatar
Kim, Chaerin committed
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
              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
126
            <label className="p-1">전화번호</label>
Kim, Chaerin's avatar
Kim, Chaerin committed
127
128
129
130
131
            <input
              className="form-control"
              id="phone"
              type="text"
              name="phone"
132
              placeholder="'-'을 제외하고 입력하세요"
Kim, Chaerin's avatar
Kim, Chaerin committed
133
134
135
136
137
              value={phone}
              onChange={handleChange}
            />
          </div>
        </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
138
        {console.log(disabled)}
Kim, Chaerin's avatar
Kim, Chaerin committed
139
140
141
142
        <div className="modal-footer">
          <button type="submit" className="btn btn-primary" disabled={disabled}>
            회원가입
          </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
143
        </div>
seoyeon's avatar
0802    
seoyeon committed
144
      </form> 
Kim, Chaerin's avatar
Kim, Chaerin committed
145
146
    </div>
  );
Kim, Chaerin's avatar
Kim, Chaerin committed
147
148
149
};

export default Signup;