import React from 'react'; import PropTypes from 'prop-types'; import { Button, Form, FormGroup, Label } from 'reactstrap'; import { seafileAPI } from '../utils/seafile-api'; import { gettext } from '../utils/constants'; import { Utils } from '../utils/utils'; import toaster from './toast'; const propTypes = { token: PropTypes.string.isRequired, linkType: PropTypes.string.isRequired, toggleSendLink: PropTypes.func.isRequired, closeShareDialog: PropTypes.func.isRequired }; class SendLink extends React.Component { constructor(props) { super(props); this.state = { emails: '', msg: '', errorMsg: '', btnDisabled: false, sending: false }; } handleEmailsInputChange = (e) => { this.setState({ emails: e.target.value }); } handleMsgInputChange = (e) => { this.setState({ msg: e.target.value }); } sendLink = () => { const { emails, msg } = this.state; if (!emails.trim()) { this.setState({ errorMsg: gettext('Please input at least an email.') }); return; } this.setState({ btnDisabled: true, sending: true }); const { token, linkType } = this.props; const request = linkType == 'uploadLink' ? seafileAPI.sendUploadLink(token, emails.trim(), msg.trim()) : seafileAPI.sendShareLink(token, emails.trim(), msg.trim()); request.then((res) => { this.props.closeShareDialog(); const { success, failed } = res.data; if (success.length) { const feedbackMsg = gettext('Successfully sent to {placeholder}') .replace('{placeholder}', success.join(', ')); toaster.success(feedbackMsg); } if (failed.length) { failed.map((item) => { const feedbackMsg = gettext('Failed to send to {email_placeholder}: {errorMsg_placeholder}') .replace('{email_placeholder}', item.email) .replace('{errorMsg_placeholder}', item.error_msg); toaster.warning(feedbackMsg); }); } }).catch((error) => { let errorMsg = Utils.getErrorMsg(error); this.setState({ btnDisabled: false, sending: false, errorMsg: errorMsg }); }); } render() { const { emails, msg, errorMsg, btnDisabled, sending } = this.state; return (
); } } SendLink.propTypes = propTypes; export default SendLink;