1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-05 00:43:53 +00:00

Double name bug repair (#2879)

This commit is contained in:
杨顺强
2019-01-25 15:44:04 +08:00
committed by Daniel Pan
parent 9ddcf06225
commit 69b4db2afa
12 changed files with 222 additions and 91 deletions

View File

@@ -1,12 +1,14 @@
import React from 'react';
import PropTypes from 'prop-types';
import { gettext } from '../../utils/constants';
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter } from 'reactstrap';
import { Utils } from '../../utils/utils';
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Alert } from 'reactstrap';
const propTypes = {
currentNode: PropTypes.object,
onRename: PropTypes.func.isRequired,
toggleCancel: PropTypes.func.isRequired,
checkDuplicatedName: PropTypes.func.isRequired,
};
class Rename extends React.Component {
@@ -14,34 +16,13 @@ class Rename extends React.Component {
super(props);
this.state = {
newName: '',
errMessage: '',
};
this.newInput = React.createRef();
}
handleChange = (e) => {
this.setState({
newName: e.target.value,
});
}
handleSubmit = () => {
this.props.onRename(this.state.newName);
}
handleKeyPress = (e) => {
if (e.key === 'Enter') {
this.handleSubmit();
}
}
toggle = () => {
this.props.toggleCancel();
}
componentWillMount() {
this.setState({
newName: this.props.currentNode.object.name
});
this.setState({newName: this.props.currentNode.object.name});
}
componentDidMount() {
@@ -60,12 +41,66 @@ class Rename extends React.Component {
componentWillReceiveProps(nextProps) {
this.changeState(nextProps.currentNode);
}
handleChange = (e) => {
this.setState({newName: e.target.value});
}
changeState(currentNode) {
handleSubmit = () => {
let { isValid, errMessage } = this.validateInput();
if (!isValid) {
this.setState({errMessage : errMessage});
} else {
let isDuplicated = this.checkDuplicatedName();
if (isDuplicated) {
let errMessage = gettext('The name \'{name}\' is already occupied, please choose another name.');
errMessage = errMessage.replace('{name}', Utils.HTMLescape(this.state.newName));
this.setState({errMessage: errMessage});
} else {
this.props.onRename(this.state.newName);
}
}
}
handleKeyPress = (e) => {
if (e.key === 'Enter') {
this.handleSubmit();
}
}
toggle = () => {
this.props.toggleCancel();
}
changeState = (currentNode) => {
let name = currentNode.object.name;
this.setState({newName: name});
}
validateInput = () => {
let newName = this.state.newName.trim();
let isValid = true;
let errMessage = '';
if (!newName) {
isValid = false;
errMessage = gettext('Name is required.');
return { isValid, errMessage };
}
if (newName.indexOf('/') > -1) {
isValid = false;
errMessage = gettext('Name should not include ' + '\'/\'' + '.');
return { isValid, errMessage };
}
return { isValid, errMessage };
}
checkDuplicatedName = () => {
let isDuplicated = this.props.checkDuplicatedName(this.state.newName);
return isDuplicated;
}
render() {
let type = this.props.currentNode.object.type;
return (
@@ -74,6 +109,7 @@ class Rename extends React.Component {
<ModalBody>
<p>{type === 'file' ? gettext('Enter the new file name:'): gettext('Enter the new folder name:')}</p>
<Input onKeyPress={this.handleKeyPress} innerRef={input => {this.newInput = input;}} placeholder="newName" value={this.state.newName} onChange={this.handleChange} />
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={this.handleSubmit}>{gettext('Submit')}</Button>