From cda59f286b21336319637cd0e169c8eac9ca5d57 Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Thu, 1 Sep 2022 01:59:00 -0600 Subject: [PATCH] add some error checking --- web-ui/web-react/src/components/LogInUser.js | 109 ++++++++++++++----- 1 file changed, 83 insertions(+), 26 deletions(-) diff --git a/web-ui/web-react/src/components/LogInUser.js b/web-ui/web-react/src/components/LogInUser.js index 1160d91f..37778fc1 100644 --- a/web-ui/web-react/src/components/LogInUser.js +++ b/web-ui/web-react/src/components/LogInUser.js @@ -1,8 +1,9 @@ -import React, { Component, Fragment } from 'react'; +import React, { Component, Fragment, useRef } from 'react'; import Cookies from 'universal-cookie'; import { InputText } from 'primereact/inputtext'; import { Button } from 'primereact/button'; import { Dialog } from 'primereact/dialog'; +import { Toast } from 'primereact/toast' const baseUrl = 'http://localhost:4000/user/loginUser'; const cookies = new Cookies(); @@ -20,6 +21,8 @@ class LogInUser extends Component { errorPassword: false, logged: null, showPwdResetDialog: false, + resetEmail: '', + errorResetEmail: false, }; } @@ -32,6 +35,12 @@ class LogInUser extends Component { }); }; + handleResetEmailChange = async (event) => { + this.setState({ + resetEmail: event.target.value, + }); + }; + validaciones = (data) => { let error = false; if (data.email == '') { @@ -54,10 +63,20 @@ class LogInUser extends Component { errorPassword: false, }); } - return error; }; + validarResetEmail = (data) => { + let error = false; + if (data.email == '') { + this.setState({ + errorResetEmail: true, + }); + error = true; + } + return error; + } + iniciarSesion = async () => { const data = { email: this.state.form.email, @@ -124,20 +143,42 @@ class LogInUser extends Component { } }; + sendResetRequest = (user) => { + if (user) { + fetch(`${passwordResetUrl}/${user._id}`, { + method: 'PUT', + cache: 'no-cache', + body: JSON.stringify(user), + headers: { + 'Content-Type': 'application/json', + }, + }).then((response) => { + if (response.status != 200) + console.log(`Ocurrió un error con el servicio: ${response.status}`); + else return response.json(); + }).then((_response) => { + console.log('Se ha enviado un correo con la información para resetear la contraseña'); + }).catch((error) => { console.log(error) }) + } + }; + resetPassword = () => { const data = { - email: this.state.form.email, + email: this.state.resetEmail, }; - const tenant = fetch('http://localhost:4000/user/allUsers', - { method: 'GET' }) - .then((response) => response.json()) - .then((response) => response.message) - .then((response) => { - response = response.filter((value) => value.email === data.email) - }).catch((error) => { - console.log(error); - }); - console.log(tenant); + if (!this.validarResetEmail(data)) { + fetch('http://localhost:4000/user/allUsers', + { method: 'GET' }) + .then((response) => response.json()) + .then((response) => response.message) + .then((response) => { + response = response.find((value) => value.email === data.email) + this.sendResetRequest(response); + }) + .catch((error) => { + console.log(error); + }); + } } pwdResetDialogFooter = ( @@ -152,7 +193,10 @@ class LogInUser extends Component { label='Cerrar' icon='pi pi-times' className='p-button-text' - onClick={() => this.props.showPwdResetDialog = false} + onClick={() => this.setState({ + errorResetEmail: false, + showPwdResetDialog: false + })} /> ) @@ -256,31 +300,44 @@ class LogInUser extends Component {