1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-28 19:51:34 +00:00
seahub/frontend/src/components/dialog/create-department-repo-dialog.js

105 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-12-10 09:59:26 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Alert } from 'reactstrap';
2018-12-10 09:59:26 +00:00
import { gettext, maxFileName } from '../../utils/constants';
const propTypes = {
onCreateRepo: PropTypes.func.isRequired,
onCreateToggle: PropTypes.func.isRequired
};
class CreateDepartmentRepoDialog extends React.Component {
constructor(props) {
super(props);
this.state = {
repoName: '',
errMessage: '',
isSubmitBtnActive: false,
2018-12-10 09:59:26 +00:00
};
}
handleChange = (e) => {
if (!e.target.value.trim()) {
this.setState({isSubmitBtnActive: false});
} else {
this.setState({isSubmitBtnActive: true});
}
2018-12-10 09:59:26 +00:00
this.setState({
repoName: e.target.value,
2018-12-10 09:59:26 +00:00
});
}
handleSubmit = () => {
let isValid = this.validateRepoName();
if (isValid) {
let repo = this.createRepo(this.state.repoName);
2018-12-16 03:17:17 +00:00
this.props.onCreateRepo(repo, 'department');
2018-12-10 09:59:26 +00:00
}
}
2018-12-10 09:59:26 +00:00
handleKeyPress = (e) => {
if (e.key === 'Enter') {
this.handleSubmit();
e.preventDefault();
2018-12-10 09:59:26 +00:00
}
}
toggle = () => {
this.props.onCreateToggle();
}
validateRepoName = () => {
let errMessage = '';
let repoName = this.state.repoName.trim();
if (!repoName.length) {
errMessage = gettext('Name is required');
2018-12-10 09:59:26 +00:00
this.setState({errMessage: errMessage});
return false;
}
if (repoName.indexOf('/') > -1) {
errMessage = gettext('Name should not include \'/\'.');
2018-12-10 09:59:26 +00:00
this.setState({errMessage: errMessage});
return false;
}
2018-12-10 09:59:26 +00:00
return true;
}
createRepo = (repoName) => {
let repo = { repo_name: repoName };
return repo;
}
render() {
return (
<Modal isOpen={true} toggle={this.toggle} autoFocus={false}>
2018-12-10 09:59:26 +00:00
<ModalHeader toggle={this.toggle}>{gettext('New Department Library')}</ModalHeader>
<ModalBody>
<Form>
<FormGroup>
<Label for="repo-name">{gettext('Name')}</Label>
<Input
id="repo-name"
onKeyPress={this.handleKeyPress}
value={this.state.repoName}
2018-12-10 09:59:26 +00:00
onChange={this.handleChange}
maxLength={maxFileName}
autoFocus={true}
2018-12-10 09:59:26 +00:00
/>
</FormGroup>
</Form>
{this.state.errMessage && <Alert color="danger">{this.state.errMessage}</Alert>}
2018-12-10 09:59:26 +00:00
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
2018-12-10 09:59:26 +00:00
</ModalFooter>
</Modal>
);
}
}
CreateDepartmentRepoDialog.propTypes = propTypes;
export default CreateDepartmentRepoDialog;