import React, { Component } from 'react';
import Images from '../../assets/Images';
import { TextField, InputAdornment, Button, Typography, IconButton, Snackbar } from '@material-ui/core';
import Visibility from '@material-ui/icons/Visibility';
import VisibilityOff from '@material-ui/icons/VisibilityOff';
import MuiAlert from '@material-ui/lab/Alert';
import { withStyles } from '@material-ui/core/styles';
import api from '../../api';
const Alert = withStyles({
})((props) => );
class ResetPassword extends Component {
constructor(props) {
super(props)
this.state = {
password: '',
confirmPassword: '',
showPass: false,
showPass2: false,
errorPassword: false,
errorConfirmPassword: false,
msgPassword: 'Consists of 8 Characters with a Combination of Numbers',
msgConfirmPassword: 'Consists of 8 Characters with a Combination of Numbers',
userId: 0,
alert: false,
tipeAlert: '',
messageAlert: ''
}
}
componentDidMount() {
// console.log(this.props.match.params.id)
let userId = this.props.match.params.id
this.setState({ userId })
this.checkExpiredLink(userId)
// console.log(this.props)
}
checkExpiredLink(userId) {
api.create().isResetPassword(userId).then((response) => {
if (response.data) {
if (response.data.status == 'success') {
//
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
setTimeout(() => {
this.props.history.push('/login')
}, 1500);
})
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
isRegex(value) {
// const re = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/;
const re = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?([^\w\s]|[_])).{8,}$/
// const re = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#_?&])[A-Za-z\d@$!%*#?&]{1,}$/;
return re.test(String(value));
// return value
}
isEmail(email) {
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
validateReset() {
if (this.state.password.trim() == "") {
this.setState({ errorPassword: true, msgPassword: 'New Password Cannot be Empty' })
} else if (this.state.password.length < 8) {
this.setState({ errorPassword: true, msgPassword: 'Invalid password. Minimum length : 8' })
} else if (this.isEmail(this.state.password)) {
this.setState({ errorPassword: true, msgPassword: 'Invalid password. Should not be same as Email Address' })
} else if (!this.isRegex(this.state.password)) {
this.setState({ errorPassword: true, msgPassword: 'Invalid password. Must using combination of characters, letters and numbers' })
} else if (this.state.confirmPassword.trim() == "") {
this.setState({ errorConfirmPassword: true, msgConfirmPassword: 'Repeat Password Cannot be Empty' })
} else if (this.state.password !== this.state.confirmPassword) {
this.setState({ errorConfirmPassword: true, msgConfirmPassword: 'The password and password confirmation do not match' })
} else {
this.confirmPassword()
// console.log(this.state.password)
// console.log(this.state.confirmPassword)
}
}
confirmPassword() {
let payload = {
"password": this.state.password,
"confirm_password": this.state.confirmPassword,
"user_id": this.state.userId
}
api.create().resetPassword(payload).then((response) => {
if (response.data) {
if (response.ok) {
if (response.data.status === 'success') {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' }, () => {
setTimeout(() => {
this.props.history.push('/login')
}, 1000);
})
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' })
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
closeAlert() {
this.setState({ alert: false })
}
handleChange(e) {
let data = this.state
this.setState({ ...data, [e.target.name]: e.target.value })
if (e.target.name == "password") {
this.setState({ errorPassword: false, msgPassword: 'Consists of 8 Characters with a Combination of Numbers' })
} else if (e.target.name == "confirmPassword") {
this.setState({ errorConfirmPassword: false, msgConfirmPassword: 'Consists of 8 Characters with a Combination of Numbers' })
}
}
render() {
return (
this.closeAlert()}>
this.closeAlert()} severity={this.state.tipeAlert}>
{this.state.messageAlert}
New password *}
id="password"
type={this.state.showPass ? 'text' : 'password'}
name={"password"}
value={this.state.password}
onChange={(password) => {
this.handleChange(password)
}}
variant="outlined"
error={this.state.errorPassword}
style={{ width: 250, height: 51, marginTop: 32 }}
helperText={{this.state.msgPassword}}
InputProps={{
endAdornment:
this.setState({ showPass: !this.state.showPass })}
edge="end"
>
{this.state.showPass ? : }
,
}}
/>
Repeat New Password *}
id="confirmPassword"
type={this.state.showPass2 ? 'text' : 'password'}
name={"confirmPassword"}
value={this.state.confirmPassword}
onChange={(confirmPassword) => {
this.handleChange(confirmPassword)
}}
variant="outlined"
error={this.state.errorConfirmPassword}
style={{ width: 250, height: 51, marginTop: 45 }}
helperText={{this.state.msgConfirmPassword}}
InputProps={{
endAdornment:
this.setState({ showPass2: !this.state.showPass2 })}
edge="end"
>
{this.state.showPass2 ? : }
,
}}
/>
);
}
}
export default ResetPassword;