import React from 'react';
import { Alert, Snackbar } from '@mui/material';
import { useAppDispatch, useAppSelector } from '../hooks';
import { hideSnackbar, selectSnackbarState } from '../slices/snackbarSlice';

function GlobalSnackbar() {
  const dispatch = useAppDispatch();
  const { open, message, severity } = useAppSelector(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;
