import React from 'react'; import PropTypes from 'prop-types'; import RepoListView from './repo-list-view'; import { seafileAPI } from '../../utils/seafile-api'; import { gettext } from '../../utils/constants'; import RepoInfo from '../../models/repo-info'; import '../../css/file-chooser.css'; const propTypes = { isShowFile: PropTypes.bool, repoID: PropTypes.string, onDirentItemClick: PropTypes.func, onRepoItemClick: PropTypes.func, }; class FileChooser extends React.Component { constructor(props) { super(props); this.state = { hasRequest: false, isCurrentRepoShow: true, isOtherRepoShow: false, repoList: [], currentRepoInfo: null, selectedRepo: null, selectedPath: '', }; } componentDidMount() { if (this.props.repoID) { let repoID = this.props.repoID; seafileAPI.getRepoInfo(repoID).then(res => { // need to optimized let repoInfo = new RepoInfo(res.data); this.setState({ currentRepoInfo: repoInfo, }); }); } } onOtherRepoToggle = () => { if (!this.state.hasRequest) { let that = this; seafileAPI.listRepos().then(res => { // todo optimized code let repos = res.data.repos; let repoList = []; let repoIdList = []; for(let i = 0; i < repos.length; i++) { if (repos[i].permission !== 'rw') { continue; } if (that.props.repoID && (repos[i].repo_name === that.state.currentRepoInfo.repo_name)) { continue; } if (repoIdList.indexOf(repos[i].repo_id) > -1) { continue; } repoList.push(repos[i]); repoIdList.push(repos[i].repo_id); } this.setState({ repoList: repoList, isOtherRepoShow: !this.state.isOtherRepoShow, }); }); } else { this.setState({isOtherRepoShow: !this.state.isOtherRepoShow}); } } onCurrentRepoToggle = () => [ this.setState({isCurrentRepoShow: !this.state.isCurrentRepoShow}) ] onDirentItemClick = (repo, filePath, dirent) => { this.props.onDirentItemClick(repo, filePath, dirent); this.setState({ selectedRepo: repo, selectedPath: filePath }); } onRepoItemClick = (repo) => { if (this.props.onRepoItemClick) { this.props.onRepoItemClick(repo); } this.setState({ selectedRepo: repo, selectedPath: '', }); } render() { return (
{ this.props.repoID &&
{gettext('Current Library')}
{ this.state.isCurrentRepoShow && this.state.currentRepoInfo && }
}
{gettext('Other Libraries')}
{ this.state.isOtherRepoShow && }
); } } FileChooser.propTypes = propTypes; export default FileChooser;