2023-01-16 18:28:54 +01:00
|
|
|
//Lib
|
2023-01-18 11:39:39 +01:00
|
|
|
import { toast } from 'react-toastify';
|
2023-01-16 18:28:54 +01:00
|
|
|
import 'react-toastify/dist/ReactToastify.css';
|
|
|
|
import classes from '../UserSettings.module.css';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import { SpinnerDotted } from 'spinners-react';
|
|
|
|
|
|
|
|
//Components
|
|
|
|
import Error from '../../../Components/UI/Error/Error';
|
|
|
|
import Switch from '../../../Components/UI/Switch/Switch';
|
|
|
|
|
|
|
|
export default function EmailAlertSettings(props) {
|
|
|
|
//Var
|
|
|
|
const toastOptions = {
|
|
|
|
position: 'top-right',
|
2023-01-18 11:39:39 +01:00
|
|
|
autoClose: 5000,
|
2023-01-16 18:28:54 +01:00
|
|
|
hideProgressBar: false,
|
|
|
|
closeOnClick: true,
|
|
|
|
pauseOnHover: true,
|
|
|
|
draggable: true,
|
|
|
|
progress: undefined,
|
2023-01-18 11:39:39 +01:00
|
|
|
//Callback > re-enabled button after notification.
|
|
|
|
onClose: () => setDisabled(false),
|
2023-01-16 18:28:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
////State
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const [error, setError] = useState();
|
2023-01-18 11:39:39 +01:00
|
|
|
const [disabled, setDisabled] = useState(false);
|
2023-01-16 18:28:54 +01:00
|
|
|
|
|
|
|
////Functions
|
2023-01-18 11:39:39 +01:00
|
|
|
const onChangeSwitchHandler = async (data) => {
|
|
|
|
//Remove old error
|
|
|
|
setError();
|
|
|
|
//Disabled button
|
|
|
|
setDisabled(true);
|
|
|
|
const response = await fetch('/api/account/updateEmailAlert', {
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
'Content-type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
});
|
|
|
|
const result = await response.json();
|
2023-01-16 18:28:54 +01:00
|
|
|
|
2023-01-18 11:39:39 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
//setIsLoading(false);
|
|
|
|
setError(result.message);
|
|
|
|
setTimeout(() => {
|
|
|
|
setError();
|
|
|
|
setDisabled(false);
|
|
|
|
}, 4000);
|
|
|
|
} else {
|
|
|
|
if (data.emailAlert) {
|
|
|
|
//setIsLoading(false);
|
|
|
|
toast.success('Email notification enabled !', toastOptions);
|
|
|
|
} else {
|
|
|
|
//setIsLoading(false);
|
|
|
|
toast.success('Email notification disabled !', toastOptions);
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 18:28:54 +01:00
|
|
|
};
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{/* EMAIL ALERT */}
|
|
|
|
<div className={classes.containerSetting}>
|
|
|
|
<div className={classes.settingCategory}>
|
|
|
|
<h2>Alerting</h2>
|
|
|
|
</div>
|
|
|
|
<div className={classes.setting}>
|
|
|
|
<div className={classes.bwFormWrapper}>
|
|
|
|
<Switch
|
2023-01-18 11:39:39 +01:00
|
|
|
disabled={disabled}
|
2023-01-16 18:28:54 +01:00
|
|
|
switchName='Email'
|
|
|
|
switchDescription='You will receive an alert every 24H if you have a down status.'
|
|
|
|
onChange={(e) =>
|
|
|
|
onChangeSwitchHandler({ emailAlert: e })
|
|
|
|
}
|
|
|
|
/>
|
2023-01-18 11:39:39 +01:00
|
|
|
{error && <Error message={error} />}
|
2023-01-16 18:28:54 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|