2019-10-21 01:45:00 +00:00
import React from 'react' ;
import PropTypes from 'prop-types' ;
import { Alert , Modal , ModalHeader , ModalBody , ModalFooter , Button , Form , FormGroup , Label , Input , InputGroup , InputGroupAddon } from 'reactstrap' ;
import { gettext } from '../../../utils/constants' ;
import { Utils } from '../../../utils/utils' ;
import SysAdminUserRoleEditor from '../../../components/select-editor/sysadmin-user-role-editor' ;
const propTypes = {
toggleDialog : PropTypes . func . isRequired ,
addUser : PropTypes . func . isRequired
} ;
class SysAdminAddUserDialog extends React . Component {
constructor ( props ) {
super ( props ) ;
this . state = {
errorMsg : '' ,
isPasswordVisible : false ,
password : '' ,
passwordAgain : '' ,
email : '' ,
name : '' ,
2019-10-28 02:29:20 +00:00
role : 'default' ,
2019-10-21 01:45:00 +00:00
isSubmitBtnActive : false
} ;
}
checkSubmitBtnActive = ( ) => {
const { email , password , passwordAgain } = this . state ;
let btnActive = true ;
2019-11-14 22:58:19 +00:00
if ( email . trim ( ) &&
password . trim ( ) &&
passwordAgain . trim ( ) ) {
2019-10-21 01:45:00 +00:00
btnActive = true ;
} else {
btnActive = false ;
}
this . setState ( {
isSubmitBtnActive : btnActive
} ) ;
}
toggle = ( ) => {
this . props . toggleDialog ( ) ;
}
togglePasswordVisible = ( ) => {
this . setState ( { isPasswordVisible : ! this . state . isPasswordVisible } ) ;
}
inputPassword = ( e ) => {
2019-11-14 22:58:19 +00:00
let passwd = e . target . value ;
2019-10-21 01:45:00 +00:00
this . setState ( {
password : passwd ,
errorMsg : ''
} , this . checkSubmitBtnActive ) ;
}
inputPasswordAgain = ( e ) => {
2019-11-14 22:58:19 +00:00
let passwd = e . target . value ;
2019-10-21 01:45:00 +00:00
this . setState ( {
passwordAgain : passwd ,
errorMsg : ''
} , this . checkSubmitBtnActive ) ;
}
generatePassword = ( ) => {
let val = Utils . generatePassword ( 8 ) ;
this . setState ( {
password : val ,
passwordAgain : val
} , this . checkSubmitBtnActive ) ;
}
inputEmail = ( e ) => {
2019-11-14 22:58:19 +00:00
let email = e . target . value ;
2019-10-21 01:45:00 +00:00
this . setState ( {
email : email
} , this . checkSubmitBtnActive ) ;
}
inputName = ( e ) => {
2019-11-14 22:58:19 +00:00
let name = e . target . value ;
2019-10-21 01:45:00 +00:00
this . setState ( {
name : name
} ) ;
}
2019-10-28 02:29:20 +00:00
updateRole = ( role ) => {
this . setState ( {
role : role
} ) ;
}
2019-10-21 01:45:00 +00:00
handleSubmit = ( ) => {
2019-10-28 02:29:20 +00:00
const { email , password , passwordAgain , name , role } = this . state ;
2019-10-21 01:45:00 +00:00
if ( password != passwordAgain ) {
this . setState ( { errorMsg : gettext ( 'Passwords do not match.' ) } ) ;
return ;
}
2019-10-28 02:29:20 +00:00
let data = {
2019-11-14 22:58:19 +00:00
email : email . trim ( ) ,
name : name . trim ( ) ,
2019-11-27 05:43:17 +00:00
password : password . trim ( )
2019-10-21 01:45:00 +00:00
} ;
2019-10-28 02:29:20 +00:00
if ( this . props . showRole ) {
data . role = role ;
}
2019-10-21 01:45:00 +00:00
this . props . addUser ( data ) ;
this . toggle ( ) ;
}
render ( ) {
2019-10-28 02:29:20 +00:00
const { dialogTitle , showRole } = this . props ;
const {
errorMsg , isPasswordVisible ,
email , name , role , password , passwordAgain ,
2019-10-21 01:45:00 +00:00
isSubmitBtnActive
} = this . state ;
return (
< Modal isOpen = { true } toggle = { this . toggle } >
2019-10-28 02:29:20 +00:00
< ModalHeader toggle = { this . toggle } > { dialogTitle || gettext ( 'Add Member' ) } < / M o d a l H e a d e r >
2019-10-21 01:45:00 +00:00
< ModalBody >
< Form autoComplete = "off" >
< FormGroup >
< Label > { gettext ( 'Email' ) } < / L a b e l >
< Input value = { email } onChange = { this . inputEmail } / >
< / F o r m G r o u p >
< FormGroup >
< Label > { gettext ( 'Name(optional)' ) } < / L a b e l >
2019-10-28 02:29:20 +00:00
< Input type = "text" value = { name } onChange = { this . inputName } / >
< / F o r m G r o u p >
2019-11-14 22:58:19 +00:00
{ showRole &&
2019-10-28 02:29:20 +00:00
< FormGroup >
< Label >
2019-11-14 22:58:19 +00:00
{ gettext ( 'Role' ) }
< span className = "small text-secondary ml-1 fas fa-question-circle" title = { gettext ( 'You can also add a user as a guest, who will not be allowed to create libraries and groups.' ) } > < / s p a n >
< / L a b e l >
2019-10-28 02:29:20 +00:00
< SysAdminUserRoleEditor
isTextMode = { false }
isEditIconShow = { false }
currentRole = { role }
roleOptions = { this . props . availableRoles }
onRoleChanged = { this . updateRole }
/ >
2019-10-21 01:45:00 +00:00
< / F o r m G r o u p >
2019-11-14 22:58:19 +00:00
}
2019-10-21 01:45:00 +00:00
< FormGroup >
< Label > { gettext ( 'Password' ) } < / L a b e l >
< InputGroup >
< Input autoComplete = "new-password" type = { isPasswordVisible ? 'text' : 'password' } value = { password || '' } onChange = { this . inputPassword } / >
< InputGroupAddon addonType = "append" >
< Button className = "mt-0" onClick = { this . togglePasswordVisible } > < i className = { ` link-operation-icon fas ${ this . state . isPasswordVisible ? 'fa-eye' : 'fa-eye-slash' } ` } > < / i > < / B u t t o n >
< Button className = "mt-0" onClick = { this . generatePassword } > < i className = "link-operation-icon fas fa-magic" > < / i > < / B u t t o n >
< / I n p u t G r o u p A d d o n >
< / I n p u t G r o u p >
< / F o r m G r o u p >
< FormGroup >
< Label > { gettext ( 'Password again' ) } < / L a b e l >
< Input type = { isPasswordVisible ? 'text' : 'password' } value = { passwordAgain || '' } onChange = { this . inputPasswordAgain } / >
< / F o r m G r o u p >
< / F o r m >
{ errorMsg && < Alert color = "danger" > { errorMsg } < / A l e r t > }
< / M o d a l B o d y >
< ModalFooter >
< Button color = "secondary" onClick = { this . toggle } > { gettext ( 'Cancel' ) } < / B u t t o n >
< Button color = "primary" onClick = { this . handleSubmit } disabled = { ! isSubmitBtnActive } > { gettext ( 'Submit' ) } < / B u t t o n >
< / M o d a l F o o t e r >
< / M o d a l >
) ;
}
}
SysAdminAddUserDialog . propTypes = propTypes ;
export default SysAdminAddUserDialog ;