1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-19 09:37:51 +00:00
seahub/frontend/src/components/dialog/invite-people-dialog.js

132 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-04-19 10:09:12 +00:00
import React from 'react';
import PropTypes from 'prop-types';
2019-08-05 12:46:59 +00:00
import { Utils } from '../../utils/utils';
import { gettext } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api';
import { Modal, ModalHeader, ModalBody, ModalFooter, Input, Button } from 'reactstrap';
2019-05-11 12:38:17 +00:00
import toaster from '../toast';
2019-08-05 12:46:59 +00:00
import Loading from '../loading';
const InvitePeopleDialogPropTypes = {
onInvitePeople: PropTypes.func.isRequired,
toggleDialog: PropTypes.func.isRequired,
};
2019-04-19 10:09:12 +00:00
class InvitePeopleDialog extends React.Component {
constructor(props) {
super(props);
this.state = {
emails: '',
errorMsg: '',
2019-08-05 12:46:59 +00:00
isSubmitting: false
2019-04-19 10:09:12 +00:00
};
}
2019-08-05 12:46:59 +00:00
handleInputChange = (e) => {
let emails = e.target.value;
2019-04-19 10:09:12 +00:00
this.setState({
emails: emails
});
if (this.state.errorMsg) {
this.setState({
errorMsg: ''
});
}
}
handleKeyDown = (e) => {
if (e.keyCode === 13) {
2019-04-23 05:36:21 +00:00
e.preventDefault();
2019-04-19 10:09:12 +00:00
this.handleSubmitInvite();
}
}
handleSubmitInvite = () => {
let emails = this.state.emails.trim();
2019-08-05 12:46:59 +00:00
if (!emails) {
this.setState({
errorMsg: gettext('It is required.')
});
return false;
}
2019-04-23 05:36:21 +00:00
let emailsArray = [];
2019-08-05 12:46:59 +00:00
emails = emails.split(',');
for (let i = 0, len = emails.length; i < len; i++) {
2019-04-23 05:36:21 +00:00
let email = emails[i].trim();
if (email) {
emailsArray.push(email);
}
}
2019-08-05 12:46:59 +00:00
if (!emailsArray.length) {
this.setState({
errorMsg: gettext('Email is invalid.')
});
return false;
}
this.setState({
isSubmitting: true
});
seafileAPI.invitePeople(emailsArray).then((res) => {
this.props.toggleDialog();
const success = res.data.success;
if (success.length) {
2019-04-23 05:36:21 +00:00
let successMsg = '';
2019-08-05 12:46:59 +00:00
if (success.length == 1) {
2019-04-23 05:36:21 +00:00
successMsg = gettext('Successfully invited %(email).')
2019-08-05 12:46:59 +00:00
.replace('%(email)', success[0].accepter);
} else {
2019-04-23 05:36:21 +00:00
successMsg = gettext('Successfully invited %(email) and %(num) other people.')
2019-08-05 12:46:59 +00:00
.replace('%(email)', success[0].accepter)
.replace('%(num)', success.length - 1);
2019-04-23 05:36:21 +00:00
}
2019-08-05 12:46:59 +00:00
toaster.success(successMsg);
this.props.onInvitePeople(success);
}
const failed = res.data.failed;
if (failed.length) {
for (let i = 0, len = failed.length; i < len; i++) {
let failedMsg = failed[i].email + ': ' + failed[i].error_msg;
toaster.danger(failedMsg);
2019-04-23 05:36:21 +00:00
}
}
2019-08-05 12:46:59 +00:00
}).catch((error) => {
const errorMsg = Utils.getErrorMsg(error);
toaster.danger(errorMsg);
this.props.toggleDialog();
});
2019-04-19 10:09:12 +00:00
}
render() {
2019-08-05 12:46:59 +00:00
const { isSubmitting } = this.state;
2019-04-19 10:09:12 +00:00
return (
2019-08-05 12:46:59 +00:00
<Modal isOpen={true} toggle={this.props.toggleDialog}>
<ModalHeader toggle={this.props.toggleDialog}>{gettext('Invite People')}</ModalHeader>
2019-04-19 10:09:12 +00:00
<ModalBody>
2019-04-23 05:36:21 +00:00
<label htmlFor="emails">{gettext('Emails')}</label>
2019-05-11 12:38:17 +00:00
<Input
2019-08-05 12:46:59 +00:00
type="text"
id="emails"
2019-05-15 06:56:46 +00:00
placeholder={gettext('Emails, separated by \',\'')}
2019-05-11 12:38:17 +00:00
value={this.state.emails}
2019-08-05 12:46:59 +00:00
onChange={this.handleInputChange}
2019-05-11 12:38:17 +00:00
onKeyDown={this.handleKeyDown}
2019-04-19 10:09:12 +00:00
/>
2019-08-05 12:46:59 +00:00
<p className="error mt-2">{this.state.errorMsg}</p>
2019-04-19 10:09:12 +00:00
</ModalBody>
<ModalFooter>
2019-08-05 12:46:59 +00:00
<Button color="secondary" onClick={this.props.toggleDialog}>{gettext('Cancel')}</Button>
<Button className="submit-btn" color="primary" onClick={this.handleSubmitInvite} disabled={isSubmitting}>{isSubmitting ? <Loading /> : gettext('Submit')}</Button>
2019-04-19 10:09:12 +00:00
</ModalFooter>
</Modal>
);
}
}
InvitePeopleDialog.propTypes = InvitePeopleDialogPropTypes;
2019-08-05 12:46:59 +00:00
export default InvitePeopleDialog;