1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-13 23:14:29 +00:00
seahub/frontend/src/pages/share-admin/upload-links.js

238 lines
6.2 KiB
JavaScript
Raw Normal View History

2018-11-26 09:53:18 +00:00
import React, { Component } from 'react';
import { Link } from '@reach/router';
import { Modal, ModalHeader, ModalBody } from 'reactstrap';
import { seafileAPI } from '../../utils/seafile-api';
import { Utils } from '../../utils/utils';
2018-12-26 08:07:22 +00:00
import { gettext, siteRoot, loginUrl, canGenerateShareLink } from '../../utils/constants';
class Content extends Component {
2018-12-26 08:07:22 +00:00
constructor(props) {
super(props);
this.state = {
modalOpen: false,
modalContent: ''
};
}
// required by `Modal`, and can only set the 'open' state
2018-12-26 08:07:22 +00:00
toggleModal = () => {
this.setState({
modalOpen: !this.state.modalOpen
});
}
2018-12-26 08:07:22 +00:00
showModal = (options) => {
this.toggleModal();
this.setState({
modalContent: options.content
});
}
render() {
const {loading, errorMsg, items} = this.props.data;
if (loading) {
return <span className="loading-icon loading-tip"></span>;
} else if (errorMsg) {
return <p className="error text-center">{errorMsg}</p>;
} else {
const emptyTip = (
<div className="empty-tip">
<h2>{gettext("You don't have any upload links")}</h2>
<p>{gettext("You can generate an upload link from any folder. Anyone who receives this link can upload files to this folder.")}</p>
</div>
);
const table = (
<React.Fragment>
2018-12-26 08:07:22 +00:00
<table className="table-hover">
<thead>
<tr>
2018-12-26 08:07:22 +00:00
<th width="4%">{/*icon*/}</th>
<th width="42%">{gettext("Name")}</th>
<th width="30%">{gettext("Library")}</th>
2018-12-26 08:07:22 +00:00
<th width="14%">{gettext("Visits")}</th>
<th width="10%">{/*Operations*/}</th>
</tr>
</thead>
<TableBody items={items} showModal={this.showModal} />
</table>
<Modal isOpen={this.state.modalOpen} toggle={this.toggleModal} centered={true}>
<ModalHeader toggle={this.toggleModal}>{gettext('Link')}</ModalHeader>
<ModalBody>
{this.state.modalContent}
</ModalBody>
</Modal>
</React.Fragment>
);
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 <Item key={index} data={item} showModal={this.props.showModal} />;
}, this);
return (
<tbody>{listItems}</tbody>
);
}
}
class Item extends Component {
constructor(props) {
super(props);
this.state = {
showOpIcon: false,
deleted: false
};
}
2018-12-26 08:07:22 +00:00
handleMouseOver = () => {
this.setState({showOpIcon: true});
}
2018-12-26 08:07:22 +00:00
handleMouseOut = () => {
this.setState({showOpIcon: false});
}
2018-12-26 08:07:22 +00:00
viewLink = (e) => {
e.preventDefault();
this.props.showModal({content: this.props.data.link});
}
2018-12-26 08:07:22 +00:00
removeLink = (e) => {
e.preventDefault();
const data = this.props.data;
seafileAPI.deleteUploadLink(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;
data.icon_url = Utils.getFolderIconUrl({
is_readonly: false,
size: icon_size
2018-12-26 08:07:22 +00:00
});
data.url = `${siteRoot}library/${data.repo_id}/${data.repo_name}${Utils.encodePath(data.path)}`;
let iconVisibility = this.state.showOpIcon ? '' : ' invisible';
let linkIconClassName = 'sf2-icon-link op-icon' + iconVisibility;
let deleteIconClassName = 'sf2-icon-delete op-icon' + iconVisibility;
const item = (
<tr onMouseOver={this.handleMouseOver} onMouseOut={this.handleMouseOut}>
<td><img src={data.icon_url} width="24" /></td>
2018-12-26 08:07:22 +00:00
<td><Link to={data.url}>{data.obj_name}</Link></td>
<td><Link to={`${siteRoot}library/${data.repo_id}/${data.repo_name}`}>{data.repo_name}</Link></td>
<td>{data.view_cnt}</td>
<td>
<a href="#" className={linkIconClassName} title={gettext('View')} onClick={this.viewLink}></a>
<a href="#" className={deleteIconClassName} title={gettext('Remove')} onClick={this.removeLink}></a>
</td>
</tr>
);
return item;
}
}
class ShareAdminUploadLinks extends Component {
2018-12-26 08:07:22 +00:00
constructor(props) {
super(props);
this.state = {
loading: true,
errorMsg: '',
items: []
};
}
componentDidMount() {
seafileAPI.listUploadLinks().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 (
2018-11-26 09:53:18 +00:00
<div className="main-panel-center">
<div className="cur-view-container">
<div className="cur-view-path">
<ul className="nav">
{ canGenerateShareLink ?
<li className="nav-item"><Link to={`${siteRoot}share-admin-share-links/`} className="nav-link">{gettext('Share Links')}</Link></li>
: '' }
<li className="nav-item"><Link to={`${siteRoot}share-admin-upload-links/`} className="nav-link active">{gettext('Upload Links')}</Link></li>
</ul>
</div>
<div className="cur-view-content">
<Content data={this.state} />
2018-11-26 06:00:32 +00:00
</div>
</div>
2018-11-26 09:53:18 +00:00
</div>
);
}
}
export default ShareAdminUploadLinks;