import React, { createContext, useContext, useState, useCallback } from 'react';
|
import { Snackbar, Alert, Portal, Slide } from '@mui/material';
|
|
const NotificationContext = createContext();
|
|
const SlideTransition = (props) => {
|
return <Slide {...props} direction="up" />;
|
}
|
|
export const NotificationProvider = ({ children }) => {
|
const [notification, setNotification] = useState({
|
open: false,
|
message: '',
|
severity: 'info',
|
});
|
|
const showNotification = useCallback((message, severity = 'info') => {
|
setNotification({ open: true, message, severity });
|
}, []);
|
|
const handleClose = () => {
|
setNotification({ ...notification, open: false });
|
};
|
|
return (
|
<NotificationContext.Provider value={showNotification}>
|
{children}
|
<Portal>
|
<Snackbar
|
open={notification.open}
|
autoHideDuration={4000}
|
onClose={handleClose}
|
TransitionComponent={SlideTransition}
|
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
|
sx={{
|
// mt: 12,
|
zIndex: 2000,
|
}}
|
>
|
<Alert onClose={handleClose} severity={notification.severity} sx={{ width: '300px', zIndex: 2000 }}>
|
{notification.message}
|
</Alert>
|
</Snackbar>
|
</Portal>
|
</NotificationContext.Provider>
|
);
|
};
|
|
export const useNotification = () => {
|
return useContext(NotificationContext);
|
};
|