SendTestEmailModal.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { css } from '@emotion/css';
  2. import React, { ChangeEvent, FC, useState } from 'react';
  3. import { GrafanaTheme } from '@grafana/data';
  4. import { Button, Checkbox, Field, Input, Modal, stylesFactory, useTheme } from '@grafana/ui';
  5. import { validateMultipleEmails } from '../utils/validators';
  6. interface Props {
  7. emails: string;
  8. onDismiss: () => void;
  9. onSendTestEmail: (email: string, useEmailsFromReport: boolean) => void;
  10. }
  11. export const SendTestEmailModal: FC<Props> = ({ onDismiss, onSendTestEmail, emails }) => {
  12. const theme = useTheme();
  13. const styles = getStyles(theme);
  14. const [email, setEmail] = useState('');
  15. const [emailErrorMessage, setEmailErrorMessage] = useState('');
  16. const [emailIsInvalid, setEmailIsInvalid] = useState(false);
  17. const [useEmailsFromReport, setUseEmailsFromReport] = useState(false);
  18. const handleUseEmailsFromReportChange = (event: ChangeEvent<HTMLInputElement>) => {
  19. const { checked } = event.target;
  20. setUseEmailsFromReport(checked);
  21. if (checked) {
  22. setEmail(emails);
  23. doValidation(emails);
  24. } else {
  25. clearError();
  26. setEmail('');
  27. }
  28. };
  29. const handleEmailChange = (event: ChangeEvent<HTMLInputElement>) => {
  30. setEmail(event.currentTarget.value);
  31. setEmailIsInvalid(false);
  32. };
  33. const doValidation = (e: string): boolean => {
  34. if (!validateMultipleEmails(e)) {
  35. setEmailIsInvalid(true);
  36. setEmailErrorMessage('Invalid email');
  37. return false;
  38. } else {
  39. clearError();
  40. return true;
  41. }
  42. };
  43. const clearError = () => {
  44. setEmailIsInvalid(false);
  45. setEmailErrorMessage('');
  46. };
  47. const sendMail = () => {
  48. doValidation(email);
  49. if (!doValidation(email)) {
  50. return;
  51. }
  52. onSendTestEmail(email, useEmailsFromReport);
  53. onDismiss();
  54. };
  55. return (
  56. <Modal className={styles.modal} isOpen={true} title="Send test email" icon="share-alt" onDismiss={onDismiss}>
  57. <>
  58. <div className={styles.content}>
  59. <Field label="Email" disabled={useEmailsFromReport} invalid={emailIsInvalid} error={emailErrorMessage}>
  60. <Input
  61. disabled={useEmailsFromReport}
  62. name="email"
  63. placeholder="your.address@company.com"
  64. type="email"
  65. value={email}
  66. onChange={handleEmailChange}
  67. />
  68. </Field>
  69. <Checkbox
  70. value={useEmailsFromReport}
  71. label="Use emails from report"
  72. onChange={handleUseEmailsFromReportChange}
  73. />
  74. </div>
  75. <Modal.ButtonRow>
  76. <Button variant="secondary" onClick={onDismiss} fill="outline">
  77. Cancel
  78. </Button>
  79. <Button variant="primary" onClick={sendMail}>
  80. Send
  81. </Button>
  82. </Modal.ButtonRow>
  83. </>
  84. </Modal>
  85. );
  86. };
  87. const getStyles = stylesFactory((theme: GrafanaTheme) => {
  88. return {
  89. modal: css`
  90. width: 500px;
  91. `,
  92. content: css`
  93. margin-bottom: ${theme.spacing.lg};
  94. `,
  95. };
  96. });