|
| 1 | +import * as React from 'react' |
| 2 | +import { Box, Button, Snackbar } from '@mui/material' |
| 3 | +import MuiAlert, { AlertProps } from '@mui/material/Alert' |
| 4 | +import config from '../../config' |
| 5 | +import axios from 'axios' |
| 6 | + |
| 7 | +const Alert = React.forwardRef<HTMLDivElement, AlertProps>(function Alert( |
| 8 | + props, |
| 9 | + ref |
| 10 | +) { |
| 11 | + return <MuiAlert elevation={6} ref={ref} variant="filled" {...props} /> |
| 12 | +}) |
| 13 | + |
| 14 | +export default function FlushCacheButton() { |
| 15 | + const [open, setOpen] = React.useState(false) |
| 16 | + // const [loading, setLoading] = React.useState(false) |
| 17 | + // const [success, setSuccess] = React.useState(false) |
| 18 | + const [error, setError] = React.useState(false) |
| 19 | + |
| 20 | + /** |
| 21 | + * Call the backend api endpoint to flush the backend-cache |
| 22 | + */ |
| 23 | + const flushCache = () => { |
| 24 | + const url = config.backendUrl + '/api/cache/flush' |
| 25 | + axios |
| 26 | + .get(url) |
| 27 | + .then((response) => { |
| 28 | + if (response.status !== 200) { |
| 29 | + setError(true) |
| 30 | + } |
| 31 | + setError(false) |
| 32 | + }) |
| 33 | + .catch((error) => { |
| 34 | + console.log(error.message) |
| 35 | + setError(true) |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + const handleClick = () => { |
| 40 | + flushCache() |
| 41 | + setOpen(true) |
| 42 | + } |
| 43 | + |
| 44 | + const handleClose = ( |
| 45 | + event?: React.SyntheticEvent | Event, |
| 46 | + reason?: string |
| 47 | + ) => { |
| 48 | + if (reason === 'clickaway') { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + setOpen(false) |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <Box sx={{ m: 1, position: 'absolute' }} className="flush-cache-container"> |
| 57 | + <Button |
| 58 | + variant="contained" |
| 59 | + onClick={handleClick} |
| 60 | + className="flush-cache-button" |
| 61 | + > |
| 62 | + Flush Cache |
| 63 | + </Button> |
| 64 | + <Snackbar |
| 65 | + open={open} |
| 66 | + autoHideDuration={3000} |
| 67 | + onClose={handleClose} |
| 68 | + anchorOrigin={{ vertical: 'top', horizontal: 'right' }} |
| 69 | + > |
| 70 | + {error ? ( |
| 71 | + <Alert severity="error" onClose={handleClose} sx={{ width: '100%' }}> |
| 72 | + Error when flush the cache |
| 73 | + </Alert> |
| 74 | + ) : ( |
| 75 | + <Alert |
| 76 | + onClose={handleClose} |
| 77 | + severity="success" |
| 78 | + sx={{ width: '100%' }} |
| 79 | + > |
| 80 | + Cache flushed! |
| 81 | + </Alert> |
| 82 | + )} |
| 83 | + </Snackbar> |
| 84 | + </Box> |
| 85 | + ) |
| 86 | +} |
0 commit comments