Commit a4348b96 authored by Bilal's avatar Bilal

Updated snackbar handling

parent 0e44734a
...@@ -21,7 +21,7 @@ const TelepayForm = () => { ...@@ -21,7 +21,7 @@ const TelepayForm = () => {
{ description: '', amount: '' }, { description: '', amount: '' },
], ],
}); });
const [snackbarOpen, setSnackbarOpen] = React.useState(false); const [errorSnackbarOpen, setErrorSnackbarOpen] = React.useState(false);
const [submitted, setSubmitted] = React.useState(false); const [submitted, setSubmitted] = React.useState(false);
const handleChange = (field, value) => { const handleChange = (field, value) => {
...@@ -69,11 +69,11 @@ const TelepayForm = () => { ...@@ -69,11 +69,11 @@ const TelepayForm = () => {
} else { } else {
const errorData = await response.json(); const errorData = await response.json();
setError(errorData.error || 'An error occurred'); setError(errorData.error || 'An error occurred');
setSnackbarOpen(true); setErrorSnackbarOpen(true);
} }
} catch (error) { } catch (error) {
setError('An error occurred while making the API request'); setError('An error occurred while making the API request');
setSnackbarOpen(true); setErrorSnackbarOpen(true);
} finally { } finally {
setSubmitted(false); setSubmitted(false);
} }
...@@ -88,37 +88,31 @@ const TelepayForm = () => { ...@@ -88,37 +88,31 @@ const TelepayForm = () => {
}); });
}; };
const handleClose = (event, reason) => { return (
if (reason === 'clickaway') { <div>
return; <Snackbar
open={errorSnackbarOpen}
autoHideDuration={3000}
onClose={(event, reason) => {
if (reason !== 'clickaway') {
setErrorSnackbarOpen(false);
} }
}}
setSnackbarOpen(false); message={error}
}; action={
const action = (
<React.Fragment> <React.Fragment>
<IconButton <IconButton
size='small' size='small'
aria-label='close' aria-label='close'
color='inherit' color='inherit'
onClick={handleClose} onClick={() => setErrorSnackbarOpen(false)}
> >
<CloseOutlined fontSize='small' /> <CloseOutlined fontSize='small' />
</IconButton> </IconButton>
</React.Fragment> </React.Fragment>
); }
return (
<div>
<Snackbar
open={snackbarOpen}
autoHideDuration={3000}
onClose={handleClose}
message={error}
action={action}
> >
<Alert onClose={handleClose} severity='error' sx={{ width: '100%' }}> <Alert onClose={() => setErrorSnackbarOpen(false)} severity='error' sx={{ width: '100%' }}>
{error} {error}
</Alert> </Alert>
</Snackbar> </Snackbar>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment