import React from 'react';
import PropTypes from 'prop-types';
import { Modal, ModalBody, ModalFooter, Alert } from 'reactstrap';
import { gettext, repoPasswordMinLength } from '../../utils/constants';
import { Utils } from '../../utils/utils';
import { seafileAPI } from '../../utils/seafile-api';
import toaster from '../toast';
import SeahubModalHeader from '@/components/common/seahub-modal-header';
const propTypes = {
repoID: PropTypes.string.isRequired,
repoName: PropTypes.string.isRequired,
toggleDialog: PropTypes.func.isRequired
};
class ChangeRepoPasswordDialog extends React.Component {
constructor(props) {
super(props);
this.state = {
oldPassword: '',
newPassword: '',
newPasswordAgain: '',
submitBtnDisabled: false,
errorMsg: ''
};
}
handleOldPasswordInputChange = (e) => {
this.setState({
oldPassword: e.target.value
});
};
handleNewPasswordInputChange = (e) => {
this.setState({
newPassword: e.target.value
});
};
handleNewPasswordAgainInputChange = (e) => {
this.setState({
newPasswordAgain: e.target.value
});
};
formSubmit = (e) => {
const { oldPassword, newPassword, newPasswordAgain } = this.state;
if (!oldPassword) {
this.setState({
errorMsg: gettext('Please enter the old password')
});
return false;
}
if (!newPassword) {
this.setState({
errorMsg: gettext('Please enter a new password')
});
return false;
}
if (newPassword.length < repoPasswordMinLength) {
this.setState({
errorMsg: gettext('New password is too short')
});
return false;
}
if (!newPasswordAgain) {
this.setState({
errorMsg: gettext('Please enter the new password again')
});
return false;
}
if (newPassword != newPasswordAgain) {
this.setState({
errorMsg: gettext('New passwords don\'t match')
});
return false;
}
this.setState({
submitBtnDisabled: true
});
seafileAPI.changeEncryptedRepoPassword(this.props.repoID, oldPassword, newPassword)
.then(() => {
this.props.toggleDialog();
toaster.success(gettext('Successfully changed library password.'));
}).catch((error) => {
let errorMsg = Utils.getErrorMsg(error);
this.setState({
errorMsg: errorMsg,
submitBtnDisabled: false
});
});
};
render() {
const { repoName, toggleDialog } = this.props;
let title = gettext('Change Password of Library {placeholder}');
title = title.replace('{placeholder}', '' + Utils.HTMLescape(repoName) + '');
return (
);
}
}
ChangeRepoPasswordDialog.propTypes = propTypes;
export default ChangeRepoPasswordDialog;