1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-14 14:21:23 +00:00
Files
seahub/frontend/src/pages/wikis/wikis.js

188 lines
5.5 KiB
JavaScript
Raw Normal View History

2018-12-10 13:33:32 +08:00
import React, { Component, Fragment } from 'react';
2019-04-22 18:21:25 +08:00
import PropTypes from 'prop-types';
import { Button } from 'reactstrap';
2018-12-08 00:01:23 +08:00
import { seafileAPI } from '../../utils/seafile-api';
import { gettext, loginUrl, canPublishRepo } from '../../utils/constants';
2018-12-11 11:20:40 +08:00
import toaster from '../../components/toast';
2018-12-11 08:42:30 +08:00
import ModalPortal from '../../components/modal-portal';
2018-12-26 16:27:15 +08:00
import CommonToolbar from '../../components/toolbar/common-toolbar';
2018-12-10 13:33:32 +08:00
import NewWikiDialog from '../../components/dialog/new-wiki-dialog';
import WikiSelectDialog from '../../components/dialog/wiki-select-dialog';
2018-12-11 08:42:30 +08:00
import WikiListView from '../../components/wiki-list-view/wiki-list-view';
2018-12-08 00:01:23 +08:00
2019-04-22 18:21:25 +08:00
const propTypes = {
onShowSidePanel: PropTypes.func.isRequired,
onSearchedClick: PropTypes.func.isRequired,
};
2018-12-08 00:01:23 +08:00
class Wikis extends Component {
constructor(props) {
super(props);
this.state = {
loading: true,
errorMsg: '',
wikis: [],
2018-12-11 13:44:09 +08:00
isShowAddWikiMenu: false,
2018-12-08 00:01:23 +08:00
isShowSelectDialog: false,
isShowCreateDialog: false,
};
}
componentDidMount() {
this.getWikis();
}
getWikis = () => {
seafileAPI.listWikis().then(res => {
this.setState({
loading: false,
wikis: res.data.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.')
});
}
});
}
2018-12-11 13:22:52 +08:00
clickMenuToggle = (e) => {
e.preventDefault();
this.onMenuToggle();
2018-12-08 00:01:23 +08:00
}
2018-12-11 13:22:52 +08:00
onMenuToggle = () => {
2018-12-11 13:44:09 +08:00
this.setState({isShowAddWikiMenu: !this.state.isShowAddWikiMenu});
2018-12-08 00:01:23 +08:00
}
onSelectToggle = () => {
2018-12-11 13:44:09 +08:00
this.setState({isShowSelectDialog: !this.state.isShowSelectDialog});
2018-12-08 00:01:23 +08:00
}
onCreateToggle = () => {
2018-12-11 13:44:09 +08:00
this.setState({isShowCreateDialog: !this.state.isShowCreateDialog});
2018-12-08 00:01:23 +08:00
}
addWiki = (repoID) => {
seafileAPI.addWiki(repoID).then((res) => {
2018-12-08 00:01:23 +08:00
this.state.wikis.push(res.data);
2018-12-11 13:44:09 +08:00
this.setState({wikis: this.state.wikis});
2018-12-08 00:01:23 +08:00
}).catch((error) => {
if(error.response) {
let errorMsg = error.response.data.error_msg;
2018-12-11 11:20:40 +08:00
toaster.danger(errorMsg);
2018-12-08 00:01:23 +08:00
}
});
}
2018-12-08 11:40:28 +08:00
renameWiki = (wiki, newName) => {
seafileAPI.renameWiki(wiki.slug, newName).then((res) => {
let wikis = this.state.wikis.map((item) => {
if (item.name === wiki.name) {
item = res.data;
}
return item;
});
2018-12-11 11:20:40 +08:00
this.setState({wikis: wikis});
2018-12-08 11:40:28 +08:00
}).catch((error) => {
if(error.response) {
let errorMsg = error.response.data.error_msg;
2018-12-11 11:20:40 +08:00
toaster.danger(errorMsg);
2018-12-08 11:40:28 +08:00
}
});
}
2018-12-08 00:01:23 +08:00
deleteWiki = (wiki) => {
seafileAPI.deleteWiki(wiki.slug).then(() => {
2018-12-11 11:20:40 +08:00
let wikis = this.state.wikis.filter(item => {
return item.name !== wiki.name;
2018-12-08 00:01:23 +08:00
});
2018-12-11 11:20:40 +08:00
this.setState({wikis: wikis});
2018-12-08 00:01:23 +08:00
}).catch((error) => {
if(error.response) {
let errorMsg = error.response.data.error_msg;
2018-12-11 11:20:40 +08:00
toaster.danger(errorMsg);
2018-12-08 00:01:23 +08:00
}
});
}
render() {
return (
2018-12-10 13:33:32 +08:00
<Fragment>
2019-02-20 11:54:25 +08:00
<div className="main-panel-north border-left-show">
<div className="cur-view-toolbar">
2018-12-26 16:27:15 +08:00
<span className="sf2-icon-menu side-nav-toggle hidden-md-up d-md-none" title="Side Nav Menu" onClick={this.props.onShowSidePanel}></span>
<div className="operation">
{canPublishRepo &&
2019-05-28 14:38:29 +08:00
<Button className="btn btn-secondary operation-item my-1" onClick={this.onSelectToggle}>
{gettext('Publish a Library')}
</Button>
}
2018-12-26 16:27:15 +08:00
</div>
</div>
<CommonToolbar onSearchedClick={this.props.onSearchedClick} />
</div>
<div className="main-panel-center">
<div className="cur-view-container" id="wikis">
<div className="cur-view-path">
<div className="path-container">
<h3 className="sf-heading">{gettext('Published Libraries')}</h3>
2018-12-08 00:01:23 +08:00
</div>
2018-12-10 13:33:32 +08:00
</div>
<div className="cur-view-content">
{(this.state.loading || this.state.wikis.length !== 0) &&
2018-12-11 08:42:30 +08:00
<WikiListView
2018-12-10 13:33:32 +08:00
data={this.state}
renameWiki={this.renameWiki}
deleteWiki={this.deleteWiki}
/>
}
{(!this.state.loading && this.state.wikis.length === 0) &&
<div className="message empty-tip">
2019-05-10 15:05:21 +08:00
<h2>{gettext('You do not have any public library')}</h2>
<p>{gettext('Public libraries are for publishing your contents in an organized way.')}</p>
2018-12-10 13:33:32 +08:00
</div>
}
</div>
2018-12-08 00:01:23 +08:00
</div>
</div>
2018-12-11 13:44:09 +08:00
{this.state.isShowCreateDialog && (
2018-12-10 13:33:32 +08:00
<ModalPortal>
<NewWikiDialog
toggleCancel={this.onCreateToggle}
addWiki={this.addWiki}
/>
</ModalPortal>
2018-12-11 13:44:09 +08:00
)}
{this.state.isShowSelectDialog && (
2018-12-10 13:33:32 +08:00
<ModalPortal>
<WikiSelectDialog
toggleCancel={this.onSelectToggle}
addWiki={this.addWiki}
/>
</ModalPortal>
2018-12-11 13:44:09 +08:00
)}
2018-12-10 13:33:32 +08:00
</Fragment>
2018-12-08 00:01:23 +08:00
);
}
}
2019-04-22 18:21:25 +08:00
Wikis.propTypes = propTypes;
2018-12-08 00:01:23 +08:00
export default Wikis;