import React from 'react'; import { Field, Form, Formik } from 'formik'; import useSWR from 'swr'; import LoadingSpinner from '../../Common/LoadingSpinner'; import Button from '../../Common/Button'; import { defineMessages, useIntl } from 'react-intl'; import axios from 'axios'; import * as Yup from 'yup'; import { useToasts } from 'react-toast-notifications'; import NotificationTypeSelector from '../../NotificationTypeSelector'; import Alert from '../../Common/Alert'; import Badge from '../../Common/Badge'; import globalMessages from '../../../i18n/globalMessages'; const messages = defineMessages({ save: 'Save Changes', saving: 'Saving…', validationSmtpHostRequired: 'You must provide an SMTP host', validationSmtpPortRequired: 'You must provide an SMTP port', agentenabled: 'Enable Agent', emailsender: 'Sender Address', smtpHost: 'SMTP Host', smtpPort: 'SMTP Port', enableSsl: 'Enable SSL', authUser: 'SMTP Username', authPass: 'SMTP Password', emailsettingssaved: 'Email notification settings saved successfully!', emailsettingsfailed: 'Email notification settings failed to save.', test: 'Test', testsent: 'Test notification sent!', allowselfsigned: 'Allow Self-Signed Certificates', ssldisabletip: 'SSL should be disabled on standard TLS connections (port 587)', senderName: 'Sender Name', notificationtypes: 'Notification Types', validationEmail: 'You must provide a valid email address', emailNotificationTypesAlert: 'Email Notification Recipients', emailNotificationTypesAlertDescription: 'Media Requested, Media Automatically Approved, and Media Failed\ email notifications are sent to all users with the Manage Requests permission.', emailNotificationTypesAlertDescriptionPt2: 'Media Approved, Media Declined, and Media Available\ email notifications are sent to the user who submitted the request.', pgpPrivateKey: 'PGP Private Key', pgpPrivateKeyTip: 'Sign encrypted email messages (PGP password is also required)', pgpPassword: 'PGP Password', pgpPasswordTip: 'Sign encrypted email messages (PGP private key is also required)', }); export function PgpLink(msg: string): JSX.Element { return ( {msg} ); } const NotificationsEmail: React.FC = () => { const intl = useIntl(); const { addToast } = useToasts(); const { data, error, revalidate } = useSWR( '/api/v1/settings/notifications/email' ); const NotificationsEmailSchema = Yup.object().shape({ emailFrom: Yup.string() .required(intl.formatMessage(messages.validationEmail)) .email(intl.formatMessage(messages.validationEmail)), smtpHost: Yup.string() .required(intl.formatMessage(messages.validationSmtpHostRequired)) .matches( // eslint-disable-next-line /^(([a-z]|\d|_|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*)?([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])$/i, intl.formatMessage(messages.validationSmtpHostRequired) ), smtpPort: Yup.number().required( intl.formatMessage(messages.validationSmtpPortRequired) ), }); if (!data && !error) { return ; } return ( { try { await axios.post('/api/v1/settings/notifications/email', { enabled: values.enabled, types: values.types, options: { emailFrom: values.emailFrom, smtpHost: values.smtpHost, smtpPort: Number(values.smtpPort), secure: values.secure, authUser: values.authUser, authPass: values.authPass, allowSelfSigned: values.allowSelfSigned, senderName: values.senderName, pgpPrivateKey: values.pgpPrivateKey, pgpPassword: values.pgpPassword, }, }); addToast(intl.formatMessage(messages.emailsettingssaved), { appearance: 'success', autoDismiss: true, }); } catch (e) { addToast(intl.formatMessage(messages.emailsettingsfailed), { appearance: 'error', autoDismiss: true, }); } finally { revalidate(); } }} > {({ errors, touched, isSubmitting, values, isValid, setFieldValue }) => { const testSettings = async () => { await axios.post('/api/v1/settings/notifications/email/test', { enabled: true, types: values.types, options: { emailFrom: values.emailFrom, smtpHost: values.smtpHost, smtpPort: Number(values.smtpPort), secure: values.secure, authUser: values.authUser, authPass: values.authPass, senderName: values.senderName, pgpPrivateKey: values.pgpPrivateKey, pgpPassword: values.pgpPassword, }, }); addToast(intl.formatMessage(messages.testsent), { appearance: 'info', autoDismiss: true, }); }; return ( <>

{intl.formatMessage( messages.emailNotificationTypesAlertDescription, { strong: function strong(msg) { return ( {msg} ); }, } )}

{intl.formatMessage( messages.emailNotificationTypesAlertDescriptionPt2, { strong: function strong(msg) { return ( {msg} ); }, } )}

{errors.emailFrom && touched.emailFrom && (
{errors.emailFrom}
)}
{errors.smtpHost && touched.smtpHost && (
{errors.smtpHost}
)}
{errors.smtpPort && touched.smtpPort && (
{errors.smtpPort}
)}
{intl.formatMessage(messages.notificationtypes)}
setFieldValue('types', newTypes) } />
); }}
); }; export default NotificationsEmail;