1 |
f2db3e05
|
Vaclav Honzik
|
import { Typography } from '@mui/material'
|
2 |
|
|
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
|
3 |
|
|
import { useDispatch } from 'react-redux'
|
4 |
|
|
|
5 |
|
|
export interface ConsumeLastErrorProps {
|
6 |
|
|
// Observable error from the store
|
7 |
|
|
err?: string
|
8 |
|
|
// Consume function that clears the error in the store
|
9 |
|
|
consumeFn: () => void
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
// Utility component that consumes the last error from the store and shows it
|
13 |
|
|
const ConsumeLastError: FunctionComponent<ConsumeLastErrorProps> = ({
|
14 |
|
|
err: error,
|
15 |
|
|
consumeFn,
|
16 |
|
|
}) => {
|
17 |
|
|
const dispatch = useDispatch()
|
18 |
|
|
const [err, setErr] = useState('')
|
19 |
|
|
|
20 |
|
|
useEffect(() => {
|
21 |
|
|
if (error) {
|
22 |
|
|
setErr(`${error}`)
|
23 |
|
|
dispatch(consumeFn())
|
24 |
|
|
}
|
25 |
|
|
}, [consumeFn, dispatch, error])
|
26 |
|
|
|
27 |
|
|
return <Fragment>
|
28 |
|
|
{err ? <Typography variant="h6" fontWeight="400">{err}</Typography> : null}
|
29 |
|
|
</Fragment>
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
export default ConsumeLastError
|