feat: fetch the current state of the EmailAlert

This commit is contained in:
bsourisse 2023-01-18 15:19:40 +01:00
parent c2d48f8821
commit 70ecbf419b
2 changed files with 47 additions and 13 deletions

View file

@ -1,9 +1,10 @@
//Lib //Lib
import { useEffect } from 'react';
import { toast } from 'react-toastify'; import { toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css'; import 'react-toastify/dist/ReactToastify.css';
import classes from '../UserSettings.module.css'; import classes from '../UserSettings.module.css';
import { useState } from 'react'; import { useState } from 'react';
import { SpinnerDotted } from 'spinners-react'; import { SpinnerCircularFixed } from 'spinners-react';
//Components //Components
import Error from '../../../Components/UI/Error/Error'; import Error from '../../../Components/UI/Error/Error';
@ -24,9 +25,30 @@ export default function EmailAlertSettings(props) {
}; };
////State ////State
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(); const [error, setError] = useState();
const [disabled, setDisabled] = useState(false); const [disabled, setDisabled] = useState(false);
const [checked, setChecked] = useState();
////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) {
console.log('Fetching email alert setting failed.');
}
};
dataFetch();
}, []);
////Functions ////Functions
const onChangeSwitchHandler = async (data) => { const onChangeSwitchHandler = async (data) => {
@ -52,10 +74,10 @@ export default function EmailAlertSettings(props) {
}, 4000); }, 4000);
} else { } else {
if (data.emailAlert) { if (data.emailAlert) {
//setIsLoading(false); setChecked(!checked);
toast.success('Email notification enabled !', toastOptions); toast.success('Email notification enabled !', toastOptions);
} else { } else {
//setIsLoading(false); setChecked(!checked);
toast.success('Email notification disabled !', toastOptions); toast.success('Email notification disabled !', toastOptions);
} }
} }
@ -69,14 +91,26 @@ export default function EmailAlertSettings(props) {
</div> </div>
<div className={classes.setting}> <div className={classes.setting}>
<div className={classes.bwFormWrapper}> <div className={classes.bwFormWrapper}>
<Switch {isLoading ? (
disabled={disabled} <SpinnerCircularFixed
switchName='Email' size={30}
switchDescription='You will receive an alert every 24H if you have a down status.' thickness={150}
onChange={(e) => speed={150}
onChangeSwitchHandler({ emailAlert: e }) color='#704dff'
} secondaryColor='#c3b6fa'
/> />
) : (
<Switch
checked={checked}
disabled={disabled}
switchName='Email'
switchDescription='You will receive an alert every 24H if you have a down status.'
onChange={(e) =>
onChangeSwitchHandler({ emailAlert: e })
}
/>
)}
{error && <Error message={error} />} {error && <Error message={error} />}
</div> </div>
</div> </div>

View file

@ -54,7 +54,7 @@
} }
.bwFormWrapper { .bwFormWrapper {
text-align: center; text-align: left;
margin: auto; margin: auto;
width: 100%; width: 100%;
height: auto; height: auto;