import React, {Component} from 'react'; import { Form, Modal, Input, Button, message, } from 'antd'; import CustomAction from '../api/CustomAction'; class UpdatePasswordModal extends React.Component { state = { loading: false, passwordHasError: false, passwordState: { hasFeedback: false, validateStatus:'', help:'Password must include at least one number, one lowercase letter, one uppercase letter and a symbol' }, confirmationHasError: false, confirmationState: { hasFeedback: false, validateStatus:'', help:'' }, }; constructor(props) { super(props); this.formRef = React.createRef(); this.customAction = new CustomAction(this.props.adapter); } componentDidMount() { message.config({ top: 40, }); } clearConfirmFeedback = () => { this.setState({confirmationHasError: false}); this.setState({ confirmationState: { hasFeedback : false, validateStatus:'', help:'', } }); } updatePasswordState(value) { const passwordValidationResult = this.validatePassword(value); if (passwordValidationResult !== null) { this.setState({passwordHasError: true}); this.setState({ passwordState: { hasFeedback : true, validateStatus:'error', help:passwordValidationResult, } }); return false; } else { this.setState({passwordHasError: false}); this.setState({ passwordState: { hasFeedback : true, validateStatus:'success', help:'', } }); } return true; } updateConfirmPasswordState(values) { if (values.confirm !== values.new) { this.setState({confirmationHasError: true}); this.setState({ confirmationState: { hasFeedback : true, validateStatus:'error', help:'Passwords don\'t match', } }); return false; } else { this.setState({confirmationHasError: false}); this.setState({ confirmationState: { hasFeedback : false, validateStatus:'', help:'', } }); } return true; } handleOk = () => { const from = this.formRef.current; from .validateFields() .then((values) => { if (this.updatePasswordState(values.new) && this.updateConfirmPasswordState(values)) { this.updatePassword(values.current, values.new) .then((response) => { const data = response.data; console.log(data); if (data.status === 'SUCCESS') { this.handleCancel(); message.success(this.props.adapter.gt('Password updated')); } else { message.error( `${this.props.adapter.gt('Error updating password')}: ${this.props.adapter.gt(data.data)}` ); } }).catch((error) => { message.error( `${this.props.adapter.gt('Error updating password')}` ); console.log(error.message); }); } }) .catch((info) => { this.setState({ loading: false }); }); } handleCancel = () => { if (this.formRef.current) { this.formRef.current.resetFields(); } this.props.closeModal(); } updatePassword = (oldPassword, newPassword) => { const req = { current: oldPassword ? oldPassword : '', pwd: newPassword }; const reqJson = JSON.stringify(req); const callBackData = []; callBackData.callBackData = []; callBackData.callBackSuccess = 'changePasswordSuccessCallBack'; callBackData.callBackFail = 'changePasswordFailCallBack'; return this.customAction.execute('changePassword', 'modules=employees', reqJson); } validatePassword = (password) => { if (password.length < 8) { return this.props.adapter.gt('Password too short'); } if (password.length > 20) { return this.props.adapter.gt('Password too long'); } const numberTester = /.*[0-9]+.*$/; if (!password.match(numberTester)) { return this.props.adapter.gt('Password must include at least one number'); } const lowerTester = /.*[a-z]+.*$/; if (!password.match(lowerTester)) { return this.props.adapter.gt('Password must include at least one lowercase letter'); } const upperTester = /.*[A-Z]+.*$/; if (!password.match(upperTester)) { return this.props.adapter.gt('Password must include at least one uppercase letter'); } const symbolTester = /.*[\W]+.*$/; if (!password.match(symbolTester)) { return this.props.adapter.gt('Password must include at least one symbol'); } return null; } render() { const layout = { labelCol: { span: 8 }, wrapperCol: { span: 16 }, }; return ( {this.props.adapter.gt('Cancel')} , , ]} >
{ this.state.passwordHasError && this.updatePasswordState(event.target.value)}/> } { !this.state.passwordHasError && this.updatePasswordState(event.target.value)}/> } { this.state.confirmationHasError && this.clearConfirmFeedback()}/> } { !this.state.confirmationHasError && this.clearConfirmFeedback()}/> }
) } } export default UpdatePasswordModal;