import { css } from '@emotion/css'; import React, { ChangeEvent, FC, useState } from 'react'; import { GrafanaTheme } from '@grafana/data'; import { Button, Checkbox, Field, Input, Modal, stylesFactory, useTheme } from '@grafana/ui'; import { validateMultipleEmails } from '../utils/validators'; interface Props { emails: string; onDismiss: () => void; onSendTestEmail: (email: string, useEmailsFromReport: boolean) => void; } export const SendTestEmailModal: FC = ({ onDismiss, onSendTestEmail, emails }) => { const theme = useTheme(); const styles = getStyles(theme); const [email, setEmail] = useState(''); const [emailErrorMessage, setEmailErrorMessage] = useState(''); const [emailIsInvalid, setEmailIsInvalid] = useState(false); const [useEmailsFromReport, setUseEmailsFromReport] = useState(false); const handleUseEmailsFromReportChange = (event: ChangeEvent) => { const { checked } = event.target; setUseEmailsFromReport(checked); if (checked) { setEmail(emails); doValidation(emails); } else { clearError(); setEmail(''); } }; const handleEmailChange = (event: ChangeEvent) => { setEmail(event.currentTarget.value); setEmailIsInvalid(false); }; const doValidation = (e: string): boolean => { if (!validateMultipleEmails(e)) { setEmailIsInvalid(true); setEmailErrorMessage('Invalid email'); return false; } else { clearError(); return true; } }; const clearError = () => { setEmailIsInvalid(false); setEmailErrorMessage(''); }; const sendMail = () => { doValidation(email); if (!doValidation(email)) { return; } onSendTestEmail(email, useEmailsFromReport); onDismiss(); }; return ( <>
); }; const getStyles = stylesFactory((theme: GrafanaTheme) => { return { modal: css` width: 500px; `, content: css` margin-bottom: ${theme.spacing.lg}; `, }; });