import React from 'react'; import PropTypes from 'prop-types'; import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Alert } from 'reactstrap'; import { gettext } from '../../utils/constants'; import { seafileAPI } from '../../utils/seafile-api'; import FileChooser from '../file-chooser/file-chooser'; const propTypes = { repoID: PropTypes.string.isRequired, filePath: PropTypes.string.isRequired, toggleCancel: PropTypes.func.isRequired, onRelatedFileChange: PropTypes.func.isRequired, }; class AddRelatedFileDialog extends React.Component { constructor(props) { super(props); this.state = { repo: null, selectedPath: '', isShowFile: true, errMessage: '', }; } handleSubmit = () => { this.onAddRelatedFile(); } onAddRelatedFile = () => { let { repo, selectedPath } = this.state; let oRepoID = this.props.repoID; let rRepoID = repo.repo_id; let oFilePath = this.props.filePath; let rFilePath = selectedPath; if (oRepoID === rRepoID && oFilePath === rFilePath) { let message = gettext('Can not select self as a related file.'); this.setState({errMessage: message}); return; } seafileAPI.addRelatedFile(oRepoID, rRepoID, oFilePath, rFilePath).then((res) => { this.props.onRelatedFileChange(); this.toggle(); }).catch((error) => { if (error.response) { this.setState({ errMessage: error.response.data.error_msg }); } }); } toggle = () => { this.props.toggleCancel(); } onDirentItemClick = (repo, selectedPath, dirent) => { if(dirent.type === 'file') { this.setState({ repo: repo, selectedPath: selectedPath, }); } else { this.setState({ repo: null, selectedPath: '', }); } } render() { return ( {gettext('Select File')} {this.state.errMessage && {this.state.errMessage}} { this.state.selectedPath ? : } ); } } AddRelatedFileDialog.propTypes = propTypes; export default AddRelatedFileDialog;