import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Alert, Snackbar } from '@mui/material';
import { hideSnackbar, selectSnackbarState } from '../slices/snackbarSlice';

function GlobalSnackbar() {
  const dispatch = useDispatch();
  const { open, message, severity } = useSelector(selectSnackbarState);

  const handleClose = () => {
    dispatch(hideSnackbar());
  };

  return (
    <Snackbar open={open} onClose={handleClose} autoHideDuration={5000}>
      <div>
        <Alert severity={severity} onClose={handleClose}>
          {message}
        </Alert>
      </div>
    </Snackbar>
  );
}

export default GlobalSnackbar;
