1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-17 08:41:40 +00:00
seahub/frontend/src/pages/groups/group-view.js

299 lines
10 KiB
JavaScript
Raw Normal View History

2018-12-10 05:44:11 +00:00
import React,{ Fragment } from 'react';
2018-12-08 00:37:18 +00:00
import PropTypes from 'prop-types';
2018-12-10 10:12:46 +00:00
import { gettext, siteRoot, username, loginUrl } from '../../utils/constants';
2018-12-11 10:05:57 +00:00
import { Link } from '@reach/router';
2018-12-08 00:37:18 +00:00
import { seafileAPI } from '../../utils/seafile-api';
2018-12-10 05:44:11 +00:00
import Loading from '../../components/loading';
2018-12-10 09:59:26 +00:00
import ModalPortal from '../../components/modal-portal';
2018-12-08 00:37:18 +00:00
import Group from '../../models/group';
import RepoInfo from '../../models/repoInfo';
2018-12-10 05:44:11 +00:00
import CommonToolbar from '../../components/toolbar/common-toolbar';
2018-12-10 09:59:26 +00:00
import CreateRepoDialog from '../../components/dialog/create-repo-dialog';
import CreateDepartmentRepoDialog from '../../components/dialog/create-department-repo-dialog';
2018-12-10 10:19:03 +00:00
import SharedRepoListView from '../../components/shared-repo-list-view/shared-repo-list-view';
2018-12-08 00:37:18 +00:00
const propTypes = {
2018-12-08 08:35:00 +00:00
onShowSidePanel: PropTypes.func.isRequired,
onSearchedClick: PropTypes.func.isRequired,
2018-12-08 00:37:18 +00:00
};
2018-12-10 09:59:26 +00:00
const DEPARETMENT_GROUP_ONWER_NAME = 'system admin';
2018-12-08 00:37:18 +00:00
class GroupView extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
errMessage: '',
2018-12-08 08:35:00 +00:00
emptyTip: null,
2018-12-08 00:37:18 +00:00
currentGroup: null,
2018-12-08 08:35:00 +00:00
isStaff: false,
2018-12-08 00:37:18 +00:00
repoList: [],
2018-12-10 09:59:26 +00:00
libraryType: 'group',
isCreateRepoDialogShow: false,
isDepartmentGroup: false,
2018-12-08 00:37:18 +00:00
}
}
componentDidMount() {
let groupID = this.props.groupID;
this.loadGroup(groupID);
}
componentWillReceiveProps(nextProps) {
if (nextProps.groupID !== this.props.groupID) {
this.loadGroup(nextProps.groupID);
}
}
loadGroup = (groupID) => {
seafileAPI.getGroup(groupID).then((res) => {
let currentGroup = new Group(res.data);
2018-12-08 08:35:00 +00:00
let emptyTip = this.getEmptyTip(currentGroup);
let isStaff = currentGroup.admins.indexOf(username) > -1; //for item operations
2018-12-10 09:59:26 +00:00
let isDepartmentGroup = currentGroup.owner === DEPARETMENT_GROUP_ONWER_NAME;
2018-12-08 08:35:00 +00:00
this.setState({
emptyTip: emptyTip,
currentGroup: currentGroup,
isStaff: isStaff,
2018-12-10 09:59:26 +00:00
isDepartmentGroup: isDepartmentGroup,
2018-12-08 08:35:00 +00:00
});
2018-12-08 00:37:18 +00:00
this.loadRepos(groupID);
}).catch((error) => {
if (error.response) {
if (error.response.status == 403) {
this.setState({
isLoading: false,
2018-12-10 05:44:11 +00:00
errMessage: gettext("Permission denied")
2018-12-08 00:37:18 +00:00
});
location.href = `${loginUrl}?next=${encodeURIComponent(location.href)}`;
} else {
this.setState({
isLoading: false,
2018-12-10 05:44:11 +00:00
errMessage: gettext("Error")
2018-12-08 00:37:18 +00:00
});
}
} else {
this.setState({
isLoading: false,
2018-12-10 05:44:11 +00:00
errMessage: gettext("Please check the network.")
2018-12-08 00:37:18 +00:00
});
}
});
}
loadRepos = (groupID) => {
this.setState({isLoading: true});
seafileAPI.listGroupRepos(groupID).then((res) => {
let repoList = res.data.map(item => {
let repoInfo = new RepoInfo(item);
return repoInfo;
});
this.setState({
isLoading: false,
repoList: repoList
});
}).catch((error) => {
if (error.response) {
if (error.response.status == 403) {
this.setState({
isLoading: false,
2018-12-10 05:44:11 +00:00
errMessage: gettext("Permission denied")
2018-12-08 00:37:18 +00:00
});
location.href = `${loginUrl}?next=${encodeURIComponent(location.href)}`;
} else {
this.setState({
isLoading: false,
2018-12-10 05:44:11 +00:00
errMessage: gettext("Error")
2018-12-08 00:37:18 +00:00
});
}
} else {
this.setState({
isLoading: false,
2018-12-10 05:44:11 +00:00
errMessage: gettext("Please check the network.")
2018-12-08 00:37:18 +00:00
});
}
});
}
2018-12-08 08:35:00 +00:00
getEmptyTip = (currentGroup) => {
2018-12-08 00:37:18 +00:00
let emptyTip = null;
if (currentGroup) {
if (currentGroup.parent_group_id === 0) {
emptyTip = (
<div className="empty-tip">
<h2>{gettext('No library is shared to this group')}</h2>
<p>{gettext('You can share libraries by clicking the "New Library" button above or the "Share" icon on your libraries list.')}</p>
<p>{gettext('Libraries shared as writable can be downloaded and synced by other group members. Read only libraries can only be downloaded, updates by others will not be uploaded.')}</p>
</div>
);
} else {
2018-12-08 08:35:00 +00:00
if (currentGroup.admins.indexOf(username) == -1) { // is a member of this group
2018-12-08 00:37:18 +00:00
emptyTip = (
<div className="empty-tip">
<h2>{gettext('No libraries')}</h2>
</div>
);
} else {
emptyTip = (
<div className="empty-tip">
<h2>{gettext('No libraries')}</h2>
<p>{gettext('You can create libraries by clicking the "New Library" button above.')}</p>
</div>
);
}
}
}
return emptyTip;
}
2018-12-10 09:59:26 +00:00
onCreateRepoToggle = () => {
this.setState({isCreateRepoDialogShow: !this.state.isCreateRepoDialogShow});
}
2018-12-16 03:17:17 +00:00
onCreateRepo = (repo, groupOwnerType) => {
2018-12-10 05:44:11 +00:00
let groupId = this.props.groupID;
2018-12-16 03:17:17 +00:00
if (groupOwnerType && groupOwnerType === 'department') {
seafileAPI.createGroupOwnedLibrary(groupId, repo).then(res => { //need modify endpoint api
let object = {
repo_id: res.data.id,
repo_name: res.data.name,
2018-12-17 07:12:10 +00:00
owner_name: res.data.group_name,
2018-12-16 03:17:17 +00:00
owner_email: res.data.owner,
permission: res.data.permission,
mtime: res.data.mtime,
size: res.data.size,
encrypted: res.data.encrypted,
};
let repo = new RepoInfo(object);
let repoList = this.addRepoItem(repo);
this.setState({repoList: repoList});
}).then(() => {
//todo
});
} else {
seafileAPI.createGroupRepo(groupId, repo).then(res => {
let repo = new RepoInfo(res.data);
let repoList = this.addRepoItem(repo);
this.setState({repoList: repoList});
}).catch(() => {
//todo
});
}
this.onCreateRepoToggle();
}
onItemDelete = (repo) => {
let groupID = this.props.groupID;
seafileAPI.deleteGroupOwnedLibrary(groupID, repo.repo_id).then(() => {
let repoList = this.state.repoList.filter(item => {
return item.repo_id !== repo.repo_id;
});
2018-12-10 05:44:11 +00:00
this.setState({repoList: repoList});
}).catch(() => {
2018-12-16 03:17:17 +00:00
// todo;
2018-12-10 05:44:11 +00:00
});
}
addRepoItem = (repo) => {
let newRepoList = this.state.repoList.map(item => {return item;});
newRepoList.push(repo);
return newRepoList;
}
2018-12-10 10:37:59 +00:00
onItemUnshare = (repo) => {
2018-12-10 09:59:26 +00:00
let group = this.state.currentGroup;
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-13 06:40:09 +00:00
onTabNavClick = (tabName) => {
this.props.onTabNavClick(tabName);
}
2018-12-08 00:37:18 +00:00
render() {
2018-12-10 10:12:46 +00:00
let { errMessage, emptyTip, currentGroup } = this.state;
let isShowSettingIcon = !(currentGroup && currentGroup.parent_group_id !== 0 && currentGroup.admins.indexOf(username) === -1);
2018-12-08 00:37:18 +00:00
return (
2018-12-10 05:44:11 +00:00
<Fragment>
<div className="main-panel-north">
2018-12-10 09:59:26 +00:00
<div className="cur-view-toolbar border-left-show">
<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">
<button className="btn btn-secondary operation-item" title={gettext('New Library')} onClick={this.onCreateRepoToggle}>
<i className="fas fa-plus-square op-icon"></i>
{gettext('New Library')}
</button>
</div>
</div>
2018-12-10 05:44:11 +00:00
<CommonToolbar onSearchedClick={this.props.onSearchedClick} />
</div>
<div className="main-panel-center">
<div className="cur-view-container">
<div className="cur-view-path">
2018-12-10 10:12:46 +00:00
{currentGroup && (
<Fragment>
<div className="path-container">
2018-12-13 06:40:09 +00:00
<Link to={`${siteRoot}groups/`} onClick={() => this.onTabNavClick('groups')}>{gettext("Groups")}</Link>
2018-12-10 10:12:46 +00:00
<span className="path-split">/</span>
<span>{currentGroup.name}</span>
{currentGroup.parent_group_id !== 0 && (
2018-12-11 10:34:31 +00:00
<span className="department-group-icon fas fa-building" title={gettext("This is a special group representing a department.")}></span>
2018-12-10 10:12:46 +00:00
)}
</div>
<div className="path-tool">
{isShowSettingIcon && (
<a href="#" className="sf2-icon-cog1 op-icon group-top-op-icon" title={gettext("Settings")} aria-label={gettext("Settings")}></a>
)}
<a href="#" className="sf2-icon-user2 op-icon group-top-op-icon" title={gettext("Members")} aria-label={gettext("Members")}></a>
</div>
</Fragment>
)}
2018-12-10 05:44:11 +00:00
</div>
<div className="cur-view-content">
{this.state.isLoading && <Loading />}
{(!this.state.isLoading && errMessage) && errMessage}
{(!this.state.isLoading && this.state.repoList.length === 0) && emptyTip}
{(!this.state.isLoading && this.state.repoList.length > 0) &&
2018-12-10 10:19:03 +00:00
<SharedRepoListView
2018-12-10 05:44:11 +00:00
repoList={this.state.repoList}
currentGroup={this.state.currentGroup}
2018-12-10 10:37:59 +00:00
onItemUnshare={this.onItemUnshare}
2018-12-16 03:17:17 +00:00
onItemDelete={this.onItemDelete}
2018-12-10 05:44:11 +00:00
/>
}
</div>
</div>
</div>
2018-12-10 09:59:26 +00:00
{this.state.isCreateRepoDialogShow && !this.state.isDepartmentGroup && (
<ModalPortal>
<CreateRepoDialog
hasPermission={this.hasPermission}
libraryType={this.state.libraryType}
onCreateToggle={this.onCreateRepoToggle}
onCreateRepo={this.onCreateRepo}
/>
</ModalPortal>
)}
{this.state.isCreateRepoDialogShow && this.state.isDepartmentGroup &&
<CreateDepartmentRepoDialog
isAdmin={this.state.isAdmin}
onCreateToggle={this.onCreateRepoToggle}
onCreateRepo={this.onCreateRepo}
/>
}
2018-12-10 05:44:11 +00:00
</Fragment>
2018-12-08 00:37:18 +00:00
);
}
}
GroupView.propTypes = propTypes;
export default GroupView;