add some error checking

This commit is contained in:
Eduardo Quiros 2022-09-01 01:59:00 -06:00
parent 20198a9071
commit cda59f286b
No known key found for this signature in database
GPG Key ID: B77F36C3F12720B4
1 changed files with 83 additions and 26 deletions

View File

@ -1,8 +1,9 @@
import React, { Component, Fragment } from 'react'; import React, { Component, Fragment, useRef } from 'react';
import Cookies from 'universal-cookie'; import Cookies from 'universal-cookie';
import { InputText } from 'primereact/inputtext'; import { InputText } from 'primereact/inputtext';
import { Button } from 'primereact/button'; import { Button } from 'primereact/button';
import { Dialog } from 'primereact/dialog'; import { Dialog } from 'primereact/dialog';
import { Toast } from 'primereact/toast'
const baseUrl = 'http://localhost:4000/user/loginUser'; const baseUrl = 'http://localhost:4000/user/loginUser';
const cookies = new Cookies(); const cookies = new Cookies();
@ -20,6 +21,8 @@ class LogInUser extends Component {
errorPassword: false, errorPassword: false,
logged: null, logged: null,
showPwdResetDialog: false, showPwdResetDialog: false,
resetEmail: '',
errorResetEmail: false,
}; };
} }
@ -32,6 +35,12 @@ class LogInUser extends Component {
}); });
}; };
handleResetEmailChange = async (event) => {
this.setState({
resetEmail: event.target.value,
});
};
validaciones = (data) => { validaciones = (data) => {
let error = false; let error = false;
if (data.email == '') { if (data.email == '') {
@ -54,10 +63,20 @@ class LogInUser extends Component {
errorPassword: false, errorPassword: false,
}); });
} }
return error; return error;
}; };
validarResetEmail = (data) => {
let error = false;
if (data.email == '') {
this.setState({
errorResetEmail: true,
});
error = true;
}
return error;
}
iniciarSesion = async () => { iniciarSesion = async () => {
const data = { const data = {
email: this.state.form.email, 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 = () => { resetPassword = () => {
const data = { const data = {
email: this.state.form.email, email: this.state.resetEmail,
}; };
const tenant = fetch('http://localhost:4000/user/allUsers', if (!this.validarResetEmail(data)) {
fetch('http://localhost:4000/user/allUsers',
{ method: 'GET' }) { method: 'GET' })
.then((response) => response.json()) .then((response) => response.json())
.then((response) => response.message) .then((response) => response.message)
.then((response) => { .then((response) => {
response = response.filter((value) => value.email === data.email) response = response.find((value) => value.email === data.email)
}).catch((error) => { this.sendResetRequest(response);
})
.catch((error) => {
console.log(error); console.log(error);
}); });
console.log(tenant); }
} }
pwdResetDialogFooter = ( pwdResetDialogFooter = (
@ -152,7 +193,10 @@ class LogInUser extends Component {
label='Cerrar' label='Cerrar'
icon='pi pi-times' icon='pi pi-times'
className='p-button-text' className='p-button-text'
onClick={() => this.props.showPwdResetDialog = false} onClick={() => this.setState({
errorResetEmail: false,
showPwdResetDialog: false
})}
/> />
</> </>
) )
@ -256,32 +300,45 @@ class LogInUser extends Component {
<Button <Button
label="Restablecer Contraseña" label="Restablecer Contraseña"
className="p-button-link" className="p-button-link"
onClick={() => this.props.showPwdResetDialog = true} onClick={() => this.setState({ showPwdResetDialog: true })}
/> />
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<Dialog <Dialog
visible={this.props.showPwdResetDialog} visible={this.state.showPwdResetDialog}
style={{ width: '50vw' }} style={{ width: '50vw' }}
header="Restablecer Contraseña" header="Restablecer Contraseña"
modal modal
className="p-fluid" className="p-fluid"
footer={this.pwdResetDialogFooter} footer={this.pwdResetDialogFooter}
onHide={() => this.props.showPwdResetDialog = false} onHide={() => this.setState({ showPwdResetDialog: true })}
> >
<div className='flex align-items-center justify-content-center'> <div className="p-fluid formgrid grid">
<i <div className="field col-12 md:col-12">
className='pi pi-exclamation-triangle mr-3' <div className="p-0 col-12 md:col-12">
style={{ fontSize: '2rem' }} <div className="p-inputgroup">
/> <span className="p-inputgroup-addon p-button p-icon-input-khaki">
<i className="pi pi-user"></i>
</span>
<InputText <InputText
type='email' type='email'
style={{ width: '100%' }} style={{ width: '100%' }}
placeholder='Correo electrónico' placeholder='Correo electrónico'
onChange={this.handleResetEmailChange}
className={this.state.errorResetEmail ? 'p-invalid' : ''}
/> />
</div> </div>
{this.state.errorResetEmail && (
<small className="p-invalid">
Correo electrónico es requerido
</small>)}
</div>
</div>
</div>
<div className='flex align-items-center justify-content-center'>
</div>
</Dialog> </Dialog>
</Fragment> </Fragment>
); );