2019-04-19 10:09:12 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
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-04-19 10:09:12 +00:00
|
|
|
|
|
|
|
class InvitePeopleDialog extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
emails: '',
|
|
|
|
errorMsg: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEmailsChange = (event) => {
|
|
|
|
let emails = event.target.value;
|
|
|
|
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-04-23 05:36:21 +00:00
|
|
|
let emailsArray = [];
|
|
|
|
emails = emails.split(',');
|
|
|
|
for (let i = 0; i < emails.length; i++) {
|
|
|
|
let email = emails[i].trim();
|
|
|
|
if (email) {
|
|
|
|
emailsArray.push(email);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (emailsArray.length) {
|
|
|
|
seafileAPI.invitePeople(emailsArray).then((res) => {
|
2019-04-19 10:09:12 +00:00
|
|
|
this.setState({
|
2019-04-23 05:36:21 +00:00
|
|
|
emails: '',
|
2019-04-19 10:09:12 +00:00
|
|
|
});
|
2019-04-23 05:36:21 +00:00
|
|
|
this.props.toggleInvitePeopleDialog();
|
|
|
|
// success messages
|
|
|
|
let successMsg = '';
|
|
|
|
if (res.data.success.length === 1) {
|
|
|
|
successMsg = gettext('Successfully invited %(email).')
|
|
|
|
.replace('%(email)', res.data.success[0].accepter);
|
|
|
|
} else if(res.data.success.length > 1) {
|
|
|
|
successMsg = gettext('Successfully invited %(email) and %(num) other people.')
|
|
|
|
.replace('%(email)', res.data.success[0].accepter)
|
|
|
|
.replace('%(num)', res.data.success.length - 1);
|
|
|
|
}
|
|
|
|
if (successMsg) {
|
|
|
|
toaster.success(successMsg, {duration: 2});
|
|
|
|
this.props.onInvitePeople(res.data.success);
|
|
|
|
}
|
|
|
|
// failed messages
|
|
|
|
if (res.data.failed.length) {
|
|
|
|
for (let i = 0; i< res.data.failed.length; i++){
|
|
|
|
let failedMsg = res.data.failed[i].email + ': ' + res.data.failed[i].error_msg;
|
|
|
|
toaster.danger(failedMsg, {duration: 3});}
|
|
|
|
}
|
|
|
|
}).catch((error) => {
|
|
|
|
this.props.toggleInvitePeopleDialog();
|
|
|
|
if (error.response){
|
|
|
|
toaster.danger(error.response.data.detail || gettext('Error'), {duration: 3});
|
|
|
|
} else {
|
|
|
|
toaster.danger(gettext('Please check the network.'), {duration: 3});
|
|
|
|
}
|
2019-04-19 10:09:12 +00:00
|
|
|
});
|
|
|
|
} else {
|
2019-04-23 05:36:21 +00:00
|
|
|
if (this.state.emails){
|
|
|
|
this.setState({
|
|
|
|
errorMsg: gettext('Email is invalid.')
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
errorMsg: gettext('It is required.')
|
|
|
|
});
|
|
|
|
}
|
2019-04-19 10:09:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2019-04-23 05:36:21 +00:00
|
|
|
<Modal isOpen={this.props.isInvitePeopleDialogOpen} toggle={this.props.toggleInvitePeopleDialog}>
|
|
|
|
<ModalHeader toggle={this.props.toggleInvitePeopleDialog}>{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
|
|
|
|
type="text" id="emails"
|
|
|
|
placeholder={gettext("Emails, separated by ','")}
|
|
|
|
value={this.state.emails}
|
|
|
|
onChange={this.handleEmailsChange}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
2019-04-19 10:09:12 +00:00
|
|
|
/>
|
|
|
|
<span className="error">{this.state.errorMsg}</span>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2019-04-23 05:36:21 +00:00
|
|
|
<Button color="secondary" onClick={this.props.toggleInvitePeopleDialog}>{gettext('Cancel')}</Button>
|
2019-04-19 10:09:12 +00:00
|
|
|
<Button color="primary" onClick={this.handleSubmitInvite}>{gettext('Submit')}</Button>
|
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const InvitePeopleDialogPropTypes = {
|
2019-04-23 05:36:21 +00:00
|
|
|
toggleInvitePeopleDialog: PropTypes.func.isRequired,
|
|
|
|
isInvitePeopleDialogOpen: PropTypes.bool.isRequired,
|
2019-04-19 10:09:12 +00:00
|
|
|
onInvitePeople: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
InvitePeopleDialog.propTypes = InvitePeopleDialogPropTypes;
|
|
|
|
|
|
|
|
export default InvitePeopleDialog;
|