2018-12-10 11:52:44 +08:00
|
|
|
import React, { Fragment } from 'react';
|
2018-12-08 08:37:18 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2018-12-10 11:52:44 +08:00
|
|
|
import { gettext, siteRoot, loginUrl } from '../../utils/constants';
|
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
|
|
|
import Loading from '../../components/loading';
|
|
|
|
import Group from '../../models/group';
|
2018-12-18 17:21:01 +08:00
|
|
|
import Repo from '../../models/repo';
|
2019-05-28 17:48:44 +08:00
|
|
|
import toaster from '../../components/toast';
|
2018-12-12 18:24:47 +08:00
|
|
|
import GroupsToolbar from '../../components/toolbar/groups-toolbar';
|
2018-12-10 18:19:03 +08:00
|
|
|
import SharedRepoListView from '../../components/shared-repo-list-view/shared-repo-list-view';
|
2018-12-12 18:24:47 +08:00
|
|
|
import CreateGroupDialog from '../../components/dialog/create-group-dialog';
|
2018-12-29 18:25:18 +08:00
|
|
|
import LibDetail from '../../components/dirent-detail/lib-details';
|
2018-12-08 08:37:18 +08:00
|
|
|
|
2018-12-10 11:52:44 +08:00
|
|
|
import '../../css/groups.css';
|
2018-12-08 08:37:18 +08:00
|
|
|
|
2018-12-10 11:52:44 +08:00
|
|
|
const propTypes = {
|
|
|
|
group: PropTypes.object.isRequired,
|
2019-01-31 17:37:02 +08:00
|
|
|
onItemDetails: PropTypes.func.isRequired,
|
2018-12-08 08:37:18 +08:00
|
|
|
};
|
|
|
|
|
2018-12-10 11:52:44 +08:00
|
|
|
|
|
|
|
class RepoListViewPanel extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
repoList: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
let group = this.props.group;
|
2018-12-10 13:30:44 +08:00
|
|
|
let repoList = group.repos.map(item => {
|
2018-12-18 17:21:01 +08:00
|
|
|
let repo = new Repo(item);
|
2018-12-10 13:30:44 +08:00
|
|
|
return repo;
|
2018-12-10 11:52:44 +08:00
|
|
|
});
|
2018-12-10 13:30:44 +08:00
|
|
|
this.setState({repoList: repoList});
|
2018-12-10 11:52:44 +08:00
|
|
|
}
|
|
|
|
|
2018-12-10 18:37:59 +08:00
|
|
|
onItemUnshare = (repo) => {
|
|
|
|
let group = this.props.group;
|
|
|
|
seafileAPI.unshareRepo(repo.repo_id, {share_type: 'group', group_id: group.id}).then(() => {
|
|
|
|
let repoList = this.state.repoList.filter(item => {
|
|
|
|
return item.repo_id !== repo.repo_id;
|
|
|
|
});
|
|
|
|
this.setState({repoList: repoList});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-16 11:17:17 +08:00
|
|
|
onItemDelete = (repo) => {
|
|
|
|
let group = this.props.group;
|
|
|
|
seafileAPI.deleteGroupOwnedLibrary(group.id, repo.repo_id).then(() => {
|
|
|
|
let repoList = this.state.repoList.filter(item => {
|
|
|
|
return item.repo_id !== repo.repo_id;
|
|
|
|
});
|
|
|
|
this.setState({repoList: repoList});
|
2019-05-28 17:48:44 +08:00
|
|
|
let name = repo.repo_name;
|
|
|
|
var msg = gettext("Successfully deleted {name}.").replace('{name}', name);
|
|
|
|
toaster.success(msg);
|
2018-12-16 11:17:17 +08:00
|
|
|
}).catch(() => {
|
2019-05-28 17:48:44 +08:00
|
|
|
let name = repo.repo_name;
|
|
|
|
var msg = gettext("Failed to delete {name}.").replace('{name}', name);
|
|
|
|
toaster.danger(msg);
|
2018-12-16 11:17:17 +08:00
|
|
|
});
|
|
|
|
}
|
2019-02-11 15:22:42 +08:00
|
|
|
|
|
|
|
onItemRename = (repo, newName) => {
|
|
|
|
let group = this.props.group;
|
|
|
|
seafileAPI.renameGroupOwnedLibrary(group.id, repo.repo_id, newName).then(res => {
|
|
|
|
let repoList = this.state.repoList.map(item => {
|
|
|
|
if (item.repo_id === repo.repo_id) {
|
|
|
|
item.repo_name = newName;
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
this.setState({repoList: repoList});
|
|
|
|
}).catch(() => {
|
|
|
|
// todo
|
|
|
|
});
|
|
|
|
}
|
2018-12-16 11:17:17 +08:00
|
|
|
|
2018-12-10 11:52:44 +08:00
|
|
|
render() {
|
|
|
|
let group = this.props.group;
|
|
|
|
const emptyTip = <p className="group-item-empty-tip">{gettext('No libraries')}</p>;
|
|
|
|
return (
|
2018-12-13 22:11:08 +08:00
|
|
|
<div className="group-list-panel">
|
2018-12-10 11:52:44 +08:00
|
|
|
<h4 className="group-item-heading ellipsis">
|
|
|
|
<a href={`${siteRoot}group/${group.id}/`} title={group.name}>{group.name}</a>
|
|
|
|
</h4>
|
|
|
|
{this.state.repoList.length === 0 ?
|
|
|
|
emptyTip :
|
2018-12-10 18:19:03 +08:00
|
|
|
<SharedRepoListView
|
2018-12-10 11:52:44 +08:00
|
|
|
isShowTableThread={false}
|
|
|
|
isShowRepoOwner={false}
|
|
|
|
currentGroup={this.props.group}
|
|
|
|
repoList={this.state.repoList}
|
2018-12-10 18:37:59 +08:00
|
|
|
onItemUnshare={this.onItemUnshare}
|
2018-12-16 11:17:17 +08:00
|
|
|
onItemDelete={this.onItemDelete}
|
2018-12-29 18:25:18 +08:00
|
|
|
onItemDetails={this.props.onItemDetails}
|
2019-02-11 15:22:42 +08:00
|
|
|
onItemRename={this.onItemRename}
|
2018-12-10 11:52:44 +08:00
|
|
|
/>
|
|
|
|
}
|
2018-12-13 22:11:08 +08:00
|
|
|
</div>
|
2018-12-10 11:52:44 +08:00
|
|
|
);
|
|
|
|
}
|
2019-01-31 17:37:02 +08:00
|
|
|
}
|
2018-12-10 11:52:44 +08:00
|
|
|
|
|
|
|
RepoListViewPanel.propTypes = propTypes;
|
|
|
|
|
|
|
|
class GroupsView extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
|
|
|
errorMsg: '',
|
|
|
|
groupList: [],
|
2018-12-12 18:24:47 +08:00
|
|
|
showAddGroupModal: false,
|
2018-12-29 18:25:18 +08:00
|
|
|
isShowDetails: false,
|
|
|
|
currentRepo: null,
|
2019-01-31 17:37:02 +08:00
|
|
|
};
|
2018-12-10 11:52:44 +08:00
|
|
|
}
|
|
|
|
|
2018-12-12 18:24:47 +08:00
|
|
|
listGroups = () => {
|
2019-01-08 13:55:00 +08:00
|
|
|
seafileAPI.listGroups(true).then((res) => {
|
2018-12-10 11:52:44 +08:00
|
|
|
// `{'with_repos': 1}`: list repos of every group
|
|
|
|
// res: {data: [...], status: 200, statusText: "OK", headers: {…}, config: {…}, …}
|
2018-12-10 13:30:44 +08:00
|
|
|
let groupList = res.data.map(item => {
|
2018-12-10 11:52:44 +08:00
|
|
|
let group = new Group(item);
|
|
|
|
return group;
|
2019-01-31 17:37:02 +08:00
|
|
|
});
|
2018-12-10 11:52:44 +08:00
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
groupList: groupList,
|
|
|
|
});
|
|
|
|
}).catch((error) => {
|
|
|
|
if (error.response) {
|
|
|
|
if (error.response.status == 403) {
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
2019-01-31 17:37:02 +08:00
|
|
|
errorMsg: gettext('Permission denied')
|
2018-12-10 11:52:44 +08:00
|
|
|
});
|
|
|
|
location.href = `${loginUrl}?next=${encodeURIComponent(location.href)}`;
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
2019-01-31 17:37:02 +08:00
|
|
|
errorMsg: gettext('Error')
|
2018-12-10 11:52:44 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
2019-01-31 17:37:02 +08:00
|
|
|
errorMsg: gettext('Please check the network.')
|
2018-12-10 11:52:44 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-12-08 08:37:18 +08:00
|
|
|
|
2018-12-12 18:24:47 +08:00
|
|
|
toggleAddGroupModal = () => {
|
|
|
|
this.setState({
|
|
|
|
showAddGroupModal: !this.state.showAddGroupModal
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-13 17:55:22 +08:00
|
|
|
onCreateGroup = () => {
|
|
|
|
this.setState({
|
|
|
|
showAddGroupModal: false,
|
|
|
|
isLoading: true,
|
|
|
|
groupList: [],
|
|
|
|
});
|
|
|
|
this.listGroups();
|
|
|
|
}
|
|
|
|
|
2018-12-12 18:24:47 +08:00
|
|
|
componentDidMount() {
|
|
|
|
this.listGroups();
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:25:18 +08:00
|
|
|
onItemDetails = (repo) => {
|
|
|
|
this.setState({
|
|
|
|
isShowDetails: true,
|
|
|
|
currentRepo: repo,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
closeDetails = () => {
|
|
|
|
this.setState({isShowDetails: false});
|
|
|
|
}
|
|
|
|
|
2018-12-08 08:37:18 +08:00
|
|
|
render() {
|
|
|
|
return (
|
2018-12-10 11:52:44 +08:00
|
|
|
<Fragment>
|
2018-12-12 18:24:47 +08:00
|
|
|
<GroupsToolbar
|
|
|
|
onShowSidePanel={this.props.onShowSidePanel}
|
|
|
|
onSearchedClick={this.props.onSearchedClick}
|
|
|
|
toggleAddGroupModal={this.toggleAddGroupModal}
|
|
|
|
/>
|
2018-12-29 18:25:18 +08:00
|
|
|
<div className="main-panel-center flex-row">
|
2018-12-10 11:52:44 +08:00
|
|
|
<div className="cur-view-container">
|
|
|
|
<div className="cur-view-path">
|
2019-01-31 17:37:02 +08:00
|
|
|
<h3 className="sf-heading">{gettext('My Groups')}</h3>
|
2018-12-10 11:52:44 +08:00
|
|
|
</div>
|
2018-12-13 16:35:11 +08:00
|
|
|
<div className="cur-view-content cur-view-content-groups">
|
2018-12-10 11:52:44 +08:00
|
|
|
{this.state.isLoading && <Loading />}
|
|
|
|
{(!this.state.isLoading && this.state.errorMsg !== '') && this.state.errorMsg}
|
|
|
|
{/* {(!this.state.isLoading && this.state.groupList.length === 0 ) && emptyTip} //todo */}
|
|
|
|
{!this.state.isLoading && this.state.groupList.map((group, index) => {
|
|
|
|
return (
|
2018-12-29 18:25:18 +08:00
|
|
|
<RepoListViewPanel
|
|
|
|
key={index}
|
|
|
|
group={group}
|
|
|
|
onItemDetails={this.onItemDetails}
|
|
|
|
/>
|
2018-12-10 11:52:44 +08:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-12-29 18:25:18 +08:00
|
|
|
{this.state.isShowDetails && (
|
|
|
|
<div className="cur-view-detail">
|
|
|
|
<LibDetail currentRepo={this.state.currentRepo} closeDetails={this.closeDetails}/>
|
|
|
|
</div>
|
|
|
|
)}
|
2018-12-10 11:52:44 +08:00
|
|
|
</div>
|
2018-12-12 18:24:47 +08:00
|
|
|
{ this.state.showAddGroupModal &&
|
|
|
|
<CreateGroupDialog
|
|
|
|
toggleAddGroupModal={this.toggleAddGroupModal}
|
2018-12-13 17:55:22 +08:00
|
|
|
onCreateGroup={this.onCreateGroup}
|
2018-12-12 18:24:47 +08:00
|
|
|
showAddGroupModal={this.state.showAddGroupModal}
|
|
|
|
/>
|
|
|
|
}
|
2018-12-10 11:52:44 +08:00
|
|
|
</Fragment>
|
2018-12-08 08:37:18 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 18:24:47 +08:00
|
|
|
const GroupsViewPropTypes = {
|
|
|
|
onShowSidePanel: PropTypes.func.isRequired,
|
|
|
|
onSearchedClick: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
GroupsView.propTypes = GroupsViewPropTypes;
|
2018-12-08 08:37:18 +08:00
|
|
|
|
2018-12-10 11:52:44 +08:00
|
|
|
export default GroupsView;
|