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

export default function Signup() {
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  const [user, setUser] = useState<SignupUser & { password2: string }>({
    name: "",
    email: "",
    password: "",
    password2: "",
  });

  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [disabled, setDisabled] = useState(false);
  const [success, setSuccess] = useState(false);

  useEffect(() => {
    setDisabled(!(user.name && user.email && user.password && user.password2));
  }, [user]);

  function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
    const { name, value } = event.currentTarget;
    setUser({ ...user, [name]: value });
  }

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

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

  if (success) {
    alert("회원가입 되었습니다");
67
    return <Navigate to={"/login"} replace />;
68
69
  }

70
  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
71
72
    <div className="flex flex-col">
      <div className="flex place-items-strat ml-2 mt-8 text-center text-2xl ">
Kim, MinGyu's avatar
Kim, MinGyu committed
73
        <Link to="/">회원가입</Link>
74
      </div>
75

Kim, MinGyu's avatar
Kim, MinGyu committed
76
      <form onSubmit={handleSubmit} className="flex flex-col  mt-16 gap-y-4">
Kim, MinGyu's avatar
Kim, MinGyu committed
77
78
79
        <div className="flex flex-col ">
          <div className="border-0 border-y-2 border-black">
            <div className="h-16 flex ">
Kim, MinGyu's avatar
Kim, MinGyu committed
80
              <div className="whitespace-nowrap grid place-items-center w-32 lg:basis-1/5 shrink-0 border-0 border-r-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
81
82
                이름
              </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
83
              <div className="flex items-center mx-5">
Kim, MinGyu's avatar
Kim, MinGyu committed
84
                <input
Kim, MinGyu's avatar
Kim, MinGyu committed
85
                  className="h-10 w-4/5 lg:w-60 border-2 focus:border-black"
Kim, MinGyu's avatar
Kim, MinGyu committed
86
87
88
89
90
91
92
                  type="text"
                  name="name"
                  onChange={handleChange}
                />
              </div>
            </div>
            <div className="h-16 flex border-0 border-t-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
93
              <div className="whitespace-nowrap grid place-items-center w-32 lg:basis-1/5 shrink-0 border-0 border-r-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
94
95
                이메일
              </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
96
              <div className="flex items-center mx-5">
Kim, MinGyu's avatar
Kim, MinGyu committed
97
                <input
Kim, MinGyu's avatar
Kim, MinGyu committed
98
                  className=" w-4/5 h-10 lg:w-60  border-2 focus:border-black"
Kim, MinGyu's avatar
Kim, MinGyu committed
99
100
101
102
103
104
105
                  type="email"
                  name="email"
                  onChange={handleChange}
                />
              </div>
            </div>
            <div className="h-16 flex border-0 border-t-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
106
              <div className="whitespace-nowrap grid place-items-center w-32 lg:basis-1/5 shrink-0 border-0 border-r-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
107
108
                비밀번호
              </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
109
              <div className="flex items-center  mx-5">
Kim, MinGyu's avatar
Kim, MinGyu committed
110
                <input
Kim, MinGyu's avatar
Kim, MinGyu committed
111
                  className="w-4/5 h-10  lg:w-60  border-2 focus:border-black"
Kim, MinGyu's avatar
Kim, MinGyu committed
112
113
114
115
116
117
118
119
                  type="password"
                  name="password"
                  onChange={handleChange}
                />
              </div>
            </div>

            <div className="h-16 flex border-0 border-t-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
120
              <div className="whitespace-nowrap grid place-items-center w-32 lg:basis-1/5 shrink-0 border-0 border-r-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
121
122
123
                비밀번호 확인
              </div>

Kim, MinGyu's avatar
Kim, MinGyu committed
124
              <div className="flex items-center mx-5">
Kim, MinGyu's avatar
Kim, MinGyu committed
125
                <input
Kim, MinGyu's avatar
Kim, MinGyu committed
126
                  className="w-4/5 h-10 lg:w-60  border-2 focus:border-black"
Kim, MinGyu's avatar
Kim, MinGyu committed
127
128
129
130
131
132
                  type="password"
                  name="password2"
                  onChange={handleChange}
                />
              </div>
            </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
133
134
135
136
137
          </div>
          {error && (
            <div className="text-red-500 text-sm">
              <p>{error}</p>
            </div>
Yoon, Daeki's avatar
Yoon, Daeki committed
138
          )}
Kim, MinGyu's avatar
Kim, MinGyu committed
139

Kim, MinGyu's avatar
Kim, MinGyu committed
140
141
142
143
          <button
            disabled={disabled}
            className="border-b border-white mt-5 h-12 w-52 text-white text-lg bg-amber-400 place-self-center"
          >
Kim, MinGyu's avatar
Kim, MinGyu committed
144
145
146
147
148
149
            {loading && (
              <SpinnerIcon className="animate-spin h-5 w-5 mr-1 text-slate" />
            )}
            회원가입
          </button>
        </div>
150
151
152
153
      </form>
    </div>
  );
}