import React from 'react'; import PropTypes from 'prop-types'; import { Button, Modal, Input, ModalHeader, ModalBody, ModalFooter, Label, Form, InputGroup, InputGroupAddon, FormGroup } from 'reactstrap'; import { gettext } from '../../utils/constants'; const propTypes = { toggle: PropTypes.func.isRequired, handleSubmit: PropTypes.func.isRequired, }; class AddOrgUserDialog extends React.Component { constructor(props) { super(props); this.state = { isPasswordVisible: true, email: '', name: '', password: '', passwdnew: '', errMessage: '', isAddingUser: false, }; this.passwdInput = React.createRef(); this.passwdNewInput = React.createRef(); } handleSubmit = () => { let isValid = this.validateInputParams(); if (isValid) { let { email, name, password } = this.state; this.setState({isAddingUser: true}); this.props.handleSubmit(email, name, password); } } handleKeyPress = (e) => { e.preventDefault(); if (e.key == 'Enter') { this.handleSubmit(e); } }; togglePasswordVisible = () => { this.setState({isPasswordVisible: !this.state.isPasswordVisible}, () => { if (this.state.isPasswordVisible) { this.passwdInput.type = 'password'; this.passwdNewInput.type = 'password'; } else { this.passwdInput.type = 'text'; this.passwdNewInput.type = 'text'; } }); } generatePassword = () => { let val = Math.random().toString(36).substr(5); this.setState({ password: val, passwdnew: val, isPasswordVisible: false }, () => { this.passwdInput.type = 'text'; this.passwdNewInput.type = 'text'; }); } inputEmail = (e) => { let email = e.target.value.trim(); this.setState({email: email}); } inputName = (e) => { let name = e.target.value.trim(); this.setState({name: name}); } inputPassword = (e) => { let passwd = e.target.value.trim(); this.setState({password: passwd}, () => { if (this.state.isPasswordVisible) { this.passwdInput.type = 'password'; this.passwdNewInput.type = 'password'; } }); } inputPasswordNew = (e) => { let passwd = e.target.value.trim(); this.setState({passwdnew: passwd}, () => { if (this.state.isPasswordVisible) { this.passwdInput.type = 'password'; this.passwdNewInput.type = 'password'; } }); } toggle = () => { this.props.toggle(); } validateInputParams() { let errMessage = ''; let email = this.state.email; if (!email.length) { errMessage = 'email is required'; this.setState({errMessage: errMessage}); return false; } let password1 = this.state.password; let password2 = this.state.passwdnew; if (!password1.length) { errMessage = 'Please enter password'; this.setState({errMessage: errMessage}); return false; } if (!password2.length) { errMessage = 'Please enter the password again'; this.setState({errMessage: errMessage}); return false; } if (password1 !== password2) { errMessage = 'Passwords don\'t match'; this.setState({errMessage: errMessage}); return false; } return true; } render() { return ( {gettext('Add User')}
{this.passwdInput = input;}} value={this.state.password || ''} onChange={this.inputPassword} /> {this.passwdNewInput = input;}} className="passwd" value={this.state.passwdnew || ''} onChange={this.inputPasswordNew} />
); } } AddOrgUserDialog.propTypes = propTypes; export default AddOrgUserDialog;