2018-12-28 08:34:04 +00:00
|
|
|
import React, { Fragment } from 'react';
|
2018-12-06 03:28:16 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-12-25 08:28:17 +00:00
|
|
|
import { Button } from 'reactstrap';
|
2018-12-06 03:28:16 +00:00
|
|
|
import Select from 'react-select';
|
2019-01-15 02:45:53 +00:00
|
|
|
import { gettext, isPro } from '../../utils/constants';
|
2018-12-06 03:28:16 +00:00
|
|
|
import { seafileAPI } from '../../utils/seafile-api.js';
|
2019-07-16 02:01:09 +00:00
|
|
|
import { Utils } from '../../utils/utils';
|
|
|
|
import toaster from '../toast';
|
2019-01-11 04:41:30 +00:00
|
|
|
import SharePermissionEditor from '../select-editor/share-permission-editor';
|
2018-12-06 03:28:16 +00:00
|
|
|
|
2018-12-17 13:38:55 +00:00
|
|
|
class GroupItem extends React.Component {
|
2018-12-18 02:36:42 +00:00
|
|
|
|
2018-12-17 13:38:55 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isOperationShow: false
|
|
|
|
};
|
|
|
|
}
|
2020-11-02 05:56:35 +00:00
|
|
|
|
2018-12-17 13:38:55 +00:00
|
|
|
onMouseEnter = () => {
|
|
|
|
this.setState({isOperationShow: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseLeave = () => {
|
|
|
|
this.setState({isOperationShow: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteShareItem = () => {
|
|
|
|
let item = this.props.item;
|
|
|
|
this.props.deleteShareItem(item.group_info.id);
|
|
|
|
}
|
|
|
|
|
2018-12-25 08:28:17 +00:00
|
|
|
onChangeUserPermission = (permission) => {
|
|
|
|
let item = this.props.item;
|
|
|
|
this.props.onChangeUserPermission(item, permission);
|
|
|
|
}
|
|
|
|
|
2018-12-17 13:38:55 +00:00
|
|
|
render() {
|
|
|
|
let item = this.props.item;
|
2019-06-20 08:56:55 +00:00
|
|
|
let currentPermission = item.is_admin ? 'admin' : item.permission;
|
2018-12-17 13:38:55 +00:00
|
|
|
return (
|
|
|
|
<tr onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>
|
2018-12-26 08:07:22 +00:00
|
|
|
<td className='name'>{item.group_info.name}</td>
|
2018-12-25 08:28:17 +00:00
|
|
|
<td>
|
2020-11-02 05:56:35 +00:00
|
|
|
<SharePermissionEditor
|
2018-12-25 08:28:17 +00:00
|
|
|
isTextMode={true}
|
2018-12-26 08:07:22 +00:00
|
|
|
isEditIconShow={this.state.isOperationShow}
|
2019-06-20 08:56:55 +00:00
|
|
|
currentPermission={currentPermission}
|
2018-12-25 13:04:03 +00:00
|
|
|
permissions={this.props.permissions}
|
2019-01-14 05:33:36 +00:00
|
|
|
onPermissionChanged={this.onChangeUserPermission}
|
2018-12-25 08:28:17 +00:00
|
|
|
/>
|
|
|
|
</td>
|
2018-12-17 13:38:55 +00:00
|
|
|
<td>
|
|
|
|
<span
|
2018-12-28 03:12:24 +00:00
|
|
|
className={`sf2-icon-x3 action-icon ${this.state.isOperationShow ? '' : 'hide'}`}
|
2020-11-02 05:56:35 +00:00
|
|
|
onClick={this.deleteShareItem}
|
2018-12-17 13:38:55 +00:00
|
|
|
title={gettext('Delete')}
|
|
|
|
>
|
|
|
|
</span>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GroupList extends React.Component {
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let items = this.props.items;
|
|
|
|
return (
|
|
|
|
<tbody>
|
|
|
|
{items.map((item, index) => {
|
|
|
|
return (
|
2020-11-02 05:56:35 +00:00
|
|
|
<GroupItem
|
|
|
|
key={index}
|
|
|
|
item={item}
|
2018-12-25 13:04:03 +00:00
|
|
|
permissions={this.props.permissions}
|
2018-12-25 08:28:17 +00:00
|
|
|
deleteShareItem={this.props.deleteShareItem}
|
|
|
|
onChangeUserPermission={this.props.onChangeUserPermission}
|
|
|
|
/>
|
2018-12-17 13:38:55 +00:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</tbody>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:28:16 +00:00
|
|
|
const propTypes = {
|
2018-12-14 13:39:17 +00:00
|
|
|
isGroupOwnedRepo: PropTypes.bool,
|
2018-12-06 03:28:16 +00:00
|
|
|
itemPath: PropTypes.string.isRequired,
|
2019-02-27 05:53:36 +00:00
|
|
|
itemType: PropTypes.string.isRequired,
|
2019-06-20 09:13:15 +00:00
|
|
|
repoID: PropTypes.string.isRequired,
|
|
|
|
isRepoOwner: PropTypes.bool.isRequired,
|
2018-12-06 03:28:16 +00:00
|
|
|
};
|
|
|
|
|
2019-02-18 08:49:38 +00:00
|
|
|
const NoOptionsMessage = (props) => {
|
|
|
|
return (
|
|
|
|
<div {...props.innerProps} style={{margin: '6px 10px', textAlign: 'center', color: 'hsl(0,0%,50%)'}}>{gettext('Group not found')}</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-12-06 03:28:16 +00:00
|
|
|
class ShareToGroup extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2019-11-27 03:36:50 +00:00
|
|
|
options: [],
|
2018-12-06 03:28:16 +00:00
|
|
|
selectedOption: null,
|
|
|
|
errorMsg: [],
|
|
|
|
permission: 'rw',
|
|
|
|
sharedItems: []
|
|
|
|
};
|
2019-07-19 15:50:22 +00:00
|
|
|
this.permissions = [];
|
2019-06-20 09:13:15 +00:00
|
|
|
let { itemType, isRepoOwner } = props;
|
|
|
|
if (itemType === 'library') {
|
|
|
|
this.permissions = isRepoOwner ? ['rw', 'r', 'admin', 'cloud-edit', 'preview'] : ['rw', 'r', 'cloud-edit', 'preview'];
|
|
|
|
} else if (itemType === 'dir') {
|
2019-02-27 05:53:36 +00:00
|
|
|
this.permissions = ['rw', 'r', 'cloud-edit', 'preview'];
|
|
|
|
}
|
2019-01-15 02:45:53 +00:00
|
|
|
if (this.props.isGroupOwnedRepo || !isPro) {
|
2018-12-25 13:04:03 +00:00
|
|
|
this.permissions = ['rw', 'r'];
|
2018-12-25 08:28:17 +00:00
|
|
|
}
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSelectChange = (option) => {
|
2018-12-17 06:42:49 +00:00
|
|
|
this.setState({selectedOption: option});
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.loadOptions();
|
|
|
|
this.listSharedGroups();
|
|
|
|
}
|
|
|
|
|
|
|
|
loadOptions = () => {
|
|
|
|
seafileAPI.shareableGroups().then((res) => {
|
2019-11-27 03:36:50 +00:00
|
|
|
let options = [];
|
2018-12-06 03:28:16 +00:00
|
|
|
for (let i = 0 ; i < res.data.length; i++) {
|
|
|
|
let obj = {};
|
|
|
|
obj.value = res.data[i].name;
|
|
|
|
obj.id = res.data[i].id;
|
|
|
|
obj.label = res.data[i].name;
|
2019-11-27 03:36:50 +00:00
|
|
|
options.push(obj);
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
2019-11-27 03:36:50 +00:00
|
|
|
this.setState({options: options});
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
listSharedGroups = () => {
|
|
|
|
let path = this.props.itemPath;
|
2020-11-02 05:56:35 +00:00
|
|
|
let repoID = this.props.repoID;
|
2018-12-06 03:28:16 +00:00
|
|
|
seafileAPI.listSharedItems(repoID, path, 'group').then((res) => {
|
|
|
|
if(res.data.length !== 0) {
|
|
|
|
this.setState({
|
|
|
|
sharedItems: res.data
|
|
|
|
});
|
|
|
|
}
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-25 08:28:17 +00:00
|
|
|
setPermission = (permission) => {
|
|
|
|
this.setState({permission: permission});
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shareToGroup = () => {
|
|
|
|
let groups = [];
|
|
|
|
let path = this.props.itemPath;
|
2020-11-02 05:56:35 +00:00
|
|
|
let repoID = this.props.repoID;
|
2018-12-14 13:39:17 +00:00
|
|
|
let isGroupOwnedRepo = this.props.isGroupOwnedRepo;
|
2018-12-17 13:38:55 +00:00
|
|
|
if (this.state.selectedOption && this.state.selectedOption.length > 0 ) {
|
2018-12-06 03:28:16 +00:00
|
|
|
for (let i = 0; i < this.state.selectedOption.length; i ++) {
|
|
|
|
groups[i] = this.state.selectedOption[i].id;
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 13:39:17 +00:00
|
|
|
if (isGroupOwnedRepo) {
|
2019-10-30 06:38:50 +00:00
|
|
|
seafileAPI.shareGroupOwnedRepoToGroup(repoID, this.state.permission, groups, path).then(res => {
|
2018-12-25 08:28:17 +00:00
|
|
|
let errorMsg = [];
|
2018-12-14 13:39:17 +00:00
|
|
|
if (res.data.failed.length > 0) {
|
|
|
|
for (let i = 0 ; i < res.data.failed.length ; i++) {
|
|
|
|
errorMsg[i] = res.data.failed[i];
|
|
|
|
}
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
2018-12-14 13:39:17 +00:00
|
|
|
|
|
|
|
// todo modify api
|
|
|
|
let items = res.data.success.map(item => {
|
|
|
|
let sharedItem = {
|
|
|
|
'group_info': { 'id': item.group_id, 'name': item.group_name},
|
|
|
|
'permission': item.permission,
|
|
|
|
'share_type': 'group',
|
|
|
|
};
|
|
|
|
return sharedItem;
|
|
|
|
});
|
2020-11-02 05:56:35 +00:00
|
|
|
|
2018-12-06 03:28:16 +00:00
|
|
|
this.setState({
|
2018-12-25 08:28:17 +00:00
|
|
|
errorMsg: errorMsg,
|
2018-12-17 06:42:49 +00:00
|
|
|
sharedItems: this.state.sharedItems.concat(items),
|
|
|
|
selectedOption: null,
|
2018-12-25 08:28:17 +00:00
|
|
|
permission: 'rw',
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
2018-12-14 13:39:17 +00:00
|
|
|
} else {
|
|
|
|
seafileAPI.shareFolder(repoID, path, 'group', this.state.permission, groups).then(res => {
|
2018-12-25 08:28:17 +00:00
|
|
|
let errorMsg = [];
|
2018-12-14 13:39:17 +00:00
|
|
|
if (res.data.failed.length > 0) {
|
|
|
|
for (let i = 0 ; i < res.data.failed.length ; i++) {
|
|
|
|
errorMsg[i] = res.data.failed[i];
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 05:56:35 +00:00
|
|
|
|
2018-12-14 13:39:17 +00:00
|
|
|
this.setState({
|
2018-12-25 08:28:17 +00:00
|
|
|
errorMsg: errorMsg,
|
2018-12-17 06:42:49 +00:00
|
|
|
sharedItems: this.state.sharedItems.concat(res.data.success),
|
|
|
|
selectedOption: null,
|
2018-12-25 08:28:17 +00:00
|
|
|
permission: 'rw'
|
2018-12-14 13:39:17 +00:00
|
|
|
});
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2018-12-14 13:39:17 +00:00
|
|
|
});
|
|
|
|
}
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 13:38:55 +00:00
|
|
|
deleteShareItem = (groupID) => {
|
2018-12-06 03:28:16 +00:00
|
|
|
let path = this.props.itemPath;
|
2020-11-02 05:56:35 +00:00
|
|
|
let repoID = this.props.repoID;
|
2018-12-14 13:39:17 +00:00
|
|
|
if (this.props.isGroupOwnedRepo) {
|
2019-10-30 06:38:50 +00:00
|
|
|
seafileAPI.deleteGroupOwnedRepoSharedGroupItem(repoID, groupID, path).then(() => {
|
2018-12-14 13:39:17 +00:00
|
|
|
this.setState({
|
2020-11-02 05:56:35 +00:00
|
|
|
sharedItems: this.state.sharedItems.filter(item => { return item.group_info.id !== groupID; })
|
2018-12-14 13:39:17 +00:00
|
|
|
});
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
2018-12-14 13:39:17 +00:00
|
|
|
} else {
|
|
|
|
seafileAPI.deleteShareToGroupItem(repoID, path, 'group', groupID).then(() => {
|
|
|
|
this.setState({
|
2020-11-02 05:56:35 +00:00
|
|
|
sharedItems: this.state.sharedItems.filter(item => { return item.group_info.id !== groupID; })
|
2018-12-14 13:39:17 +00:00
|
|
|
});
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2018-12-14 13:39:17 +00:00
|
|
|
});
|
|
|
|
}
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
2018-12-25 08:28:17 +00:00
|
|
|
onChangeUserPermission = (item, permission) => {
|
|
|
|
let path = this.props.itemPath;
|
|
|
|
let repoID = this.props.repoID;
|
|
|
|
let groupID = item.group_info.id;
|
|
|
|
if (this.props.isGroupOwnedRepo) {
|
2019-10-30 06:38:50 +00:00
|
|
|
seafileAPI.modifyGroupOwnedRepoGroupSharedPermission(repoID, permission, groupID, path).then(() => {
|
2018-12-25 08:28:17 +00:00
|
|
|
this.updateSharedItems(item, permission);
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2019-01-31 09:37:02 +00:00
|
|
|
});
|
2018-12-25 08:28:17 +00:00
|
|
|
} else {
|
2018-12-25 10:31:34 +00:00
|
|
|
seafileAPI.updateShareToGroupItemPermission(repoID, path, 'group', groupID, permission).then(() => {
|
2018-12-25 08:28:17 +00:00
|
|
|
this.updateSharedItems(item, permission);
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2018-12-25 08:28:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 05:56:35 +00:00
|
|
|
|
2018-12-25 08:28:17 +00:00
|
|
|
updateSharedItems = (item, permission) => {
|
|
|
|
let groupID = item.group_info.id;
|
|
|
|
let sharedItems = this.state.sharedItems.map(sharedItem => {
|
|
|
|
let sharedItemGroupID = sharedItem.group_info.id;
|
|
|
|
if (groupID === sharedItemGroupID) {
|
|
|
|
sharedItem.permission = permission;
|
2020-03-04 04:06:07 +00:00
|
|
|
sharedItem.is_admin = permission === 'admin' ? true : false;
|
2018-12-25 08:28:17 +00:00
|
|
|
}
|
|
|
|
return sharedItem;
|
|
|
|
});
|
|
|
|
this.setState({sharedItems: sharedItems});
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:28:16 +00:00
|
|
|
render() {
|
2020-04-21 11:56:28 +00:00
|
|
|
const thead = (
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th width="47%">{gettext('Group')}</th>
|
|
|
|
<th width="35%">{gettext('Permission')}</th>
|
|
|
|
<th width="18%"></th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
);
|
2018-12-06 03:28:16 +00:00
|
|
|
return (
|
2018-12-28 08:34:04 +00:00
|
|
|
<Fragment>
|
2020-04-04 06:32:11 +00:00
|
|
|
<table className="w-xs-200">
|
2020-04-21 11:56:28 +00:00
|
|
|
{thead}
|
2018-12-28 08:34:04 +00:00
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<Select
|
|
|
|
isMulti
|
|
|
|
onChange={this.handleSelectChange}
|
2019-11-27 03:36:50 +00:00
|
|
|
options={this.state.options}
|
2019-04-28 02:21:30 +00:00
|
|
|
placeholder={gettext('Select groups...')}
|
2019-01-07 07:03:35 +00:00
|
|
|
maxMenuHeight={200}
|
2018-12-28 08:34:04 +00:00
|
|
|
inputId={'react-select-2-input'}
|
|
|
|
value={this.state.selectedOption}
|
2019-02-18 08:49:38 +00:00
|
|
|
components={{ NoOptionsMessage }}
|
2018-12-28 08:34:04 +00:00
|
|
|
/>
|
|
|
|
</td>
|
|
|
|
<td>
|
2020-11-02 05:56:35 +00:00
|
|
|
<SharePermissionEditor
|
2018-12-28 08:34:04 +00:00
|
|
|
isTextMode={false}
|
|
|
|
isEditIconShow={false}
|
|
|
|
currentPermission={this.state.permission}
|
|
|
|
permissions={this.permissions}
|
2019-01-14 05:33:36 +00:00
|
|
|
onPermissionChanged={this.setPermission}
|
2018-12-28 08:34:04 +00:00
|
|
|
/>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<Button onClick={this.shareToGroup}>{gettext('Submit')}</Button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
2020-11-02 05:56:35 +00:00
|
|
|
{this.state.errorMsg.length > 0 &&
|
2018-12-28 08:34:04 +00:00
|
|
|
this.state.errorMsg.map((item, index) => {
|
|
|
|
let errMessage = item.group_name + ': ' + item.error_msg;
|
|
|
|
return (
|
|
|
|
<tr key={index}>
|
|
|
|
<td colSpan={3}><p className="error">{errMessage}</p></td>
|
|
|
|
</tr>
|
|
|
|
);
|
2020-11-02 05:56:35 +00:00
|
|
|
})
|
2018-12-28 08:34:04 +00:00
|
|
|
}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
<div className="share-list-container">
|
2020-04-04 06:32:11 +00:00
|
|
|
<table className="table-thead-hidden w-xs-200">
|
2020-04-21 11:56:28 +00:00
|
|
|
{thead}
|
2020-11-02 05:56:35 +00:00
|
|
|
<GroupList
|
2018-12-28 08:34:04 +00:00
|
|
|
items={this.state.sharedItems}
|
|
|
|
permissions={this.permissions}
|
2020-11-02 05:56:35 +00:00
|
|
|
deleteShareItem={this.deleteShareItem}
|
2018-12-28 08:34:04 +00:00
|
|
|
onChangeUserPermission={this.onChangeUserPermission}
|
|
|
|
/>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
2018-12-06 03:28:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ShareToGroup.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default ShareToGroup;
|