//Lib import { useEffect } from 'react'; import { toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import classes from '../UserSettings.module.css'; import { useState } from 'react'; import { SpinnerCircularFixed } from 'spinners-react'; import { IconExternalLink } from '@tabler/icons'; import Link from 'next/link'; //Components import Error from '../../../Components/UI/Error/Error'; import Switch from '../../../Components/UI/Switch/Switch'; import AppriseURLs from './AppriseURLs/AppriseURLs'; import AppriseMode from './AppriseMode/AppriseMode'; export default function AppriseAlertSettings() { //Var const toastOptions = { position: 'top-right', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, //Callback > re-enabled button after notification. onClose: () => setDisabled(false), }; ////State const [checkIsLoading, setCheckIsLoading] = useState(true); const [error, setError] = useState(); const [disabled, setDisabled] = useState(false); const [checked, setChecked] = useState(); const [testIsLoading, setTestIsLoading] = useState(false); const [info, setInfo] = useState(false); ////LifeCycle //Component did mount useEffect(() => { //Initial fetch to get the status of Apprise Alert const getAppriseAlert = async () => { try { const response = await fetch('/api/account/getAppriseAlert', { method: 'GET', headers: { 'Content-type': 'application/json', }, }); setChecked((await response.json()).appriseAlert); setCheckIsLoading(false); } catch (error) { setError( 'Fetching apprise alert setting failed. Contact your administrator.' ); console.log('Fetching apprise alert setting failed.'); setCheckIsLoading(false); } }; getAppriseAlert(); }, []); ////Functions //Switch to enable/disable Apprise notifications const onChangeSwitchHandler = async (data) => { //Remove old error setError(); //Disabled button setDisabled(true); await fetch('/api/account/updateAppriseAlert', { method: 'PUT', headers: { 'Content-type': 'application/json', }, body: JSON.stringify(data), }) .then((response) => { console.log(response); if (response.ok) { if (data.appriseAlert) { setChecked(!checked); toast.success( 'Apprise notifications enabled.', toastOptions ); } else { setChecked(!checked); toast.success( 'Apprise notifications disabled.', toastOptions ); } } else { setError('Update apprise alert setting failed.'); setTimeout(() => { setError(); setDisabled(false); }, 4000); } }) .catch((error) => { console.log(error); setError('Update Apprise failed. Contact your administrator.'); setTimeout(() => { setError(); setDisabled(false); }, 4000); }); }; //Send Apprise test notification to services const onSendTestAppriseHandler = async () => { //Loading setTestIsLoading(true); //Remove old error setError(); try { const response = await fetch('/api/account/sendTestApprise', { method: 'POST', headers: { 'Content-type': 'application/json', }, body: JSON.stringify({ sendTestApprise: true }), }); const result = await response.json(); if (!response.ok) { setTestIsLoading(false); setError(result.message); } else { setTestIsLoading(false); setInfo(true); setTimeout(() => { setInfo(false); }, 4000); } } catch (error) { setTestIsLoading(false); console.log(error); setError('Send notification failed. Contact your administrator.'); setTimeout(() => { setError(); }, 4000); } }; return ( <> {/* APPRISE ALERT */}

Apprise alert

{/* NOTIFY SWITCH */} {checkIsLoading ? ( ) : ( onChangeSwitchHandler({ appriseAlert: e }) } /> )} {/* APPRISE SERVICES URLS */} {/* APPRISE MODE SELECTION */} {/* APPRISE TEST BUTTON */} {testIsLoading ? ( ) : ( )} {info && ( Notification successfully sent. )} {error && }
); }