SignUp.tsx 4.24 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
70
  }

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

Yoon, Daeki's avatar
Yoon, Daeki committed
100
101
102
103
        <label
          htmlFor="password"
          className="block text-gray-700 text-sm font-bold mb-2 mt-3"
        >
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
104
105
106
107
108
109
          비밀번호
        </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
110
          autoComplete="new-password"
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
111
112
113
114
          placeholder="비밀번호를 입력하세요"
          onChange={handleChange}
        />

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