/* * SonarQube * Copyright (C) 2009-2023 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; import { sendTestEmail } from '../../../api/settings'; import withCurrentUserContext from '../../../app/components/current-user/withCurrentUserContext'; import { SubmitButton } from '../../../components/controls/buttons'; import { Alert } from '../../../components/ui/Alert'; import DeferredSpinner from '../../../components/ui/DeferredSpinner'; import MandatoryFieldMarker from '../../../components/ui/MandatoryFieldMarker'; import MandatoryFieldsExplanation from '../../../components/ui/MandatoryFieldsExplanation'; import { translate, translateWithParameters } from '../../../helpers/l10n'; import { parseError } from '../../../helpers/request'; import { LoggedInUser } from '../../../types/users'; interface Props { currentUser: LoggedInUser; } interface State { recipient: string; subject: string; message: string; loading: boolean; success?: string; error?: string; } export class EmailForm extends React.PureComponent { mounted = false; constructor(props: Props) { super(props); this.state = { recipient: this.props.currentUser.email || '', subject: translate('email_configuration.test.subject'), message: translate('email_configuration.test.message_text'), loading: false, }; } componentDidMount() { this.mounted = true; } componentWillUnmount() { this.mounted = false; } handleError = (response: Response) => { return parseError(response).then((message) => { if (this.mounted) { this.setState({ error: message, loading: false }); } }); }; handleFormSubmit = (event: React.FormEvent) => { event.preventDefault(); this.setState({ success: undefined, error: undefined, loading: true }); const { recipient, subject, message } = this.state; sendTestEmail(recipient, subject, message).then(() => { if (this.mounted) { this.setState({ success: recipient, loading: false }); } }, this.handleError); }; onRecipientChange = (event: React.ChangeEvent) => { this.setState({ recipient: event.target.value }); }; onSubjectChange = (event: React.ChangeEvent) => { this.setState({ subject: event.target.value }); }; onMessageChange = (event: React.ChangeEvent) => { this.setState({ message: event.target.value }); }; render() { const { error, loading, message, recipient, subject, success } = this.state; return (

{translate('email_configuration.test.title')}

{success && (
{translateWithParameters('email_configuration.test.email_was_sent_to_x', success)}
)} {error !== undefined && (
{error}
)}