Projekt

Obecné

Profil

Stáhnout (1.83 KB) Statistiky
| Větev: | Tag: | Revize:
1 4f42fa52 Václav Honzík
import { Alert, AlertColor, Snackbar } from '@mui/material'
2
import { Fragment, useEffect, useState } from 'react'
3
import { useDispatch, useSelector } from 'react-redux'
4
import { RootState } from '../redux/store'
5
import { consumeNotification } from './notificationSlice'
6
7
// Represents notification component that will be displayed on the screen
8
const Notification = () => {
9
    const dispatch = useDispatch()
10
    const notification = useSelector((state: RootState) => state.notification)
11
12
    const [displayMessage, setDisplayMessage] = useState('')
13
    const [open, setOpen] = useState(false)
14
    const [severity, setSeverity] = useState<AlertColor>('info')
15
    const [autohideDuration, setAutohideDuration] = useState<number | null>(
16
        null
17
    )
18
19
    const closeNotification = () => {
20
        setOpen(false)
21
        setAutohideDuration(null)
22
    }
23
24
    // Set the message to be displayed if something is set
25
    useEffect(() => {
26
        if (notification.message) {
27
            setDisplayMessage(notification.message)
28
            setSeverity(notification.severity as AlertColor)
29
            if (notification.autohideSecs) {
30
                setAutohideDuration(notification.autohideSecs * 1000)
31
            }
32
            // Consume the message from store
33
            dispatch(consumeNotification())
34
35
            // Show the message in the notification
36
            setOpen(true)
37
        }
38
    }, [notification, dispatch])
39
40
    return (
41
        <Fragment>
42
            <Snackbar
43
                open={open}
44
                autoHideDuration={autohideDuration}
45
                anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
46
            >
47
                <Alert severity={severity} onClose={closeNotification}>
48
                    {displayMessage}
49
                </Alert>
50
            </Snackbar>
51
        </Fragment>
52
    )
53
}
54
55
export default Notification