//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'; export default function EmailAlertSettings() { //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 [isLoading, setIsLoading] = useState(true); const [testIsLoading, setTestIsLoading] = useState(false); const [error, setError] = useState(); const [disabled, setDisabled] = useState(false); const [checked, setChecked] = useState(); const [info, setInfo] = useState(false); ////LifeCycle //Component did mount useEffect(() => { const dataFetch = async () => { try { const response = await fetch('/api/account/getEmailAlert', { method: 'GET', headers: { 'Content-type': 'application/json', }, }); setChecked((await response.json()).emailAlert); setIsLoading(false); } catch (error) { setError( 'Fetching email alert setting failed. Contact your administrator.' ); console.log('Fetching email alert setting failed.'); setIsLoading(false); } }; dataFetch(); }, []); ////Functions //Switch to enable/disable Email notifications const onChangeSwitchHandler = async (data) => { //Remove old error setError(); //Disabled button setDisabled(true); await fetch('/api/account/updateEmailAlert', { method: 'PUT', headers: { 'Content-type': 'application/json', }, body: JSON.stringify(data), }) .then((response) => { console.log(response); if (response.ok) { if (data.emailAlert) { setChecked(!checked); toast.success( 'Email notification enabled !', toastOptions ); } else { setChecked(!checked); toast.success( 'Email notification disabled !', toastOptions ); } } else { setError('Update email alert setting failed.'); setTimeout(() => { setError(); setDisabled(false); }, 4000); } }) .catch((error) => { console.log(error); setError('Update failed. Contact your administrator.'); setTimeout(() => { setError(); setDisabled(false); }, 4000); }); }; //Send a test notification by email const onSendTestMailHandler = async () => { //Loading setTestIsLoading(true); //Remove old error setError(); await fetch('/api/account/sendTestEmail', { method: 'POST', }) .then((response) => { if (!response.ok) { setTestIsLoading(false); setError('Failed to send the notification.'); setTimeout(() => { setError(); }, 4000); } else { setTestIsLoading(false); setInfo(true); setTimeout(() => { setInfo(false); }, 4000); } }) .catch((error) => { setTestIsLoading(false); console.log(error); setError('Send email failed. Contact your administrator.'); setTimeout(() => { setError(); }, 4000); }); }; return ( <> {/* EMAIL ALERT */}

Email alert

{isLoading ? ( ) : ( onChangeSwitchHandler({ emailAlert: e }) } /> )} {testIsLoading ? ( ) : ( )} {info && ( Mail successfully sent. )} {error && }
); }