import axios from "axios";
import { useEffect, useState } from "react";
import { Redirect } from "react-router-dom";
import userApi from "../apis/user.api";
import catchErrors from "../context/catchError";
const INIT_USER = {
name: "",
email: "",
password: "",
checkpw: "",
phone: "",
};
const Signup = () => {
const [user, setUser] = useState(INIT_USER);
const [error, setError] = useState("");
const [disabled, setDisabled] = useState(false);
const [success, setSuccess] = useState(false);
useEffect(() => {
setDisabled(
!(
user.name &&
user.email &&
user.password &&
user.checkpw
)
);
}, [user]);
function handleChange(event) {
const { name, value } = event.target;
setUser({ ...user, [name]: value });
}
async function handleSubmit() {
console.log('회원가입')
try {
const data = await userApi.signup(user);
console.log(data)
setSuccess(true);
setError("");
} catch (error) {
catchErrors(error, setError);
} finally {
// setLoading(false);
}
}
function checkPassword(event) {
const p1 = user.password;
const p2 = user.checkpw;
if (p1 !== p2) {
event.preventDefault();
alert("비밀번호가 일치하지 않습니다.");
return false;
} else {
setSuccess(true);
return true;
}
}
if (success) {
alert("회원가입 되었습니다.");