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';
|
2019-04-17 10:34:43 +08:00
|
|
|
import { Button } from 'reactstrap';
|
2019-05-29 11:48:00 +08:00
|
|
|
import MediaQuery from 'react-responsive';
|
2023-07-14 12:55:30 +08:00
|
|
|
import { gettext, canPublishRepo } from '../../utils/constants';
|
2019-12-05 15:45:16 +08:00
|
|
|
import { Utils } from '../../utils/utils';
|
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';
|
2019-06-10 17:30:10 +08:00
|
|
|
import EmptyTip from '../../components/empty-tip';
|
2018-12-26 16:27:15 +08:00
|
|
|
import CommonToolbar from '../../components/toolbar/common-toolbar';
|
2024-05-15 11:57:30 +08:00
|
|
|
import AddWikiDialog from '../../components/dialog/add-wiki-dialog';
|
2018-12-11 08:42:30 +08:00
|
|
|
import WikiListView from '../../components/wiki-list-view/wiki-list-view';
|
2024-05-15 11:57:30 +08:00
|
|
|
import wikiAPI from '../../utils/wiki-api';
|
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,
|
2024-05-15 11:57:30 +08:00
|
|
|
isShowAddDialog: false,
|
2018-12-08 00:01:23 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.getWikis();
|
|
|
|
}
|
|
|
|
|
|
|
|
getWikis = () => {
|
2024-05-15 11:57:30 +08:00
|
|
|
let wikis = [];
|
|
|
|
wikiAPI.listWikis().then(res => {
|
|
|
|
wikis = wikis.concat(res.data.data);
|
|
|
|
wikis.map(wiki => {
|
|
|
|
return wiki['version'] = 'v1';
|
|
|
|
});
|
|
|
|
wikiAPI.listWikis2().then(res => {
|
2024-05-27 17:19:58 +08:00
|
|
|
let wikis2 = res.data.wikis;
|
2024-05-15 11:57:30 +08:00
|
|
|
wikis2.map(wiki => {
|
|
|
|
return wiki['version'] = 'v2';
|
|
|
|
});
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
wikis: wikis.concat(wikis2)
|
|
|
|
});
|
|
|
|
}).catch((error) => {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
errorMsg: Utils.getErrorMsg(error, true) // true: show login tip if 403
|
|
|
|
});
|
|
|
|
});
|
2018-12-08 00:01:23 +08:00
|
|
|
this.setState({
|
|
|
|
loading: false,
|
2024-05-15 11:57:30 +08:00
|
|
|
wikis: wikis
|
2018-12-08 00:01:23 +08:00
|
|
|
});
|
|
|
|
}).catch((error) => {
|
2019-12-05 15:45:16 +08:00
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
errorMsg: Utils.getErrorMsg(error, true) // true: show login tip if 403
|
|
|
|
});
|
2018-12-08 00:01:23 +08:00
|
|
|
});
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2018-12-08 00:01:23 +08:00
|
|
|
|
2018-12-11 13:22:52 +08:00
|
|
|
clickMenuToggle = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.onMenuToggle();
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
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});
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2018-12-08 00:01:23 +08:00
|
|
|
|
2024-05-15 11:57:30 +08:00
|
|
|
toggelAddWikiDialog = () => {
|
|
|
|
this.setState({isShowAddDialog: !this.state.isShowAddDialog});
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2018-12-08 00:01:23 +08:00
|
|
|
|
2024-05-28 16:31:00 +08:00
|
|
|
addWiki = (wikiName, departmentID) => {
|
|
|
|
wikiAPI.addWiki2(wikiName, departmentID).then((res) => {
|
2024-05-15 11:57:30 +08:00
|
|
|
let wikis = this.state.wikis.slice(0);
|
|
|
|
let new_wiki = res.data;
|
|
|
|
new_wiki['version'] = 'v2';
|
|
|
|
wikis.push(new_wiki);
|
|
|
|
this.setState({ wikis });
|
2018-12-08 00:01:23 +08:00
|
|
|
}).catch((error) => {
|
2024-05-15 11:57:30 +08:00
|
|
|
if (error.response) {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2018-12-08 00:01:23 +08:00
|
|
|
}
|
|
|
|
});
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2018-12-08 00:01:23 +08:00
|
|
|
|
2024-05-15 11:57:30 +08:00
|
|
|
deleteWiki = (wiki) => {
|
|
|
|
if (wiki.version === 'v1') {
|
|
|
|
wikiAPI.deleteWiki(wiki.id).then(() => {
|
|
|
|
let wikis = this.state.wikis.filter(item => {
|
|
|
|
return item.name !== wiki.name;
|
|
|
|
});
|
|
|
|
this.setState({wikis: wikis});
|
|
|
|
}).catch((error) => {
|
|
|
|
if(error.response) {
|
|
|
|
let errorMsg = error.response.data.error_msg;
|
|
|
|
toaster.danger(errorMsg);
|
2018-12-08 11:40:28 +08:00
|
|
|
}
|
|
|
|
});
|
2024-05-15 11:57:30 +08:00
|
|
|
} else {
|
|
|
|
wikiAPI.deleteWiki2(wiki.id).then(() => {
|
|
|
|
let wikis = this.state.wikis.filter(item => {
|
|
|
|
return item.name !== wiki.name;
|
|
|
|
});
|
|
|
|
this.setState({wikis: wikis});
|
|
|
|
}).catch((error) => {
|
|
|
|
if(error.response) {
|
|
|
|
let errorMsg = error.response.data.error_msg;
|
|
|
|
toaster.danger(errorMsg);
|
|
|
|
}
|
2018-12-08 00:01:23 +08:00
|
|
|
});
|
2024-05-15 11:57:30 +08:00
|
|
|
}
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
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>
|
2023-07-14 12:55:30 +08:00
|
|
|
{canPublishRepo &&
|
2018-12-26 16:27:15 +08:00
|
|
|
<div className="operation">
|
2019-10-15 10:15:04 +08:00
|
|
|
<Fragment>
|
2019-05-29 11:48:00 +08:00
|
|
|
<MediaQuery query="(min-width: 768px)">
|
2024-05-15 11:57:30 +08:00
|
|
|
<Button className="btn btn-secondary operation-item" onClick={this.toggelAddWikiDialog}>{gettext('Add Wiki')}</Button>
|
2019-05-29 11:48:00 +08:00
|
|
|
</MediaQuery>
|
|
|
|
<MediaQuery query="(max-width: 767.8px)">
|
2024-05-15 11:57:30 +08:00
|
|
|
<span className="sf2-icon-plus mobile-toolbar-icon" title={gettext('Add Wiki')} onClick={this.toggelAddWikiDialog}></span>
|
2019-05-29 11:48:00 +08:00
|
|
|
</MediaQuery>
|
2019-10-15 10:15:04 +08:00
|
|
|
</Fragment>
|
2018-12-26 16:27:15 +08:00
|
|
|
</div>
|
2023-07-14 12:55:30 +08:00
|
|
|
}
|
2018-12-26 16:27:15 +08:00
|
|
|
</div>
|
|
|
|
<CommonToolbar onSearchedClick={this.props.onSearchedClick} />
|
|
|
|
</div>
|
2024-05-15 11:57:30 +08:00
|
|
|
{this.state.isShowAddDialog &&
|
|
|
|
<ModalPortal>
|
|
|
|
<AddWikiDialog toggleCancel={this.toggelAddWikiDialog} addWiki={this.addWiki} />
|
|
|
|
</ModalPortal>
|
|
|
|
}
|
2018-12-26 16:27:15 +08:00
|
|
|
<div className="main-panel-center">
|
|
|
|
<div className="cur-view-container" id="wikis">
|
|
|
|
<div className="cur-view-path">
|
|
|
|
<div className="path-container">
|
2024-05-06 17:09:47 +08:00
|
|
|
<h3 className="sf-heading m-0">{gettext('Wikis')}</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}
|
|
|
|
deleteWiki={this.deleteWiki}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
{(!this.state.loading && this.state.wikis.length === 0) &&
|
2019-06-10 17:30:10 +08:00
|
|
|
<EmptyTip>
|
2024-05-15 11:57:30 +08:00
|
|
|
<h2>{gettext('No Wikis')}</h2>
|
|
|
|
<p>{gettext('You have not any wikis yet.')}</p>
|
|
|
|
<p>{gettext('A wiki can be accessed by anyone, not only users, via its URL.')}</p>
|
|
|
|
<p>{gettext('You can add a wiki by clicking the "Add Wiki" button in the menu bar.')}</p>
|
2019-06-10 17:30:10 +08:00
|
|
|
</EmptyTip>
|
2018-12-10 13:33:32 +08:00
|
|
|
}
|
|
|
|
</div>
|
2018-12-08 00:01:23 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
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;
|