2018-08-22 08:39:42 +00:00
|
|
|
import React from 'react';
|
2018-10-16 10:19:51 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-01-25 07:44:04 +00:00
|
|
|
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Alert } from 'reactstrap';
|
2023-11-01 07:10:43 +00:00
|
|
|
import { gettext } from '../../utils/constants';
|
2019-01-25 07:44:04 +00:00
|
|
|
import { Utils } from '../../utils/utils';
|
2018-08-22 08:39:42 +00:00
|
|
|
|
2018-10-16 10:19:51 +00:00
|
|
|
const propTypes = {
|
|
|
|
fileType: PropTypes.string,
|
|
|
|
parentPath: PropTypes.string.isRequired,
|
|
|
|
onAddFile: PropTypes.func.isRequired,
|
2019-01-25 07:44:04 +00:00
|
|
|
checkDuplicatedName: PropTypes.func.isRequired,
|
2023-07-22 07:54:25 +00:00
|
|
|
toggleDialog: PropTypes.func.isRequired,
|
2018-10-16 10:19:51 +00:00
|
|
|
};
|
|
|
|
|
2018-10-16 06:27:21 +00:00
|
|
|
class CreateFile extends React.Component {
|
2018-08-22 08:39:42 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
parentPath: '',
|
2019-04-18 03:03:25 +00:00
|
|
|
childName: props.fileType || '',
|
2019-01-25 07:44:04 +00:00
|
|
|
errMessage: '',
|
2024-01-08 02:55:34 +00:00
|
|
|
isSubmitBtnActive: props.fileType.slice(0, -5) ? true : false,
|
2018-08-22 08:39:42 +00:00
|
|
|
};
|
2018-10-16 10:19:51 +00:00
|
|
|
this.newInput = React.createRef();
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 07:44:04 +00:00
|
|
|
componentDidMount() {
|
|
|
|
let parentPath = this.props.parentPath;
|
|
|
|
if (parentPath[parentPath.length - 1] === '/') { // mainPanel
|
|
|
|
this.setState({parentPath: parentPath});
|
|
|
|
} else {
|
|
|
|
this.setState({parentPath: parentPath + '/'}); // sidePanel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 08:39:42 +00:00
|
|
|
handleChange = (e) => {
|
2019-05-17 03:09:05 +00:00
|
|
|
if (!e.target.value.trim()) {
|
|
|
|
this.setState({isSubmitBtnActive: false});
|
|
|
|
} else {
|
|
|
|
this.setState({isSubmitBtnActive: true});
|
|
|
|
}
|
|
|
|
|
2018-08-22 08:39:42 +00:00
|
|
|
this.setState({
|
2020-11-02 05:56:35 +00:00
|
|
|
childName: e.target.value,
|
2018-10-16 10:19:51 +00:00
|
|
|
}) ;
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-08-22 08:39:42 +00:00
|
|
|
|
|
|
|
handleSubmit = () => {
|
2019-05-17 06:17:52 +00:00
|
|
|
if (!this.state.isSubmitBtnActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-25 07:44:04 +00:00
|
|
|
let isDuplicated = this.checkDuplicatedName();
|
2019-01-31 09:37:02 +00:00
|
|
|
let newName = this.state.childName;
|
2020-11-02 05:56:35 +00:00
|
|
|
|
2019-01-25 07:44:04 +00:00
|
|
|
if (isDuplicated) {
|
2019-01-29 03:32:54 +00:00
|
|
|
let errMessage = gettext('The name "{name}" is already taken. Please choose a different name.');
|
2019-01-25 07:44:04 +00:00
|
|
|
errMessage = errMessage.replace('{name}', Utils.HTMLescape(newName));
|
|
|
|
this.setState({errMessage: errMessage});
|
|
|
|
} else {
|
|
|
|
let path = this.state.parentPath + newName;
|
2024-03-25 09:22:01 +00:00
|
|
|
this.props.onAddFile(path);
|
2023-07-22 07:54:25 +00:00
|
|
|
this.props.toggleDialog();
|
2019-01-25 07:44:04 +00:00
|
|
|
}
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-08-22 08:39:42 +00:00
|
|
|
|
2023-11-14 12:25:25 +00:00
|
|
|
handleKeyDown = (e) => {
|
2018-08-27 09:12:27 +00:00
|
|
|
if (e.key === 'Enter') {
|
|
|
|
this.handleSubmit();
|
2019-01-25 07:44:04 +00:00
|
|
|
e.preventDefault();
|
2018-08-27 09:12:27 +00:00
|
|
|
}
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-08-27 09:12:27 +00:00
|
|
|
|
2019-01-25 07:44:04 +00:00
|
|
|
checkDuplicatedName = () => {
|
|
|
|
let isDuplicated = this.props.checkDuplicatedName(this.state.childName);
|
|
|
|
return isDuplicated;
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-08-22 08:39:42 +00:00
|
|
|
|
2022-12-24 02:41:34 +00:00
|
|
|
onAfterModelOpened = () => {
|
|
|
|
if (!this.newInput.current) return;
|
|
|
|
this.newInput.current.focus();
|
|
|
|
this.newInput.current.setSelectionRange(0,0);
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2022-12-24 02:41:34 +00:00
|
|
|
|
2018-08-22 08:39:42 +00:00
|
|
|
render() {
|
2023-07-22 07:54:25 +00:00
|
|
|
const { toggleDialog } = this.props;
|
2018-08-22 08:39:42 +00:00
|
|
|
return (
|
2023-07-22 07:54:25 +00:00
|
|
|
<Modal isOpen={true} toggle={toggleDialog} onOpened={this.onAfterModelOpened}>
|
|
|
|
<ModalHeader toggle={toggleDialog}>{gettext('New File')}</ModalHeader>
|
2018-08-22 08:39:42 +00:00
|
|
|
<ModalBody>
|
|
|
|
<Form>
|
2018-12-17 03:54:25 +00:00
|
|
|
<FormGroup>
|
|
|
|
<Label for="fileName">{gettext('Name')}</Label>
|
2020-11-02 05:56:35 +00:00
|
|
|
<Input
|
|
|
|
id="fileName"
|
2023-11-14 12:25:25 +00:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2022-12-24 02:41:34 +00:00
|
|
|
innerRef={this.newInput}
|
2020-11-02 05:56:35 +00:00
|
|
|
value={this.state.childName}
|
2018-12-17 03:54:25 +00:00
|
|
|
onChange={this.handleChange}
|
|
|
|
/>
|
2018-08-22 08:39:42 +00:00
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
2019-01-25 07:44:04 +00:00
|
|
|
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
|
2018-08-22 08:39:42 +00:00
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2023-07-22 07:54:25 +00:00
|
|
|
<Button color="secondary" onClick={toggleDialog}>{gettext('Cancel')}</Button>
|
2019-05-17 03:09:05 +00:00
|
|
|
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
|
2018-08-22 08:39:42 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
2018-10-16 10:19:51 +00:00
|
|
|
);
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:19:51 +00:00
|
|
|
CreateFile.propTypes = propTypes;
|
|
|
|
|
2018-10-16 06:27:21 +00:00
|
|
|
export default CreateFile;
|