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'; 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: 'Notification Email Recipients', emailNotificationTypesAlertDescription: 'For the "Media Requested" and "Media Failed" notification types,\ notifications will only be sent to users with the "Manage Requests" permission.', }); 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) ), 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, }, }); 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, }, }); addToast(intl.formatMessage(messages.testsent), { appearance: 'info', autoDismiss: true, }); }; return ( <> {intl.formatMessage( messages.emailNotificationTypesAlertDescription )} {intl.formatMessage(messages.agentenabled)} {intl.formatMessage(messages.emailsender)} {errors.emailFrom && touched.emailFrom && ( {errors.emailFrom} )} {intl.formatMessage(messages.senderName)} {intl.formatMessage(messages.smtpHost)} {errors.smtpHost && touched.smtpHost && ( {errors.smtpHost} )} {intl.formatMessage(messages.smtpPort)} {errors.smtpPort && touched.smtpPort && ( {errors.smtpPort} )} {intl.formatMessage(messages.enableSsl)} {intl.formatMessage(messages.ssldisabletip)} {intl.formatMessage(messages.allowselfsigned)} {intl.formatMessage(messages.authUser)} {intl.formatMessage(messages.authPass)} {intl.formatMessage(messages.notificationtypes)} setFieldValue('types', newTypes) } /> { e.preventDefault(); testSettings(); }} > {intl.formatMessage(messages.test)} {isSubmitting ? intl.formatMessage(messages.saving) : intl.formatMessage(messages.save)} > ); }} ); }; export default NotificationsEmail;