import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { Button, Input, InputGroup, InputGroupAddon } from 'reactstrap'; import Select from 'react-select'; import makeAnimated from 'react-select/lib/animated'; import { gettext, isPro, siteRoot } from '../../utils/constants'; import { seafileAPI } from '../../utils/seafile-api.js'; import { Utils } from '../../utils/utils.js'; import SharePermissionEditor from '../select-editor/share-permission-editor'; import FileChooser from '../file-chooser/file-chooser'; class GroupItem extends React.Component { constructor(props) { super(props); this.state = { isOperationShow: false }; } onMouseEnter = () => { this.setState({isOperationShow: true}); } onMouseLeave = () => { this.setState({isOperationShow: false}); } deleteGroupPermissionItem = () => { let item = this.props.item; this.props.deleteGroupPermissionItem(item); } onChangeGroupPermission = (permission) => { let item = this.props.item; this.props.onChangeGroupPermission(item, permission); } render() { let item = this.props.item; return ( {item.group_name} {this.props.showPath && {item.folder_name} } ); } } const propTypes = { repoID: PropTypes.string.isRequired, isDepartmentRepo: PropTypes.bool }; const NoOptionsMessage = (props) => { return (
{gettext('Group not found')}
); }; class LibSubFolderSerGroupPermissionDialog extends React.Component { constructor(props) { super(props); this.state = { selectedOption: null, errorMsg: [], permission: 'rw', groupPermissionItems: [], folderPath: '', showFileChooser: false }; this.options = []; if (!isPro) { this.permissions = ['r', 'rw']; } else { this.permissions = ['r', 'rw', 'cloud-edit', 'preview']; } } handleSelectChange = (option) => { this.setState({selectedOption: option}); } componentDidMount() { this.loadOptions(); this.listGroupPermissionItems(); } loadOptions = () => { seafileAPI.shareableGroups().then((res) => { this.options = res.data.map((item, index) => { return { id: item.id, label: item.name, value: item.name }; }); }); } listGroupPermissionItems = () => { const { isDepartmentRepo, repoID, folderPath } = this.props; const request = isDepartmentRepo ? seafileAPI.listDepartmentRepoGroupFolderPerm(repoID, folderPath) : seafileAPI.listGroupFolderPerm(repoID, folderPath); request.then((res) => { if (res.data.length !== 0) { this.setState({ groupPermissionItems: res.data }); } }); } setPermission = (permission) => { this.setState({permission: permission}); } addGroupFolderPerm = () => { const { selectedOption } = this.state; const folderPath = this.props.folderPath || this.state.folderPath; if (!selectedOption || !folderPath) { return false; } const request = this.props.isDepartmentRepo ? seafileAPI.addDepartmentRepoGroupFolderPerm(this.props.repoID, this.state.permission, folderPath, selectedOption.id) : seafileAPI.addGroupFolderPerm(this.props.repoID, this.state.permission, folderPath, selectedOption.id); request.then(res => { let errorMsg = []; if (res.data.failed.length > 0) { for (let i = 0; i < res.data.failed.length; i++) { errorMsg[i] = res.data.failed[i]; } } this.setState({ errorMsg: errorMsg, groupPermissionItems: this.state.groupPermissionItems.concat(res.data.success), selectedOption: null, permission: 'rw', folderPath: '' }); }).catch((error) => { let errorMsg = ''; if (error.response) { if (error.response.data && error.response.data['error_msg']) { errorMsg = error.response.data['error_msg']; } else { errorMsg = gettext('Error'); } } else { errorMsg = gettext('Please check the network.'); } this.setState({ errorMsg: [errorMsg] }); }); } deleteGroupPermissionItem = (item) => { const request = this.props.isDepartmentRepo ? seafileAPI.deleteDepartmentRepoGroupFolderPerm(item.repo_id, item.permission, item.folder_path, item.group_id) : seafileAPI.deleteGroupFolderPerm(item.repo_id, item.permission, item.folder_path, item.group_id); request.then(() => { this.setState({ groupPermissionItems: this.state.groupPermissionItems.filter(deletedItem => { return deletedItem != item; }) }); }); } onChangeGroupPermission = (item, permission) => { const request = this.props.isDepartmentRepo ? seafileAPI.updateDepartmentRepoGroupFolderPerm(item.repo_id, permission, item.folder_path, item.group_id) : seafileAPI.updateGroupFolderPerm(item.repo_id, permission, item.folder_path, item.group_id); request.then(() => { this.updateGroupPermission(item, permission); }); } updateGroupPermission = (item, permission) => { let groupID = item.group_id; let groupPermissionItems = this.state.groupPermissionItems.map(sharedItem => { let sharedItemGroupID = sharedItem.group_id; if (groupID === sharedItemGroupID && item.folder_path === sharedItem.folder_path) { sharedItem.permission = permission; } return sharedItem; }); this.setState({groupPermissionItems: groupPermissionItems}); } onSetSubFolder = (e) => { this.setState({ folderPath: e.target.value }); } toggleFileChooser = () => { this.setState({ showFileChooser: !this.state.showFileChooser, folderPath: '' }); } toggleSubFolder = (repo, path, item) => { this.setState({ folderPath: path, }); } handleSubmit = () => { this.setState({ showFileChooser: !this.state.showFileChooser }); } onRepoItemClick = () => { this.setState({ folderPath: '/' }); } render() { let showPath = this.props.folderPath ? false : true; if (this.state.showFileChooser) { return (
); } const thead = ( {gettext('Group')} {showPath && {gettext('Folder')} } {gettext('Permission')} ); return ( {thead} {showPath && } {this.state.errorMsg.length > 0 && this.state.errorMsg.map((item, index) => { let errMessage = item.group_id + ': ' + item.error_msg; return ( ); }) }

{errMessage}

{thead} {this.state.groupPermissionItems.map((item, index) => { return ( ); })}
); } } LibSubFolderSerGroupPermissionDialog.propTypes = propTypes; export default LibSubFolderSerGroupPermissionDialog;