FrontPastel/frontend/src/Sesion/LoginForm.jsx

167 lines
5.3 KiB
React
Raw Normal View History

2024-04-22 04:41:09 +00:00
import React, { useState } from 'react';
import './LoginForm.css';
2024-05-11 19:26:44 +00:00
import { redirect } from 'react-router-dom';
import axios from "axios";
import { useNavigate } from "react-router-dom";
2024-05-11 21:54:21 +00:00
//import PopupV from "/src/Popups/PopupLoginValido.jsx";
//import PopupIv from "./Popups/PopupLoginInvalido.jsx";
2024-05-11 19:26:44 +00:00
function LoginForm(props) {
const navigate = useNavigate();
const [Cargando, setCargando] = useState(false)
const [datosFormulario, setDatosFormulario] = useState({
correo: '',
password: ''
})
2024-04-22 04:41:09 +00:00
const [loginVisible, setLoginVisible] = useState(true);
2024-04-26 19:49:27 +00:00
const [registerVisible, setRegisterVisible] = useState(false);
const [passwordVisible, setPasswordVisible] = useState(false);
2024-04-29 00:02:31 +00:00
2024-05-11 19:26:44 +00:00
const redirectToRegister = () => {
navigate("/Registro");
2024-04-22 04:41:09 +00:00
};
2024-05-11 19:26:44 +00:00
const redirectToRecuperarContra = () => {
navigate("/RecuperarContraseña");
2024-04-26 19:49:27 +00:00
};
2024-05-11 19:26:44 +00:00
const mostrarAlertaLoginExitoso=(nomb)=>{
swal({
title: "Inicio de sesion Exitoso",
text: "Bienvenido "+nomb+".",
icon: "success",
button: "Aceptar"
}).then(respuesta=>{
if(respuesta){
redirectToHome();
}
})
}
const mostrarAlertaLoginFallido=()=>{
swal({
title: "Inicio de sesion fallido",
text: "Usuario o Contraseña Incorrecta.",
icon: "error",
button: "Aceptar"
});
}
const mostrarAlertaLoginSinDatos=()=>{
swal({
title: "Inicio de sesion fallido",
text: "Introduzca los datos que se le piden.",
icon: "error",
button: "Aceptar"
});
}
const loginUsuario = async (evento) => {
evento.preventDefault();
try{
2024-05-11 21:54:21 +00:00
const response = await axios.post('http://localhost/Login',{datosFormulario})
2024-05-11 19:26:44 +00:00
console.log(response.data)
console.log("c = " +datosFormulario.correo+" p = "+datosFormulario.password)
if(datosFormulario.correo && datosFormulario.password){
if (response.data === 'Invalido') {
// Si la respuesta es 'Invalido', limpiar los campos del formulario
mostrarAlertaLoginFallido();
setDatosFormulario({
correo: '',
password: ''
});
} else {
// Si la respuesta es 'Valido', puedes realizar las acciones deseadas
setNombre(response.data.nombre);
console.log(response.data.nombre);
mostrarAlertaLoginExitoso(response.data.nombre);
//abrirPopupV()
}
2024-05-11 21:54:21 +00:00
}else{
2024-05-11 19:26:44 +00:00
mostrarAlertaLoginSinDatos();
}
return response.data
} catch(error){
throw error
}
}
const [nombre,setNombre]=useState('')
const [mostrarPopupV, setMostrarPopupV] = useState(false);
const abrirPopupV = () => {
setMostrarPopupV(true);
};
const [mostrarPopupIv, setMostrarPopupIv] = useState(false);
const abrirPopupIv = () => {
setMostrarPopupIv(true);
};
const cambiosFormulario = (evento) => {
//console.log(evento.target)
const {name,value} = evento.target
setDatosFormulario ({...datosFormulario, [name]: value})
setMostrarPopupIv(false);
}
2024-05-11 21:54:21 +00:00
const redirectToHome = () => {
navigate("/");
};
2024-05-06 22:53:21 +00:00
2024-04-22 04:41:09 +00:00
return (
<>
2024-05-06 22:53:21 +00:00
<body className='body-login'>
<div className="wrapper">
<div className="form-box">
<div className="login-container" id="login" style={{ left: loginVisible ? '4px' : '-510px', opacity: loginVisible ? 1 : 0 }}>
<form>
<div className="top">
2024-05-11 19:26:44 +00:00
<span>No tienes cuenta? <a href="" onClick={redirectToRegister}>Registrate</a></span>
2024-05-06 22:53:21 +00:00
<header className='header-login'>Inicia Sesión</header>
</div>
<div className="input-box">
2024-05-11 21:54:21 +00:00
<input type="text" className="input-field" placeholder="Correo" onChange={cambiosFormulario} name="correo" value={datosFormulario.correo}/>
2024-05-06 22:53:21 +00:00
<i className="bx bx-user"></i>
2024-04-29 00:02:31 +00:00
</div>
2024-05-06 22:53:21 +00:00
<div className="input-box">
2024-05-11 21:54:21 +00:00
<input type="password" className="input-field" placeholder="Contraseña" onChange={cambiosFormulario} name="password" value={datosFormulario.password}/>
2024-05-06 22:53:21 +00:00
<i className="bx bx-lock-alt"></i>
</div>
<div className="input-box">
2024-05-11 21:54:21 +00:00
<button type="button" className="submit" onClick={loginUsuario} disabled={Cargando}>Entrar</button>
2024-04-29 00:02:31 +00:00
</div>
2024-05-06 22:53:21 +00:00
<div className="two-col">
<div className="one">
<input type="checkbox" id="login-check" />
<label htmlFor="login-check"> Recuerdame</label>
</div>
<div className="two">
2024-05-11 19:26:44 +00:00
<span><a href="" onClick={redirectToRecuperarContra}>Olvidaste la contraseña?</a></span>
2024-05-06 22:53:21 +00:00
</div>
</div>
2024-05-11 21:54:21 +00:00
<Box m={5}>
{mostrarPopupV && <PopupV nombre ={nombre} onClose={() => setMostrarPopupV(false)} />}
</Box>
<Box m={5}>
{mostrarPopupIv && <PopupIv nombre ={nombre} onClose={() => setMostrarPopupIv(false)} />}
</Box>
2024-05-06 22:53:21 +00:00
</form>
</div>
2024-04-26 19:49:27 +00:00
</div>
2024-04-22 04:41:09 +00:00
</div>
2024-05-06 22:53:21 +00:00
</body>
2024-04-22 04:41:09 +00:00
</>
);
}
2024-05-06 22:53:21 +00:00
export default LoginForm;