SignUp.tsx 4.26 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
import React, { FormEvent, useEffect, useState } from "react";
import { Navigate } from "react-router-dom";
import { authApi } from "../apis";
import { catchErrors } from "../helpers";
import { SpinnerIcon } from "../icons";
import { SignupUser } from "../types";
Jiwon Yoon's avatar
Jiwon Yoon committed
7

Yoon, Daeki's avatar
Yoon, Daeki committed
8
9
10
11
12
13
export const SignUp = () => {
  const [user, setUser] = useState<SignupUser & { password2: string }>({
    name: "",
    email: "",
    password: "",
    password2: "",
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
14
  });
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
15

Lee SeoYeon's avatar
Lee SeoYeon committed
16
17
  const [error, setError] = useState("");
  const [disabled, setDisabled] = useState(false);
Yoon, Daeki's avatar
Yoon, Daeki committed
18
  const [loading, setLoading] = useState(false);
Lee SeoYeon's avatar
Lee SeoYeon committed
19
  const [success, setSuccess] = useState(false);
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
20
21

  useEffect(() => {
Lee SeoYeon's avatar
Lee SeoYeon committed
22
23
    setDisabled(!(user.name && user.email && user.password && user.password2));
  }, [user]);
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
24
25

  function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
Lee SeoYeon's avatar
Lee SeoYeon committed
26
27
    const { id, value } = event.target;
    setUser({ ...user, [id]: value });
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
28
  }
Lee SeoYeon's avatar
Lee SeoYeon committed
29

Yoon, Daeki's avatar
Yoon, Daeki committed
30
  async function handleSubmit(event: FormEvent) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
31
32
    event.preventDefault();
    try {
Yoon, Daeki's avatar
Yoon, Daeki committed
33
34
35
36
37
38
39
      setError("");
      console.log("checkPassword:", passwordMatch());
      console.log("user data", user);
      if (passwordMatch()) {
        const { password2, ...sUser } = user;
        setLoading(true);
        const res = await authApi.signup(sUser);
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
40
        console.log("서버연결됬나요", res);
Lee SeoYeon's avatar
Lee SeoYeon committed
41
42
43
        console.log("회원가입");
        setSuccess(true);
        setError("");
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
44
45
      }
    } catch (error) {
Lee SeoYeon's avatar
Lee SeoYeon committed
46
      console.log("에러발생");
Yoon, Daeki's avatar
Yoon, Daeki committed
47
      catchErrors(error, setError);
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
48
    } finally {
Yoon, Daeki's avatar
Yoon, Daeki committed
49
      setLoading(false);
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
50
51
    }
  }
Lee SeoYeon's avatar
Lee SeoYeon committed
52

Yoon, Daeki's avatar
Yoon, Daeki committed
53
  function passwordMatch() {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
54
    if (user.password !== user.password2) {
Yoon, Daeki's avatar
Yoon, Daeki committed
55
      setError("비밀번호가 일치하지않습니다");
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
56
57
58
59
60
61
62
      console.log("password fail");
      return false;
    } else {
      console.log("password match");
      return true;
    }
  }
Lee SeoYeon's avatar
Lee SeoYeon committed
63

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
64
  if (success) {
Lee SeoYeon's avatar
Lee SeoYeon committed
65
    alert("회원가입 되었습니다");
Yoon, Daeki's avatar
Yoon, Daeki committed
66
    return <Navigate to={"/login"} replace />;
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
67
68
69
  }

  return (
Yoon, Daeki's avatar
Yoon, Daeki committed
70
71
72
    <div className="flex flex-col items-center mt-3">
      <div className="text-2xl">회원 가입</div>
      <form onSubmit={handleSubmit} className="flex flex-col mt-5 w-80">
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
73
74
75
76
        <label className="block text-gray-700 text-sm font-bold mb-2 mt-3">
          이름
        </label>
        <input
Yoon, Daeki's avatar
Yoon, Daeki committed
77
          className="shadow appearance-none border rounded py-2 px-3 text-gray-70"
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
78
79
80
81
82
          id="name"
          type="text"
          placeholder="이름을 입력하세요"
          onChange={handleChange}
        />
Yoon, Daeki's avatar
Yoon, Daeki committed
83
84
85
86
        <label
          htmlFor="email"
          className="block text-gray-700 text-sm font-bold mb-2 mt-3"
        >
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
87
88
89
          이메일
        </label>
        <input
Yoon, Daeki's avatar
Yoon, Daeki committed
90
          className="shadow appearance-none border rounded py-2 px-3 text-gray-70"
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
91
92
93
          id="email"
          type="text"
          placeholder="이메일을 입력하세요"
Yoon, Daeki's avatar
Yoon, Daeki committed
94
          autoComplete="username"
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
95
96
97
          onChange={handleChange}
        />

Yoon, Daeki's avatar
Yoon, Daeki committed
98
99
100
101
        <label
          htmlFor="password"
          className="block text-gray-700 text-sm font-bold mb-2 mt-3"
        >
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
102
103
104
105
106
107
          비밀번호
        </label>
        <input
          className="shadow appearance-none border rounded py-2 px-3 text-gray-70"
          id="password"
          type="password"
Yoon, Daeki's avatar
Yoon, Daeki committed
108
          autoComplete="new-password"
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
109
110
111
112
          placeholder="비밀번호를 입력하세요"
          onChange={handleChange}
        />

Yoon, Daeki's avatar
Yoon, Daeki committed
113
114
115
116
        <label
          htmlFor="password2"
          className="block text-gray-700 text-sm font-bold mb-2 mt-3"
        >
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
117
118
119
120
121
122
          비밀번호 확인
        </label>
        <input
          className="shadow appearance-none border rounded py-2 px-3 text-gray-70"
          id="password2"
          type="password"
Yoon, Daeki's avatar
Yoon, Daeki committed
123
          autoComplete="new-password"
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
124
125
126
          placeholder="비밀번호를 다시 입력하세요"
          onChange={handleChange}
        />
Yoon, Daeki's avatar
Yoon, Daeki committed
127
128
129
130
131
        {error && (
          <div className="text-red-500 text-sm">
            <p>{error}</p>
          </div>
        )}
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
132
133
        <div className="text-center mt-3">
          <button
Lee SeoYeon's avatar
Lee SeoYeon committed
134
            type="submit"
Yoon, Daeki's avatar
Yoon, Daeki committed
135
            className="bg-themeColor text-white border rounded w-100 py-2 px-3 mt-3"
Lee SeoYeon's avatar
Lee SeoYeon committed
136
            disabled={disabled}
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
137
          >
Yoon, Daeki's avatar
Yoon, Daeki committed
138
139
140
            {loading && (
              <SpinnerIcon className="animate-spin h-5 w-5 mr-1 text-slate" />
            )}
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
141
142
143
            회원가입
          </button>
        </div>
Yoon, Daeki's avatar
Yoon, Daeki committed
144
      </form>
Jiwon Yoon's avatar
Jiwon Yoon committed
145
    </div>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
146
147
  );
};