import { IconExternalLink } from '@tabler/icons-react'; import Link from 'next/link'; import { useEffect, useState } from 'react'; import { toast, ToastOptions } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import { useLoader } from '~/contexts/LoaderContext'; import { EmailAlertDTO, Optional } from '~/types'; import classes from '../UserSettings.module.css'; //Components import Error from '~/Components/UI/Error/Error'; import Switch from '~/Components/UI/Switch/Switch'; import { useFormStatus } from '~/hooks'; export default function EmailAlertSettings() { const toastOptions: ToastOptions = { position: 'top-right', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, //Callback > re-enabled button after notification. onClose: () => setIsSwitchDisabled(false), }; const { error, handleError, clearError } = useFormStatus(); const { start, stop } = useLoader(); ////State const [isSendingTestNotification, setIsSendingTestNotification] = useState(false); const [isSwitchDisabled, setIsSwitchDisabled] = useState(true); const [isAlertEnabled, setIsAlertEnabled] = useState>(undefined); const [info, setInfo] = useState(false); ////LifeCycle //Component did mount useEffect(() => { const dataFetch = async () => { try { const response = await fetch('/api/v1/notif/email/alert', { method: 'GET', headers: { 'Content-type': 'application/json', }, }); const data: Optional = await response.json(); setIsAlertEnabled(data?.emailAlert ?? false); setIsSwitchDisabled(false); } catch (error) { setIsSwitchDisabled(true); setIsAlertEnabled(false); handleError('Fetching email alert setting failed'); } }; dataFetch(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); ////Functions //Switch to enable/disable Email notifications const onChangeSwitchHandler = async (data: EmailAlertDTO) => { clearError(); start(); setIsSwitchDisabled(true); await fetch('/api/v1/notif/email/alert', { method: 'PUT', headers: { 'Content-type': 'application/json', }, body: JSON.stringify(data), }) .then((response) => { if (response.ok && typeof data.emailAlert === 'boolean') { setIsAlertEnabled(data.emailAlert); toast.success( data.emailAlert ? 'Email notification enabled !' : 'Email notification disabled !', toastOptions ); } else { handleError('Update email alert setting failed.'); } }) .catch(() => { handleError('Update email alert setting failed.'); }) .finally(() => { stop(); setIsSwitchDisabled(false); }); }; //Send a test notification by email const onSendTestMailHandler = async () => { clearError(); start(); setIsSendingTestNotification(true); try { const response = await fetch('/api/v1/notif/email/test', { method: 'POST', headers: { 'Content-type': 'application/json', }, }); const result = await response.json(); if (!response.ok) { setIsSendingTestNotification(false); handleError(result.message); } else { setIsSendingTestNotification(false); setInfo(true); setTimeout(() => { setInfo(false); }, 4000); } } catch (error) { setIsSendingTestNotification(false); handleError('Send notification failed'); } finally { stop(); } }; return ( <> {/* EMAIL ALERT */}

Email alert

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