import React from 'react'; import PropTypes from 'prop-types'; import { Button, Modal, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Alert } from 'reactstrap'; import { gettext } from '../../utils/constants'; import { Utils, validateName } from '../../utils/utils'; import SeahubModalHeader from '@/components/common/seahub-modal-header'; const propTypes = { fileType: PropTypes.string, parentPath: PropTypes.string.isRequired, onAddFolder: PropTypes.func.isRequired, checkDuplicatedName: PropTypes.func.isRequired, addFolderCancel: PropTypes.func.isRequired, }; class CreateForder extends React.Component { constructor(props) { super(props); this.state = { parentPath: '', childName: '', errMessage: '', isSubmitBtnActive: false, }; } componentDidMount() { let parentPath = this.props.parentPath; if (parentPath[parentPath.length - 1] === '/') { // mainPanel this.setState({ parentPath: parentPath }); } else { this.setState({ parentPath: parentPath + '/' }); // sidePanel } } handleChange = (e) => { if (!e.target.value.trim()) { this.setState({ isSubmitBtnActive: false }); } else { this.setState({ isSubmitBtnActive: true }); } this.setState({ childName: e.target.value }); }; handleSubmit = () => { if (!this.state.isSubmitBtnActive) { return; } let newName = this.state.childName.trim(); let { isValid, errMessage } = validateName(newName); if (!isValid) { this.setState({ errMessage }); return; } let isDuplicated = this.props.checkDuplicatedName(newName); if (isDuplicated) { let errMessage = gettext('The name "{name}" is already taken. Please choose a different name.'); errMessage = errMessage.replace('{name}', Utils.HTMLescape(newName)); this.setState({ errMessage }); return; } this.props.onAddFolder(this.state.parentPath + newName); }; handleKeyDown = (e) => { if (e.key === 'Enter') { this.handleSubmit(); e.preventDefault(); } }; toggle = () => { this.props.addFolderCancel(); }; render() { return ( {gettext('New Folder')}
{this.state.errMessage && {this.state.errMessage}}
); } } CreateForder.propTypes = propTypes; export default CreateForder;