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 Alert from '../../../Common/Alert'; import NotificationTypeSelector from '../../../NotificationTypeSelector'; const messages = defineMessages({ save: 'Save Changes', saving: 'Saving…', agentenabled: 'Enable Agent', accessToken: 'Application/API Token', userToken: 'User Key', validationAccessTokenRequired: 'You must provide a valid application token', validationUserTokenRequired: 'You must provide a valid user key', pushoversettingssaved: 'Pushover notification settings saved successfully!', pushoversettingsfailed: 'Pushover notification settings failed to save.', testsent: 'Test notification sent!', test: 'Test', settinguppushover: 'Setting Up Pushover Notifications', settinguppushoverDescription: 'To configure Pushover notifications, you will need to register an application and enter the API token below.\ (You can use one of our official icons on GitHub.)\ You will also need your user key.', notificationtypes: 'Notification Types', }); const NotificationsPushover: React.FC = () => { const intl = useIntl(); const { addToast } = useToasts(); const { data, error, revalidate } = useSWR( '/api/v1/settings/notifications/pushover' ); const NotificationsPushoverSchema = Yup.object().shape({ accessToken: Yup.string() .required(intl.formatMessage(messages.validationAccessTokenRequired)) .matches( /^[a-z\d]{30}$/i, intl.formatMessage(messages.validationAccessTokenRequired) ), userToken: Yup.string() .required(intl.formatMessage(messages.validationUserTokenRequired)) .matches( /^[a-z\d]{30}$/i, intl.formatMessage(messages.validationUserTokenRequired) ), }); if (!data && !error) { return ; } return ( { try { await axios.post('/api/v1/settings/notifications/pushover', { enabled: values.enabled, types: values.types, options: { accessToken: values.accessToken, userToken: values.userToken, }, }); addToast(intl.formatMessage(messages.pushoversettingssaved), { appearance: 'success', autoDismiss: true, }); } catch (e) { addToast(intl.formatMessage(messages.pushoversettingsfailed), { appearance: 'error', autoDismiss: true, }); } finally { revalidate(); } }} > {({ errors, touched, isSubmitting, values, isValid, setFieldValue }) => { const testSettings = async () => { await axios.post('/api/v1/settings/notifications/pushover/test', { enabled: true, types: values.types, options: { accessToken: values.accessToken, userToken: values.userToken, }, }); addToast(intl.formatMessage(messages.testsent), { appearance: 'info', autoDismiss: true, }); }; return ( <> {intl.formatMessage(messages.settinguppushoverDescription, { RegisterApplicationLink: function RegisterApplicationLink(msg) { return ( {msg} ); }, IconLink: function IconLink(msg) { return ( {msg} ); }, })}
{errors.accessToken && touched.accessToken && (
{errors.accessToken}
)}
{errors.userToken && touched.userToken && (
{errors.userToken}
)}
{intl.formatMessage(messages.notificationtypes)} *
setFieldValue('types', newTypes) } />
); }}
); }; export default NotificationsPushover;