2018-12-03 08:21:09 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-02-12 03:16:49 +00:00
|
|
|
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Alert } from 'reactstrap';
|
2018-12-17 09:26:27 +00:00
|
|
|
import { gettext, enableEncryptedLibrary } from '../../utils/constants';
|
2018-12-03 08:21:09 +00:00
|
|
|
|
|
|
|
const propTypes = {
|
2018-12-17 02:23:05 +00:00
|
|
|
libraryType: PropTypes.string.isRequired,
|
2018-12-07 02:36:59 +00:00
|
|
|
onCreateRepo: PropTypes.func.isRequired,
|
|
|
|
onCreateToggle: PropTypes.func.isRequired,
|
2018-12-03 08:21:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CreateRepoDialog extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
repoName: '',
|
2018-12-07 02:36:59 +00:00
|
|
|
disabled: true,
|
|
|
|
encrypt: false,
|
|
|
|
password1: '',
|
2018-12-03 08:21:09 +00:00
|
|
|
password2: '',
|
2018-12-07 02:36:59 +00:00
|
|
|
errMessage: '',
|
2018-12-16 07:11:30 +00:00
|
|
|
permission: 'rw'
|
2018-12-03 08:21:09 +00:00
|
|
|
};
|
|
|
|
this.newInput = React.createRef();
|
|
|
|
}
|
|
|
|
|
2018-12-07 02:36:59 +00:00
|
|
|
handleRepoNameChange = (e) => {
|
|
|
|
this.setState({repoName: e.target.value});
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePassword1Change = (e) => {
|
|
|
|
this.setState({password1: e.target.value});
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePassword2Change = (e) => {
|
|
|
|
this.setState({password2: e.target.value});
|
2018-12-03 08:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
2018-12-07 05:21:43 +00:00
|
|
|
let isValid= this.validateInputParams();
|
|
|
|
if (isValid) {
|
2018-12-07 02:36:59 +00:00
|
|
|
let repoName = this.state.repoName.trim();
|
|
|
|
let password = this.state.encrypt ? this.state.password1 : '';
|
2018-12-16 07:11:30 +00:00
|
|
|
let permission= this.state.permission;
|
|
|
|
let repo = this.createRepo(repoName, password, permission);
|
2018-12-07 02:36:59 +00:00
|
|
|
this.props.onCreateRepo(repo);
|
|
|
|
}
|
2018-12-03 08:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyPress = (e) => {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
this.handleSubmit();
|
2018-12-21 07:40:59 +00:00
|
|
|
e.preventDefault();
|
2018-12-03 08:21:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle = () => {
|
2018-12-07 02:36:59 +00:00
|
|
|
this.props.onCreateToggle();
|
2018-12-03 08:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.newInput.focus();
|
|
|
|
}
|
|
|
|
|
2018-12-07 02:36:59 +00:00
|
|
|
validateInputParams() {
|
|
|
|
let errMessage = '';
|
|
|
|
let repoName = this.state.repoName.trim();
|
|
|
|
if (!repoName.length) {
|
2019-01-28 08:32:32 +00:00
|
|
|
errMessage = gettext('Name is required');
|
2018-12-07 02:36:59 +00:00
|
|
|
this.setState({errMessage: errMessage});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (repoName.indexOf('/') > -1) {
|
2019-01-28 08:32:32 +00:00
|
|
|
errMessage = gettext('Name should not include \'/\'.');
|
2018-12-07 02:36:59 +00:00
|
|
|
this.setState({errMessage: errMessage});
|
2018-12-07 05:21:43 +00:00
|
|
|
return false;
|
2018-12-07 02:36:59 +00:00
|
|
|
}
|
|
|
|
if (this.state.encrypt) {
|
|
|
|
let password1 = this.state.password1.trim();
|
|
|
|
let password2 = this.state.password2.trim();
|
|
|
|
if (!password1.length) {
|
2019-01-28 08:32:32 +00:00
|
|
|
errMessage = gettext('Please enter password');
|
2018-12-07 02:36:59 +00:00
|
|
|
this.setState({errMessage: errMessage});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!password2.length) {
|
2019-01-28 08:32:32 +00:00
|
|
|
errMessage = gettext('Please enter the password again');
|
2018-12-07 02:36:59 +00:00
|
|
|
this.setState({errMessage: errMessage});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (password1.length < 8) {
|
2019-01-28 08:32:32 +00:00
|
|
|
errMessage = gettext('Password is too short');
|
2018-12-07 02:36:59 +00:00
|
|
|
this.setState({errMessage: errMessage});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (password1 !== password2) {
|
2019-01-28 08:32:32 +00:00
|
|
|
errMessage = gettext('Passwords don\'t match');
|
2018-12-07 02:36:59 +00:00
|
|
|
this.setState({errMessage: errMessage});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-16 07:11:30 +00:00
|
|
|
onPermissionChange = (e) => {
|
|
|
|
let permission = e.target.value;
|
|
|
|
this.setState({permission: permission});
|
|
|
|
}
|
|
|
|
|
2018-12-07 02:36:59 +00:00
|
|
|
onEncrypted = (e) => {
|
|
|
|
let isChecked = e.target.checked;
|
|
|
|
this.setState({
|
|
|
|
encrypt: isChecked,
|
|
|
|
disabled: !isChecked
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-16 07:11:30 +00:00
|
|
|
createRepo = (repoName, password, permission) => {
|
2018-12-07 05:21:43 +00:00
|
|
|
let libraryType = this.props.libraryType;
|
2018-12-07 02:36:59 +00:00
|
|
|
let encrypt = password ? true : false;
|
2018-12-07 05:21:43 +00:00
|
|
|
let repo = null;
|
|
|
|
if (libraryType === 'mine' || libraryType === 'public') {
|
|
|
|
repo = {
|
|
|
|
id: null,
|
|
|
|
name: repoName,
|
|
|
|
desc: '',
|
|
|
|
encrypted: encrypt,
|
|
|
|
passwd: password,
|
|
|
|
passwd1: password,
|
|
|
|
passwd2: password,
|
|
|
|
mtime: 0,
|
|
|
|
mtime_relative: '',
|
|
|
|
owner: '-',
|
|
|
|
owner_nickname: '-',
|
|
|
|
permission: 'rw',
|
|
|
|
storage_name: '-',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (libraryType === 'group') {
|
|
|
|
repo = {
|
|
|
|
repo_name: repoName,
|
|
|
|
password: password,
|
2018-12-16 07:11:30 +00:00
|
|
|
permission: permission,
|
2018-12-07 05:21:43 +00:00
|
|
|
};
|
|
|
|
}
|
2018-12-07 02:36:59 +00:00
|
|
|
return repo;
|
|
|
|
}
|
|
|
|
|
2018-12-03 08:21:09 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-12-16 08:46:56 +00:00
|
|
|
<Modal isOpen={true} toggle={this.toggle}>
|
2018-12-07 02:36:59 +00:00
|
|
|
<ModalHeader toggle={this.toggle}>{gettext('New Library')}</ModalHeader>
|
2018-12-03 08:21:09 +00:00
|
|
|
<ModalBody>
|
|
|
|
<Form>
|
|
|
|
<FormGroup>
|
2018-12-17 03:11:45 +00:00
|
|
|
<Label for="repoName">{gettext('Name')}</Label>
|
2018-12-03 08:21:09 +00:00
|
|
|
<Input
|
2018-12-17 03:11:45 +00:00
|
|
|
id="repoName"
|
2018-12-03 08:21:09 +00:00
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
innerRef={input => {this.newInput = input;}}
|
2018-12-07 02:36:59 +00:00
|
|
|
value={this.state.repoName}
|
|
|
|
onChange={this.handleRepoNameChange}
|
2018-12-03 08:21:09 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
2018-12-16 07:11:30 +00:00
|
|
|
{this.props.libraryType === 'group' && (
|
|
|
|
<FormGroup>
|
|
|
|
<Label for="exampleSelect">{gettext('Permission')}</Label>
|
|
|
|
<Input type="select" name="select" id="exampleSelect" onChange={this.onPermissionChange} value={this.state.permission}>
|
2018-12-17 03:11:45 +00:00
|
|
|
<option value='rw'>{gettext('Read-Write')}</option>
|
|
|
|
<option value='r'>{gettext('Read-Only')}</option>
|
2018-12-16 07:11:30 +00:00
|
|
|
</Input>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
2018-12-17 09:26:27 +00:00
|
|
|
{enableEncryptedLibrary &&
|
|
|
|
<div>
|
|
|
|
<FormGroup check>
|
|
|
|
<Input type="checkbox" id="encrypt" onChange={this.onEncrypted}/>
|
|
|
|
<Label for="encrypt">{gettext('Encrypt')}</Label>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup>
|
2018-12-20 07:55:07 +00:00
|
|
|
{/* todo translate */}
|
2019-01-28 08:35:45 +00:00
|
|
|
<Label for="passwd1" className="font-weight-bold">{gettext('Password')}{' '}</Label><span className="tip">{gettext('(at least 8 characters)')}</span>
|
2018-12-17 09:26:27 +00:00
|
|
|
<Input
|
|
|
|
id="passwd1"
|
|
|
|
type="password"
|
|
|
|
disabled={this.state.disabled}
|
|
|
|
value={this.state.password1}
|
|
|
|
onChange={this.handlePassword1Change}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup>
|
2019-01-28 08:35:45 +00:00
|
|
|
<Label for="passwd2" className="font-weight-bold">{gettext('Password again')}</Label>
|
2018-12-17 09:26:27 +00:00
|
|
|
<Input
|
|
|
|
id="passwd2"
|
|
|
|
type="password"
|
|
|
|
disabled={this.state.disabled}
|
|
|
|
value={this.state.password2}
|
|
|
|
onChange={this.handlePassword2Change}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</div>
|
|
|
|
}
|
2018-12-03 08:21:09 +00:00
|
|
|
</Form>
|
2019-02-12 03:16:49 +00:00
|
|
|
{this.state.errMessage && <Alert color="danger">{this.state.errMessage}</Alert>}
|
2018-12-03 08:21:09 +00:00
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2019-04-08 02:28:44 +00:00
|
|
|
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
|
2018-12-03 08:21:09 +00:00
|
|
|
<Button color="primary" onClick={this.handleSubmit}>{gettext('Submit')}</Button>
|
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateRepoDialog.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default CreateRepoDialog;
|