import { useEffect, useState, useRef } from 'react'
import { Redirect } from 'react-router-dom'
import userApi from '../apis/user.api'
import catchErrors from '../context/catchError'
import { handleLogin } from '../context/auth'
const INIT_USER = {
email: '',
password: '',
}
const Login = () => {
const [user, setUser] = useState(INIT_USER)
const [disabled, setDisabled] = useState(true)
const [error, setError] = useState('')
const [success, setSuccess] = useState(false)
// const [modalopen, setmodalOpen] = useState(false)
// const loginref = useRef()
useEffect(() => {
const isUser = Object.values(user).every((el) => Boolean(el))
isUser ? setDisabled(false) : setDisabled(true)
// window.addEventListener('click', handleModal)
// return () => {
// window.removeEventListener('click', handleModal)
// }
}, [user])
function handleChange(event) {
const { name, value } = event.target
setUser({ ...user, [name]: value })
}
// const handleModal = (e) => {
// if (
// modalopen &&
// (!loginref.current || !loginref.current.contains(e.target))
// )
// setmodalOpen(false)
// }
async function handleSubmit(e) {
e.preventDefault()
console.log('로그인')
try {
// setLoading(true);
// setError("");
const data = await userApi.login(user)
console.log(data)
handleLogin(data.id)
setSuccess(true)
} catch (error) {
console.log('error in login', error)
catchErrors(error, setError)
} finally {
// setLoading(false);
}
}
if (success) {
alert('로그인 되었습니다')
return