import React from 'react'; import PropTypes from 'prop-types'; import { Modal, ModalHeader, ModalBody, NavLink } from 'reactstrap'; import { seafileAPI } from '../../utils/seafile-api'; import { siteRoot, gettext } from '../../utils/constants'; import { Utils } from '../../utils/utils'; import toaster from '../toast'; import Loading from '../loading'; import EmptyTip from '../empty-tip'; const repoShareUploadLinkItemPropTypes = { item: PropTypes.object.isRequired, activeTab: PropTypes.string.isRequired, deleteItem: PropTypes.func.isRequired }; class RepoShareUploadLinkItem extends React.Component { constructor(props) { super(props); this.state = { isOperationShow: false }; } onMouseEnter = () => { this.setState({isOperationShow: true}); }; onMouseLeave = () => { this.setState({isOperationShow: false}); }; onDeleteLink = (e) => { e.preventDefault(); this.props.deleteItem(this.props.item.token); }; render() { let objUrl; let item = this.props.item; let path = item.path === '/' ? '/' : item.path.slice(0, item.path.length - 1); if (this.props.activeTab === 'shareLinks') { if (item.is_dir) { objUrl = `${siteRoot}library/${item.repo_id}/${encodeURIComponent(item.repo_name)}${Utils.encodePath(path)}`; } else { objUrl = `${siteRoot}lib/${item.repo_id}/file${Utils.encodePath(item.path)}`; } } if (this.props.activeTab === 'uploadLinks') { objUrl = `${siteRoot}library/${item.repo_id}/${encodeURIComponent(item.repo_name)}${Utils.encodePath(path)}`; } return ( {item.creator_name} {item.obj_name} {item.link} ); } } RepoShareUploadLinkItem.propTypes = repoShareUploadLinkItemPropTypes; const RepoShareUploadLinksDialogPropTypes = { repo: PropTypes.object.isRequired, toggleDialog: PropTypes.func.isRequired }; class RepoShareUploadLinksDialog extends React.Component { constructor(props) { super(props); this.state = { loading: true, activeTab: 'shareLinks', repoShareUploadLinkList: [], errorMsg: '' }; } componentDidMount() { this.getItems('share-link'); } getItems = (itemType) => { const repoID = this.props.repo.repo_id; const request = itemType == 'share-link' ? seafileAPI.listRepoShareLinks(repoID) : seafileAPI.listRepoUploadLinks(repoID); request.then((res) => { this.setState({ loading: false, repoShareUploadLinkList: res.data, }); }).catch(error => { this.setState({ isLoading: false, errorMsg: Utils.getErrorMsg(error, true) }); }); } deleteItem = (token) => { const repoID = this.props.repo.repo_id; const request = this.state.activeTab == 'shareLinks' ? seafileAPI.deleteRepoShareLink(repoID, token) : seafileAPI.deleteRepoUploadLink(repoID, token); request.then((res) => { const repoShareUploadLinkList = this.state.repoShareUploadLinkList.filter(item => { return item.token !== token; }); this.setState({ repoShareUploadLinkList: repoShareUploadLinkList }); }).catch(error => { toaster.danger(Utils.getErrorMsg(error)); }); }; toggle = (tab) => { if (this.state.activeTab !== tab) { this.setState({activeTab: tab}); } if (tab == 'shareLinks') { this.getItems('share-link'); } if (tab == 'uploadLinks') { this.getItems('upload-link'); } }; render() { const { loading, errorMsg, activeTab, repoShareUploadLinkList } = this.state; const itemName = '' + Utils.HTMLescape(this.props.repo.repo_name) + ''; const title = gettext('{placeholder} Share/Upload Links').replace('{placeholder}', itemName); return (
  • {gettext('Share Links')}
  • {gettext('Upload Links')}
{loading && } {!loading && errorMsg &&

{errorMsg}

} {!loading && !errorMsg && !repoShareUploadLinkList.length &&

{activeTab == 'shareLinks' ? gettext('No share links') : gettext('No upload links')}

} {!loading && !errorMsg && repoShareUploadLinkList.length > 0 && {this.state.repoShareUploadLinkList.map((item, index) => { return ( ); })}
{gettext('Creator')} {gettext('Name')} {gettext('Link')}
}
); } } RepoShareUploadLinksDialog.propTypes = RepoShareUploadLinksDialogPropTypes; export default RepoShareUploadLinksDialog;