import React from 'react'; import PropTypes from 'prop-types'; import moment from 'moment'; import { Button, Modal, ModalHeader, ModalBody, ModalFooter, Alert } from 'reactstrap'; import { Utils } from '../../utils/utils'; import { seafileAPI } from '../../utils/seafile-api.js'; import { gettext, siteRoot } from '../../utils/constants'; const propTypes = { repoID: PropTypes.string.isRequired, repoName: PropTypes.string.isRequired, toggleDialog: PropTypes.func.isRequired }; class SearchFileDialog extends React.Component { constructor(props) { super(props); this.state = { isSubmitDisabled: true, q: '', errMessage: '', fileList: [] }; } searchFile = () => { const { q } = this.state; if (!q.trim()) { return false; } seafileAPI.searchFileInRepo(this.props.repoID, q).then((res) => { this.setState({ fileList: res.data.data, errMessage: '' }); }).catch(error => { let errMessage = Utils.getErrorMsg(error); this.setState({ errMessage: errMessage }); }); } handleKeyDown = (e) => { if (e.key == 'Enter') { e.preventDefault(); this.searchFile(); } } toggle = () => { this.props.toggleDialog(); } handleInputChange = (e) => { const q = e.target.value; this.setState({ q: q, isSubmitDisabled: !q.trim() }); } render() { const { q, errMessage, fileList, isSubmitDisabled } = this.state; return ( {gettext('Search')}
{errMessage && {errMessage}}
{fileList.length > 0 && {fileList.map((item, index) => { return ( ); }) }
{gettext('Name')} {gettext('Size')} {gettext('Last Update')}
}
); } } SearchFileDialog.propTypes = propTypes; const FileItemPropTypes = { repoID: PropTypes.string.isRequired, repoName: PropTypes.string.isRequired, item: PropTypes.object.isRequired }; class FileItem extends React.PureComponent { render() { const { item, repoID, repoName } = this.props; const name = item.path.substr(item.path.lastIndexOf('/') + 1); const url = item.type == 'file' ? `${siteRoot}lib/${repoID}/file${Utils.encodePath(item.path)}` : `${siteRoot}library/${repoID}/${Utils.encodePath(repoName + item.path)}`; return( {name} {item.type == 'file' ? Utils.bytesToSize(item.size) : ''} {moment(item.mtime).fromNow()} ); } } FileItem.propTypes = FileItemPropTypes; export default SearchFileDialog;