borgwarehouse/Containers/UserSettings/EmailAlertSettings/EmailAlertSettings.js

87 lines
2.8 KiB
JavaScript
Raw Normal View History

//Lib
2023-01-18 11:39:39 +01:00
import { toast } from 'react-toastify';
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,
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),
};
////State
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState();
2023-01-18 11:39:39 +01:00
const [disabled, setDisabled] = useState(false);
////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-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);
}
}
};
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}
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} />}
</div>
</div>
</div>
</>
);
}