import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { Button, Modal, ModalBody, ModalFooter, Nav, NavItem, NavLink, TabContent, TabPane, Label } from 'reactstrap'; import SeahubModalHeader from '@/components/common/seahub-modal-header'; import makeAnimated from 'react-select/animated'; import { seafileAPI } from '../../utils/seafile-api'; import { systemAdminAPI } from '../../utils/system-admin-api'; import { orgAdminAPI } from '../../utils/org-admin-api'; import { gettext, isPro, orgID } from '../../utils/constants'; import { Utils } from '../../utils/utils'; import toaster from '../toast'; import UserSelect from '../user-select'; import { SeahubSelect } from '../common/select'; import Switch from '../switch'; import '../../css/transfer-dialog.css'; const propTypes = { itemName: PropTypes.string.isRequired, toggleDialog: PropTypes.func.isRequired, onTransferRepo: PropTypes.func.isRequired, canTransferToDept: PropTypes.bool, isOrgAdmin: PropTypes.bool, isSysAdmin: PropTypes.bool, isDepAdminTransfer: PropTypes.bool, }; const TRANS_USER = 'transUser'; const TRANS_DEPART = 'transDepart'; class TransferDialog extends React.Component { constructor(props) { super(props); this.state = { options: [], selectedOption: null, errorMsg: [], transferToUser: true, transferToGroup: false, reshare: false, activeTab: !this.props.isDepAdminTransfer ? TRANS_USER : TRANS_DEPART }; this.userSelect = React.createRef(); } handleSelectChange = (option) => { this.setState({ selectedOption: option }); }; submit = () => { const { activeTab, reshare, selectedOption } = this.state; const email = activeTab === TRANS_DEPART ? selectedOption.email : selectedOption[0].email; this.props.onTransferRepo(email, reshare); }; componentDidMount() { if (isPro) { if (this.props.isOrgAdmin) { orgAdminAPI.orgAdminListDepartments(orgID).then((res) => { this.updateOptions(res); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); } else if (this.props.isSysAdmin) { systemAdminAPI.sysAdminListDepartments().then((res) => { this.updateOptions(res); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); } else { seafileAPI.listDepartments().then((res) => { this.updateOptions(res); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); } } } updateOptions = (departmentsRes) => { const options = departmentsRes.data.map(item => { let option = { value: item.name, email: item.email, label: item.name, }; return option; }); this.setState({ options }); }; onClick = () => { this.setState({ transferToUser: !this.state.transferToUser, }); }; toggle = (tab) => { if (this.state.activeTab !== tab) { this.setState({ activeTab: tab, reshare: false, selectedOption: null, }); } }; toggleReshareStatus = () => { this.setState({ reshare: !this.state.reshare }); }; renderTransContent = () => { let activeTab = this.state.activeTab; let reshare = this.state.reshare; let canTransferToDept = true; if (this.props.canTransferToDept != undefined) { canTransferToDept = this.props.canTransferToDept; } return (
{gettext('If the library is shared to another user, the sharing will be kept.')}
{isPro && canTransferToDept &&
{gettext('If the library is shared to another department, the sharing will be kept.')}
}
); }; render() { const { selectedOption } = this.state; const { itemName: repoName } = this.props; let title = gettext('Transfer Library {library_name}'); title = title.replace('{library_name}', '' + Utils.HTMLescape(repoName) + ''); let buttonDisabled = false; if (selectedOption === null || (Array.isArray(selectedOption) && selectedOption.length === 0)) { buttonDisabled = true; } return ( {this.renderTransContent()} ); } } TransferDialog.propTypes = propTypes; export default TransferDialog;