import { SaveIcon } from '@heroicons/react/outline'; import axios from 'axios'; import { Form, Formik } from 'formik'; import { useRouter } from 'next/router'; import React from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useToasts } from 'react-toast-notifications'; import useSWR, { mutate } from 'swr'; import { UserSettingsNotificationsResponse } from '../../../../../server/interfaces/api/userSettingsInterfaces'; import { useUser } from '../../../../hooks/useUser'; import globalMessages from '../../../../i18n/globalMessages'; import Button from '../../../Common/Button'; import LoadingSpinner from '../../../Common/LoadingSpinner'; import NotificationTypeSelector, { ALL_NOTIFICATIONS, } from '../../../NotificationTypeSelector'; const messages = defineMessages({ webpushsettingssaved: 'Web push notification settings saved successfully!', webpushsettingsfailed: 'Web push notification settings failed to save.', }); const UserWebPushSettings: React.FC = () => { const intl = useIntl(); const { addToast } = useToasts(); const router = useRouter(); const { user } = useUser({ id: Number(router.query.userId) }); const { data, error, mutate: revalidate, } = useSWR( user ? `/api/v1/user/${user?.id}/settings/notifications` : null ); if (!data && !error) { return ; } return ( { try { await axios.post(`/api/v1/user/${user?.id}/settings/notifications`, { pgpKey: data?.pgpKey, discordId: data?.discordId, pushbulletAccessToken: data?.pushbulletAccessToken, pushoverApplicationToken: data?.pushoverApplicationToken, pushoverUserKey: data?.pushoverUserKey, telegramChatId: data?.telegramChatId, telegramSendSilently: data?.telegramSendSilently, notificationTypes: { webpush: values.types, }, }); mutate('/api/v1/settings/public'); addToast(intl.formatMessage(messages.webpushsettingssaved), { appearance: 'success', autoDismiss: true, }); } catch (e) { addToast(intl.formatMessage(messages.webpushsettingsfailed), { appearance: 'error', autoDismiss: true, }); } finally { revalidate(); } }} > {({ errors, touched, isSubmitting, isValid, values, setFieldValue, setFieldTouched, }) => { return (
{ setFieldValue('types', newTypes); setFieldTouched('types'); }} error={ errors.types && touched.types ? (errors.types as string) : undefined } />
); }}
); }; export default UserWebPushSettings;