import axios from 'axios' import { useEffect, useState } from 'react' import { Link, 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(e) { e.preventDefault() console.log('회원가입') try { if (checkPassword()) { const data = await userApi.signup(user) console.log(data) setSuccess(true) setError('') } } catch (error) { console.log('error') catchErrors(error, setError) } finally { setLoading(false); } } function checkPassword(event) { const p1 = user.password const p2 = user.checkpw if (p1 !== p2) { // event.preventDefault() alert('비밀번호가 일치하지 않습니다.') window.location.href = '/login' return false } else { // alert('회원가입 되었습니다.') // window.location.href = '/' return true } } if(success) { alert('회원가입 되었습니다.') window.location.href = '/' } const { name, id, password, checkpw, phone } = user return (
{error &&
{error}
}
회원가입
{/* {console.log(disabled)} */}
) } export default Signup