import React, { Component } from 'react'; import { Link } from '@reach/router'; import moment from 'moment'; import { Modal, ModalHeader, ModalBody } from 'reactstrap'; import { seafileAPI } from '../../utils/seafile-api'; import { Utils } from '../../utils/utils'; import { gettext, siteRoot, loginUrl, isPro, canGenerateUploadLink } from '../../utils/constants'; class Content extends Component { constructor(props) { super(props); this.state = { modalOpen: false, modalContent: '' }; this.toggleModal = this.toggleModal.bind(this); this.showModal = this.showModal.bind(this); } // required by `Modal`, and can only set the 'open' state toggleModal() { this.setState({ modalOpen: !this.state.modalOpen }); } showModal(options) { this.toggleModal(); this.setState({ modalContent: options.content }); } render() { const {loading, errorMsg, items} = this.props.data; if (loading) { return ; } else if (errorMsg) { return

{errorMsg}

; } else { const emptyTip = (

{gettext("You don't have any share links")}

{gettext("You can generate a share link for a folder or a file. Anyone who receives this link can view the folder or the file online.")}

); const table = ( {/* TODO:sort */} {/*TODO:sort*/}
{/*icon*/} {gettext("Name")} {gettext("Library")} {gettext("Visits")} {gettext("Expiration")} {/*Operations*/}
{gettext('Link')} {this.state.modalContent}
); return items.length ? table : emptyTip; } } } class TableBody extends Component { constructor(props) { super(props); this.state = { //items: this.props.items }; } render() { let listItems = this.props.items.map(function(item, index) { return ; }, this); return ( {listItems} ); } } class Item extends Component { constructor(props) { super(props); this.state = { showOpIcon: false, deleted: false }; this.handleMouseOver = this.handleMouseOver.bind(this); this.handleMouseOut = this.handleMouseOut.bind(this); this.viewLink = this.viewLink.bind(this); this.removeLink = this.removeLink.bind(this); } handleMouseOver() { this.setState({ showOpIcon: true }); } handleMouseOut() { this.setState({ showOpIcon: false }); } viewLink(e) { e.preventDefault(); this.props.showModal({content: this.props.data.link}); } removeLink(e) { e.preventDefault(); const data = this.props.data; seafileAPI.deleteShareLink(data.token) .then((res) => { this.setState({ deleted: true }); // TODO: show feedback msg // gettext("Successfully deleted 1 item") }) .catch((error) => { // TODO: show feedback msg }); } render() { if (this.state.deleted) { return null; } const data = this.props.data; const icon_size = Utils.isHiDPI() ? 48 : 24; if (data.is_dir) { data.icon_url = Utils.getFolderIconUrl({ is_readonly: false, size: icon_size }); data.url = `${siteRoot}#my-libs/lib/${data.repo_id}${Utils.encodePath(data.path)}`; } else { data.icon_url = Utils.getFileIconUrl(data.obj_name, icon_size); data.url = `${siteRoot}lib/${data.repo_id}/file${Utils.encodePath(data.path)}`; } let showDate = function(options) { const date = moment(options.date).format('YYYY-MM-DD'); return options.is_expired ? {date} : date; } let iconVisibility = this.state.showOpIcon ? '' : ' invisible'; let linkIconClassName = 'sf2-icon-link op-icon' + iconVisibility; let deleteIconClassName = 'sf2-icon-delete op-icon' + iconVisibility; const item = ( {data.obj_name} {data.repo_name} {data.view_cnt} {data.expire_date ? showDate({date: data.expire_date, is_expired: data.is_expired}) : '--'} ); return item; } } class ShareAdminShareLinks extends Component { constructor(props) { super(props); this.state = { loading: true, errorMsg: '', items: [] }; } componentDidMount() { seafileAPI.listShareLinks().then((res) => { // res: {data: Array(2), status: 200, statusText: "OK", headers: {…}, config: {…}, …} this.setState({ loading: false, items: res.data }); }).catch((error) => { if (error.response) { if (error.response.status == 403) { this.setState({ loading: false, errorMsg: gettext("Permission denied") }); location.href = `${loginUrl}?next=${encodeURIComponent(location.href)}`; } else { this.setState({ loading: false, errorMsg: gettext("Error") }); } } else { this.setState({ loading: false, errorMsg: gettext("Please check the network.") }); } }); } render() { return (
  • {gettext('Share Links')}
  • { canGenerateUploadLink ?
  • {gettext('Upload Links')}
  • : '' }
); } } export default ShareAdminShareLinks;