2018-12-06 03:28:16 +00:00
|
|
|
import React from 'react';
|
|
|
|
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';
|
|
|
|
import makeAnimated from 'react-select/lib/animated';
|
|
|
|
import { gettext } from '../../utils/constants';
|
|
|
|
import { seafileAPI } from '../../utils/seafile-api.js';
|
2018-12-25 08:28:17 +00:00
|
|
|
import PermissionEditor from '../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
|
|
|
|
};
|
|
|
|
}
|
2018-12-18 02:36:42 +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;
|
|
|
|
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>
|
|
|
|
<PermissionEditor
|
|
|
|
isTextMode={true}
|
2018-12-26 08:07:22 +00:00
|
|
|
isEditIconShow={this.state.isOperationShow}
|
2018-12-25 12:45:40 +00:00
|
|
|
currentPermission={item.permission}
|
2018-12-25 13:04:03 +00:00
|
|
|
permissions={this.props.permissions}
|
2018-12-25 08:28:17 +00:00
|
|
|
onPermissionChangedHandler={this.onChangeUserPermission}
|
|
|
|
/>
|
|
|
|
</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'}`}
|
2018-12-17 13:38:55 +00:00
|
|
|
onClick={this.deleteShareItem}
|
|
|
|
title={gettext('Delete')}
|
|
|
|
>
|
|
|
|
</span>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GroupList extends React.Component {
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let items = this.props.items;
|
|
|
|
return (
|
|
|
|
<tbody>
|
|
|
|
{items.map((item, index) => {
|
|
|
|
return (
|
2018-12-25 08:28:17 +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,
|
|
|
|
repoID: PropTypes.string.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
class ShareToGroup extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
selectedOption: null,
|
|
|
|
errorMsg: [],
|
|
|
|
permission: 'rw',
|
|
|
|
sharedItems: []
|
|
|
|
};
|
|
|
|
this.options = [];
|
2018-12-25 13:04:03 +00:00
|
|
|
this.permissions = ['rw', 'r', 'cloud-edit', 'preview'];
|
2018-12-25 08:28:17 +00:00
|
|
|
if (this.props.isGroupOwnedRepo) {
|
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) => {
|
|
|
|
this.options = [];
|
|
|
|
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;
|
|
|
|
this.options.push(obj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
listSharedGroups = () => {
|
|
|
|
let path = this.props.itemPath;
|
|
|
|
let repoID = this.props.repoID;
|
|
|
|
seafileAPI.listSharedItems(repoID, path, 'group').then((res) => {
|
|
|
|
if(res.data.length !== 0) {
|
|
|
|
this.setState({
|
|
|
|
sharedItems: res.data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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) {
|
|
|
|
seafileAPI.shareGroupOwnedRepoToGroup(repoID, 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];
|
|
|
|
}
|
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;
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
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;
|
|
|
|
let repoID = this.props.repoID;
|
2018-12-14 13:39:17 +00:00
|
|
|
if (this.props.isGroupOwnedRepo) {
|
2018-12-14 13:52:54 +00:00
|
|
|
seafileAPI.deleteGroupOwnedRepoSharedGroupItem(repoID, groupID).then(() => {
|
2018-12-14 13:39:17 +00:00
|
|
|
this.setState({
|
|
|
|
sharedItems: this.state.sharedItems.filter(item => { return item.group_info.id !== groupID; })
|
|
|
|
});
|
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({
|
|
|
|
sharedItems: this.state.sharedItems.filter(item => { return item.group_info.id !== groupID; })
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
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) {
|
|
|
|
seafileAPI.modifyGroupOwnedRepoGroupSharedPermission(repoID, permission, groupID).then(() => {
|
|
|
|
this.updateSharedItems(item, permission);
|
|
|
|
})
|
|
|
|
} 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return sharedItem;
|
|
|
|
});
|
|
|
|
this.setState({sharedItems: sharedItems});
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:28:16 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
2018-12-25 10:31:34 +00:00
|
|
|
<th style={{'width': '40%'}}>{gettext('Group')}</th>
|
|
|
|
<th style={{'width': '40%'}}>{gettext('Permission')}</th>
|
2018-12-06 03:28:16 +00:00
|
|
|
<th></th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
<Select
|
|
|
|
isMulti
|
|
|
|
onChange={this.handleSelectChange}
|
|
|
|
options={this.options}
|
2018-12-25 03:59:15 +00:00
|
|
|
placeholder={gettext('Select a group')}
|
2018-12-06 03:28:16 +00:00
|
|
|
components={makeAnimated()}
|
|
|
|
inputId={'react-select-2-input'}
|
2018-12-17 06:42:49 +00:00
|
|
|
value={this.state.selectedOption}
|
2018-12-06 03:28:16 +00:00
|
|
|
/>
|
|
|
|
</td>
|
|
|
|
<td>
|
2018-12-25 08:28:17 +00:00
|
|
|
<PermissionEditor
|
|
|
|
isTextMode={false}
|
2018-12-27 02:24:34 +00:00
|
|
|
isEditIconShow={false}
|
2018-12-25 12:45:40 +00:00
|
|
|
currentPermission={this.state.permission}
|
2018-12-25 13:04:03 +00:00
|
|
|
permissions={this.permissions}
|
2018-12-25 08:28:17 +00:00
|
|
|
onPermissionChangedHandler={this.setPermission}
|
|
|
|
/>
|
2018-12-06 03:28:16 +00:00
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<Button onClick={this.shareToGroup}>{gettext('Submit')}</Button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
2018-12-25 03:59:15 +00:00
|
|
|
{this.state.errorMsg.length > 0 &&
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
2018-12-06 03:28:16 +00:00
|
|
|
</thead>
|
2018-12-25 08:28:17 +00:00
|
|
|
<GroupList
|
|
|
|
items={this.state.sharedItems}
|
2018-12-25 13:04:03 +00:00
|
|
|
permissions={this.permissions}
|
2018-12-25 08:28:17 +00:00
|
|
|
deleteShareItem={this.deleteShareItem}
|
|
|
|
onChangeUserPermission={this.onChangeUserPermission}
|
|
|
|
/>
|
2018-12-06 03:28:16 +00:00
|
|
|
</table>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ShareToGroup.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default ShareToGroup;
|