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' ;
2019-05-13 00:50:05 +00:00
const propTypes = {
2023-01-18 01:59:53 +00:00
setPassword : PropTypes . func . isRequired ,
2019-05-13 00:50:05 +00:00
toggle : PropTypes . func . isRequired
} ;
2022-02-17 07:30:06 +00:00
const { webdavSecretMinLength , webdavSecretStrengthLevel } = window . app . pageOptions ;
2023-01-18 01:59:53 +00:00
class SetWebdavPassword extends Component {
2019-05-13 00:50:05 +00:00
constructor ( props ) {
super ( props ) ;
2020-11-02 05:56:35 +00:00
this . state = {
2023-01-18 01:59:53 +00:00
password : '' ,
2019-05-13 00:50:05 +00:00
isPasswordVisible : false ,
2022-02-17 07:30:06 +00:00
btnDisabled : false ,
errMsg : ''
2019-05-13 00:50:05 +00:00
} ;
}
submit = ( ) => {
2022-02-11 05:48:18 +00:00
if ( this . state . password . length === 0 ) {
2024-07-18 03:58:42 +00:00
this . setState ( { errMsg : gettext ( 'Please enter a password.' ) } ) ;
2022-02-11 05:48:18 +00:00
return false ;
}
if ( this . state . password . length < webdavSecretMinLength ) {
2024-07-18 03:58:42 +00:00
this . setState ( { errMsg : gettext ( 'The password is too short.' ) } ) ;
2022-02-11 05:48:18 +00:00
return false ;
}
if ( Utils . getStrengthLevel ( this . state . password ) < webdavSecretStrengthLevel ) {
2024-07-18 03:58:42 +00:00
this . setState ( { errMsg : gettext ( 'The password is too weak. It should include at least {passwordStrengthLevel} of the following: number, upper letter, lower letter and other symbols.' ) . replace ( '{passwordStrengthLevel}' , webdavSecretStrengthLevel ) } ) ;
2022-02-11 05:48:18 +00:00
return false ;
}
2019-05-13 00:50:05 +00:00
this . setState ( {
btnDisabled : true
} ) ;
2022-02-11 05:48:18 +00:00
2023-01-18 01:59:53 +00:00
this . props . setPassword ( this . state . password . trim ( ) ) ;
2023-09-13 00:40:50 +00:00
} ;
2019-05-13 00:50:05 +00:00
handleInputChange = ( e ) => {
2024-07-18 03:58:42 +00:00
this . setState ( { password : e . target . value } ) ;
2023-09-13 00:40:50 +00:00
} ;
2019-05-13 00:50:05 +00:00
togglePasswordVisible = ( ) => {
this . setState ( {
isPasswordVisible : ! this . state . isPasswordVisible
} ) ;
2023-09-13 00:40:50 +00:00
} ;
2019-05-13 00:50:05 +00:00
generatePassword = ( ) => {
2022-02-11 05:48:18 +00:00
let randomPassword = Utils . generatePassword ( webdavSecretMinLength ) ;
2019-05-13 00:50:05 +00:00
this . setState ( {
2023-02-15 13:37:24 +00:00
password : randomPassword
2019-05-13 00:50:05 +00:00
} ) ;
2023-09-13 00:40:50 +00:00
} ;
2019-05-13 00:50:05 +00:00
render ( ) {
const { toggle } = this . props ;
2022-02-17 07:30:06 +00:00
const passwordTip = gettext ( '(at least {passwordMinLength} characters and includes {passwordStrengthLevel} of the following: number, upper letter, lower letter and other symbols)' ) . replace ( '{passwordMinLength}' , webdavSecretMinLength ) . replace ( '{passwordStrengthLevel}' , webdavSecretStrengthLevel ) ;
2019-05-13 00:50:05 +00:00
return (
< Modal centered = { true } isOpen = { true } toggle = { toggle } >
2023-01-18 01:59:53 +00:00
< ModalHeader toggle = { toggle } > { gettext ( 'Set WebDAV Password' ) } < / M o d a l H e a d e r >
2019-05-13 00:50:05 +00:00
< ModalBody >
2022-02-17 07:30:06 +00:00
< InputGroup >
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" >
2024-06-28 00:39:44 +00:00
< Button onClick = { this . togglePasswordVisible } >
2024-07-18 03:58:42 +00:00
< i className = { ` sf3-font sf3-font-eye ${ this . state . isPasswordVisible ? '' : 'slash' } ` } > < / i >
2024-06-28 00:39:44 +00:00
< / B u t t o n >
< Button onClick = { this . generatePassword } >
< i className = "sf3-font sf3-font-magic" > < / i >
< / B u t t o n >
2019-05-13 00:50:05 +00:00
< / 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 >
2022-02-17 07:30:06 +00:00
< p className = "form-text text-muted m-0" > { passwordTip } < / p >
{ this . state . errMsg && < Alert color = "danger" className = "m-0 mt-2" > { gettext ( this . state . errMsg ) } < / A l e r t > }
2019-05-13 00:50:05 +00:00
< / M o d a l B o d y >
< ModalFooter >
< 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 >
) ;
}
}
2023-01-18 01:59:53 +00:00
SetWebdavPassword . propTypes = propTypes ;
2019-05-13 00:50:05 +00:00
2023-01-18 01:59:53 +00:00
export default SetWebdavPassword ;