1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-11 03:42:16 +00:00
seahub/frontend/src/components/dialog/transfer-dialog.js

233 lines
8.0 KiB
JavaScript
Raw Normal View History

2024-07-18 03:58:42 +00:00
import React, { Fragment } from 'react';
2018-12-07 02:46:47 +00:00
import PropTypes from 'prop-types';
import { Button, Modal, ModalHeader, ModalBody, ModalFooter,
Nav, NavItem, NavLink, TabContent, TabPane, Label } from 'reactstrap';
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';
2024-07-18 03:58:42 +00:00
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 '../common/switch';
import '../../css/transfer-dialog.css';
2018-12-07 02:46:47 +00:00
const propTypes = {
itemName: PropTypes.string.isRequired,
toggleDialog: PropTypes.func.isRequired,
2024-12-13 06:32:10 +00:00
onTransferRepo: PropTypes.func.isRequired,
canTransferToDept: PropTypes.bool,
isOrgAdmin: PropTypes.bool,
isSysAdmin: PropTypes.bool,
2024-10-15 07:39:37 +00:00
isDepAdminTransfer: PropTypes.bool,
2018-12-07 02:46:47 +00:00
};
const TRANS_USER = 'transUser';
const TRANS_DEPART = 'transDepart';
2018-12-07 02:46:47 +00:00
class TransferDialog extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedOption: null,
errorMsg: [],
2019-05-05 08:17:56 +00:00
transferToUser: true,
transferToGroup: false,
reshare: false,
2024-10-15 07:39:37 +00:00
activeTab: !this.props.isDepAdminTransfer ? TRANS_USER : TRANS_DEPART
2018-12-07 02:46:47 +00:00
};
this.options = [];
}
handleSelectChange = (option) => {
this.setState({ selectedOption: option });
};
2018-12-07 02:46:47 +00:00
submit = () => {
2024-12-13 06:32:10 +00:00
const { activeTab, reshare, selectedOption } = this.state;
const email = activeTab === TRANS_DEPART ? selectedOption.email : selectedOption[0].email;
this.props.onTransferRepo(email, reshare);
};
2018-12-07 02:46:47 +00:00
2019-05-05 08:17:56 +00:00
componentDidMount() {
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 if (isPro) {
seafileAPI.listDepartments().then((res) => {
this.updateOptions(res);
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
});
}
2019-05-05 08:17:56 +00:00
}
updateOptions = (departmentsRes) => {
2024-12-13 06:32:10 +00:00
this.options = departmentsRes.data.map(item => {
let option = {
value: item.name,
email: item.email,
label: item.name,
};
2024-12-13 06:32:10 +00:00
return option;
});
};
2019-05-05 08:17:56 +00:00
onClick = () => {
this.setState({
transferToUser: !this.state.transferToUser,
});
};
2019-05-05 08:17:56 +00:00
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;
2019-09-24 04:18:53 +00:00
let canTransferToDept = true;
if (this.props.canTransferToDept != undefined) {
canTransferToDept = this.props.canTransferToDept;
}
return (
<Fragment>
<div className="transfer-dialog-side">
<Nav pills>
2024-10-15 07:39:37 +00:00
{!this.props.isDepAdminTransfer &&
<NavItem role="tab" aria-selected={activeTab === TRANS_USER} aria-controls="transfer-user-panel">
<NavLink
className={activeTab === TRANS_USER ? 'active' : ''}
onClick={(this.toggle.bind(this, TRANS_USER))}
tabIndex="0"
onKeyDown={this.onTabKeyDown}
>
{gettext('Transfer to user')}
</NavLink>
</NavItem>
}
{isPro &&
<NavItem role="tab" aria-selected={activeTab === TRANS_DEPART} aria-controls="transfer-depart-panel">
<NavLink
className={activeTab === TRANS_DEPART ? 'active' : ''}
onClick={this.toggle.bind(this, TRANS_DEPART)}
tabIndex="0"
onKeyDown={this.onTabKeyDown}
>
{gettext('Transfer to department')}
</NavLink>
</NavItem>}
</Nav>
</div>
<div className="transfer-dialog-main">
<TabContent activeTab={this.state.activeTab}>
<Fragment>
<TabPane tabId="transUser" role="tabpanel" id="transfer-user-panel">
<Label className='transfer-repo-label'>{gettext('Users')}</Label>
<UserSelect
ref="userSelect"
isMulti={false}
placeholder={gettext('Select a user')}
onSelectChange={this.handleSelectChange}
/>
<Switch
checked={reshare}
disabled={false}
size="large"
textPosition="right"
className='transfer-repo-reshare-switch w-100 mt-3 mb-1'
onChange={this.toggleReshareStatus}
placeholder={gettext('Keep sharing')}
/>
<div className='tip'>{gettext('If the library is shared to another user, the sharing will be kept.')}</div>
</TabPane>
{isPro && canTransferToDept &&
<TabPane tabId="transDepart" role="tabpanel" id="transfer-depart-panel">
<Label className='transfer-repo-label'>{gettext('Departments')}</Label>
<SeahubSelect
isClearable
maxMenuHeight={200}
hideSelectedOptions={true}
components={makeAnimated()}
placeholder={gettext('Select a department')}
options={this.options}
onChange={this.handleSelectChange}
value={this.state.selectedOption}
className="transfer-repo-select-department"
/>
<Switch
checked={reshare}
disabled={false}
size="large"
textPosition="right"
className='transfer-repo-reshare-switch w-100 mt-3 mb-1'
onChange={this.toggleReshareStatus}
placeholder={gettext('Keep sharing')}
/>
<div className='tip'>{gettext('If the library is shared to another department, the sharing will be kept.')}</div>
</TabPane>}
</Fragment>
</TabContent>
</div>
</Fragment>
);
};
render() {
2024-12-13 06:32:10 +00:00
const { selectedOption } = this.state;
const { itemName: repoName } = this.props;
let title = gettext('Transfer Library {library_name}');
title = title.replace('{library_name}', '<span class="op-target text-truncate mx-1">' + Utils.HTMLescape(repoName) + '</span>');
2024-12-13 06:32:10 +00:00
let buttonDisabled = false;
if (selectedOption === null || (Array.isArray(selectedOption) && selectedOption.length === 0)) {
buttonDisabled = true;
}
2018-12-07 02:46:47 +00:00
return (
2024-07-18 03:58:42 +00:00
<Modal isOpen={true} style={{ maxWidth: '720px' }} toggle={this.props.toggleDialog} className="transfer-dialog">
2018-12-07 02:46:47 +00:00
<ModalHeader toggle={this.props.toggleDialog}>
<span dangerouslySetInnerHTML={{ __html: title }} className="d-flex mw-100"></span>
2018-12-07 02:46:47 +00:00
</ModalHeader>
<ModalBody className="transfer-dialog-content" role="tablist">
{this.renderTransContent()}
2018-12-07 02:46:47 +00:00
</ModalBody>
<ModalFooter>
<Button color="secondary" onClick={this.props.toggleDialog}>{gettext('Cancel')}</Button>
2024-12-13 06:32:10 +00:00
<Button color="primary" onClick={this.submit} disabled={buttonDisabled}>{gettext('Submit')}</Button>
</ModalFooter>
2018-12-07 02:46:47 +00:00
</Modal>
);
}
}
TransferDialog.propTypes = propTypes;
export default TransferDialog;