import React from 'react'; import PropTypes from 'prop-types'; import { seafileAPI } from '../../utils/seafile-api'; import Dirent from '../../models/dirent'; const propTypes = { filePath: PropTypes.string, selectedPath: PropTypes.string, dirent: PropTypes.object.isRequired, repo: PropTypes.object.isRequired, onDirentItemClick: PropTypes.func.isRequired, }; class DirentListItem extends React.Component { constructor(props) { super(props); let filePath = this.props.filePath ? this.props.filePath + '/' + this.props.dirent.name : '/' + this.props.dirent.name; this.state = { isShowChildren: false, hasRequest: false, hasChildren: true, filePath: filePath, direntList: [], }; } onItemClick = () => { this.props.onDirentItemClick(this.state.filePath); } onToggleClick = () => { if (!this.state.hasRequest) { seafileAPI.listDir(this.props.repo.repo_id, this.state.filePath).then(res => { let direntList = []; res.data.forEach(item => { if (item.type === 'dir') { let dirent = new Dirent(item); direntList.push(dirent); } this.setState({ hasRequest: true, direntList: direntList, }); }); if (res.data.length === 0 || direntList.length === 0) { this.setState({ hasRequest: true, direntList: [], hasChildren: false }); } }); } this.setState({isShowChildren: !this.state.isShowChildren}); } renderChildren = () => { return (