2019-05-13 00:50:05 +00:00
import React , { Component } from 'react' ;
import PropTypes from 'prop-types' ;
2022-02-11 05:48:18 +00:00
import { Modal , ModalHeader , ModalBody , ModalFooter , Alert , Button , Input , InputGroup , InputGroupAddon } from 'reactstrap' ;
2019-05-13 00:50:05 +00:00
import { gettext } from '../../utils/constants' ;
2022-02-11 05:48:18 +00:00
import { Utils } from '../../utils/utils' ;
const { webdavSecretMinLength , webdavSecretStrengthLevel } = window . app . pageOptions ;
2019-05-13 00:50:05 +00:00
const propTypes = {
password : PropTypes . string . isRequired ,
updatePassword : PropTypes . func . isRequired ,
toggle : PropTypes . func . isRequired
} ;
class UpdateWebdavPassword extends Component {
constructor ( props ) {
super ( props ) ;
2020-11-02 05:56:35 +00:00
this . state = {
2022-02-11 05:48:18 +00:00
errorInfo : '' ,
2019-05-13 00:50:05 +00:00
password : this . props . password ,
isPasswordVisible : false ,
btnDisabled : false
} ;
}
submit = ( ) => {
2022-02-11 05:48:18 +00:00
if ( this . state . password . length === 0 ) {
this . setState ( { errorInfo : 'Please enter password' } ) ;
return false ;
}
if ( this . state . password . length < webdavSecretMinLength ) {
this . setState ( { errorInfo : 'Password is too short' } ) ;
return false ;
}
if ( Utils . getStrengthLevel ( this . state . password ) < webdavSecretStrengthLevel ) {
this . setState ( { errorInfo : gettext ( 'Password is too weak, should have at least {webdavSecretStrengthLevel} of the following: num, upper letter, lower letter and other symbols' . replace ( '{webdavSecretStrengthLevel}' , webdavSecretStrengthLevel ) ) } ) ;
return false ;
}
2019-05-13 00:50:05 +00:00
this . setState ( {
btnDisabled : true
} ) ;
2022-02-11 05:48:18 +00:00
2019-05-13 00:50:05 +00:00
this . props . updatePassword ( this . state . password ) ;
}
handleInputChange = ( e ) => {
let passwd = e . target . value . trim ( ) ;
this . setState ( { password : passwd } ) ;
}
togglePasswordVisible = ( ) => {
this . setState ( {
isPasswordVisible : ! this . state . isPasswordVisible
} ) ;
}
generatePassword = ( ) => {
2022-02-11 05:48:18 +00:00
let randomPassword = Utils . generatePassword ( webdavSecretMinLength ) ;
2019-05-13 00:50:05 +00:00
this . setState ( {
password : randomPassword ,
isPasswordVisible : true
} ) ;
}
render ( ) {
const { toggle } = this . props ;
2022-02-11 05:48:18 +00:00
let passwordLengthTip = gettext ( '(at least {passwordLength} characters and has {shareLinkPasswordStrengthLevel} of the following: num, upper letter, lower letter and other symbols)' ) ;
passwordLengthTip = passwordLengthTip . replace ( '{passwordLength}' , webdavSecretMinLength )
. replace ( '{shareLinkPasswordStrengthLevel}' , webdavSecretStrengthLevel ) ;
2019-05-13 00:50:05 +00:00
return (
< Modal centered = { true } isOpen = { true } toggle = { toggle } >
< ModalHeader toggle = { toggle } > { gettext ( 'WebDav Password' ) } < / M o d a l H e a d e r >
2022-02-11 05:48:18 +00:00
< span className = "tip" > { passwordLengthTip } < / s p a n >
2019-05-13 00:50:05 +00:00
< ModalBody >
< InputGroup className = "" >
2021-06-11 06:34:54 +00:00
< Input type = { this . state . isPasswordVisible ? 'text' : 'password' } value = { this . state . password } onChange = { this . handleInputChange } autoComplete = "new-password" / >
2019-05-13 00:50:05 +00:00
< InputGroupAddon addonType = "append" >
< Button onClick = { this . togglePasswordVisible } > < i className = { ` fas ${ this . state . isPasswordVisible ? 'fa-eye' : 'fa-eye-slash' } ` } > < / i > < / B u t t o n >
< Button onClick = { this . generatePassword } > < i className = "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 >
< / M o d a l B o d y >
< ModalFooter >
2022-02-11 05:48:18 +00:00
{ this . state . errorInfo && < Alert color = "danger" className = "mt-2" > { gettext ( this . state . errorInfo ) } < / A l e r t > }
2019-05-13 00:50:05 +00:00
< Button color = "secondary" onClick = { toggle } > { gettext ( 'Cancel' ) } < / B u t t o n >
< Button color = "primary" onClick = { this . submit } disabled = { this . state . btnDisabled } > { gettext ( 'Submit' ) } < / B u t t o n >
< / M o d a l F o o t e r >
< / M o d a l >
) ;
}
}
UpdateWebdavPassword . propTypes = propTypes ;
export default UpdateWebdavPassword ;