From 3df047c5326fb5f669328843500e9ae9d5e6b04b Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Tue, 30 Aug 2022 18:40:31 -0600 Subject: [PATCH] format --- web-ui/web-react/src/components/LogInUser.js | 413 ++++++++++--------- 1 file changed, 210 insertions(+), 203 deletions(-) diff --git a/web-ui/web-react/src/components/LogInUser.js b/web-ui/web-react/src/components/LogInUser.js index e909a88e..d79bac90 100644 --- a/web-ui/web-react/src/components/LogInUser.js +++ b/web-ui/web-react/src/components/LogInUser.js @@ -4,224 +4,231 @@ import { InputText } from 'primereact/inputtext'; import { Button } from 'primereact/button'; import { Toast } from 'primereact/toast'; -const baseUrl = "http://localhost:4000/user/loginUser"; +const baseUrl = 'http://localhost:4000/user/loginUser'; const cookies = new Cookies(); class LogInUser extends Component { - constructor(props) { - super(props); - this.state = { - form: { - email: '', - password: '' - }, - errorEmail: false, - errorPassword: false, - logged: null - } + constructor(props) { + super(props); + this.state = { + form: { + email: '', + password: '', + }, + errorEmail: false, + errorPassword: false, + logged: null, + }; + } + + handleChange = async (e) => { + await this.setState({ + form: { + ...this.state.form, + [e.target.name]: e.target.value, + }, + }); + }; + + validaciones = (data) => { + let error = false; + if (data.email == '') { + this.setState({ + errorEmail: true, + }); + error = true; + } else { + this.setState({ + errorEmail: false, + }); + } + if (data.password == '') { + this.setState({ + errorPassword: true, + }); + error = true; + } else { + this.setState({ + errorPassword: false, + }); } - handleChange = async e => { - await this.setState({ - form: { - ...this.state.form, - [e.target.name]: e.target.value - } - }); - } + return error; + }; - validaciones = (data) => { - let error = false; - if (data.email == '') { - this.setState({ - errorEmail: true - }) - error = true; - } else { - this.setState({ - errorEmail: false - }) - } - if (data.password == '') { - this.setState({ - errorPassword: true - }) - error = true; - } else { - this.setState({ - errorPassword: false - }) - } - - return error; - - } - - iniciarSesion = async () => { - const data = { - email: this.state.form.email, - password: this.state.form.password - } - - console.log(data); - - if (!this.validaciones(data)) { - this.setState({ - email: true, - password: true - }) - await fetch(baseUrl, { - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(data), - headers: { - 'Content-Type': 'application/json' - } - }) - .then(response => { - if (response.status != 201) - console.log('Ocurrió un error con el servicio: ' + response.status); - else return response.json(); - }) - .then(response => { - console.log(response.message); - - if (response.message) { - const user = response.message; - - if(user.user_type == '1' || user.user_type == '2'){ - cookies.set('id', user._id, { path: "/" }); - cookies.set('name', user.name, { path: "/" }); - cookies.set('email', user.email, { path: "/" }); - cookies.set('type', user.user_type, { path: "/" }); - if (user.user_type != '1') { - cookies.set('community_id', user.community_id, { path: "/" }); - } - // alert(`Bienvenido ${user.name}`); - document.getElementById('notification').hidden = true; - document.getElementById('notification2').hidden = false; - - document.getElementById("message2").innerHTML = `Bienvenido ${user.name}`; - - window.location.href = "/"; - } - window.location.href = "/page404"; - - - } else { - document.getElementById('notification2').hidden = true; - document.getElementById('notification').hidden = false; - - //alert('El usuario o la contraseña no son correctos'); - document.getElementById("message").innerHTML = "El usuario o la contraseña son incorrectos"; - } - }) - .catch(error => { - console.log(error); - }) - } - } - - componentDidMount() { - if (cookies.get('email')) { - window.location.href = "/"; - } - } - - renderErrorMessage = (name) => - name === this.state.errorMessages.name && ( -
{this.state.errorMessages.message}
- ); - - errors = { - email: "Correo requerido", - pass: "Contraseña requerida" + iniciarSesion = async () => { + const data = { + email: this.state.form.email, + password: this.state.form.password, }; - render() { - return ( + console.log(data); - + if (!this.validaciones(data)) { + this.setState({ + email: true, + password: true, + }); + await fetch(baseUrl, { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(data), + headers: { + 'Content-Type': 'application/json', + }, + }) + .then((response) => { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else return response.json(); + }) + .then((response) => { + console.log(response.message); + if (response.message) { + const user = response.message; -
-
- - + if (user.user_type == '1' || user.user_type == '2') { + cookies.set('id', user._id, { path: '/' }); + cookies.set('name', user.name, { path: '/' }); + cookies.set('email', user.email, { path: '/' }); + cookies.set('type', user.user_type, { path: '/' }); + if (user.user_type != '1') { + cookies.set('community_id', user.community_id, { path: '/' }); + } + // alert(`Bienvenido ${user.name}`); + document.getElementById('notification').hidden = true; + document.getElementById('notification2').hidden = false; + document.getElementById( + 'message2', + ).innerHTML = `Bienvenido ${user.name}`; -
-
+ window.location.href = '/'; + } + window.location.href = '/page404'; + } else { + document.getElementById('notification2').hidden = true; + document.getElementById('notification').hidden = false; -
-
Iniciar Sesión
-
- -
- -
-
- - - - -
- {this.state.errorEmail && ( - Correo electrónico es requerido - )} - -
-
-
- -
-
- - - - -
- {this.state.errorPassword && ( - Contraseña es requerida - )} -
-
- - -
-
-
-
-
- - - - ); + //alert('El usuario o la contraseña no son correctos'); + document.getElementById('message').innerHTML = + 'El usuario o la contraseña son incorrectos'; + } + }) + .catch((error) => { + console.log(error); + }); } + }; + + componentDidMount() { + if (cookies.get('email')) { + window.location.href = '/'; + } + } + + renderErrorMessage = (name) => + name === this.state.errorMessages.name && ( +
{this.state.errorMessages.message}
+ ); + + errors = { + email: 'Correo requerido', + pass: 'Contraseña requerida', + }; + + render() { + return ( + +
+
+ + +
+
+
+
Iniciar Sesión
+
+
+ +
+
+ + + + +
+ {this.state.errorEmail && ( + + Correo electrónico es requerido + + )} +
+
+
+ +
+
+ + + + +
+ {this.state.errorPassword && ( + + Contraseña es requerida + + )} +
+
+ + +
+
+
+
+
+ ); + } } export default LogInUser;