signup.tsx 3.56 KB
Newer Older
1
2
3
4
5
import React, { FormEvent, useEffect, useState } from "react";
import { Link, useNavigate } from "react-router-dom";
import { authApi } from "../apis";
import { catchErrors } from "../helpers";
import { SignupUser } from "../types";
6
7

export default function Signup() {
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  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);
  const navigate = useNavigate();

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

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

  async function handleSubmit(event: FormEvent) {
    event.preventDefault();
    try {
      console.log("checkPassword:", passwordMatch());
      console.log("user data", user);
      if (passwordMatch()) {
        const { password2, ...sUser } = user;
        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) {
      alert("비밀번호가 일치하지않습니다");
      console.log("password fail");
      return false;
    } else {
      console.log("password match");
      return true;
    }
  }

  if (success) {
    alert("회원가입 되었습니다");
    navigate("/login", { replace: true });
  }

68
  return (
69
70
    <div className="flex flex-col items-center">
      <div className="p-12 md:w-40 mt-8 bg-red-400 rounded-2xl">
71
72
        <Link to="/">Travel Report</Link>
      </div>
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
      <form
        onSubmit={handleSubmit}
        className="flex flex-col items-center mt-16 gap-y-4"
      >
        <input
          className="placeholder:text-slate-300 bg-white border border-slate-500 rounded-2xl py-2 pl-9 pr-3 focus:border-black"
          placeholder="이름"
          type="text"
          name="name"
          onChange={handleChange}
        />

        <input
          className="placeholder:text-slate-300 bg-white border border-slate-500 rounded-2xl py-2 pl-9 pr-3 focus:border-black"
          placeholder="이메일"
          type="email"
          name="email"
          onChange={handleChange}
        />

93
94
95
96
97
98
        <input
          className="placeholder:text-slate-300
          bg-white border border-slate-500 rounded-2xl
          py-2 pl-9 pr-3
          focus:border-black"
          placeholder="비밀번호"
99
          type="password"
100
101
          name="password"
          // value={password}
102
          onChange={handleChange}
103
104
105
106
107
108
109
        />

        <input
          className="placeholder:text-slate-300
          bg-white border border-slate-500 rounded-2xl
          py-2 pl-9 pr-3
          focus:border-black"
110
111
112
          placeholder="비밀번호 확인"
          type="password"
          name="password2"
113
          // value={password}
114
          onChange={handleChange}
115
        />
116
117
118
119
120
121
        <button
          disabled={disabled}
          className="place-self-center py-3 border-b border-white"
        >
          회원가입
        </button>
122
123
124
125
      </form>
    </div>
  );
}