1 |
cba453f2
|
Vaclav Honzik
|
import { Button, Dialog, DialogContent, DialogTitle, Stack, Typography } from '@mui/material'
|
2 |
|
|
import { Fragment, FunctionComponent } from 'react'
|
3 |
|
|
import CancelIcon from '@mui/icons-material/Cancel'
|
4 |
|
|
import CheckIcon from '@mui/icons-material/Check'
|
5 |
|
|
|
6 |
|
|
export interface ConfirmationDialogProps {
|
7 |
|
|
open: boolean
|
8 |
|
|
setOpen: (open: boolean) => void
|
9 |
|
|
onConfirmCallback?: () => void
|
10 |
|
|
onCancelCallback?: () => void
|
11 |
|
|
title: string
|
12 |
|
|
secondaryText?: string
|
13 |
|
|
confirmText?: string
|
14 |
|
|
cancelText?: string
|
15 |
|
|
maxWidth?: 'xs' | 'sm' | 'md' | 'lg'
|
16 |
|
|
}
|
17 |
|
|
|
18 |
|
|
/**
|
19 |
|
|
* Reusable confirmation dialog that runs specified onConfirmCallback when confirmed
|
20 |
|
|
*/
|
21 |
|
|
const ConfirmationDialog: FunctionComponent<ConfirmationDialogProps> = (
|
22 |
|
|
props: ConfirmationDialogProps
|
23 |
|
|
) => {
|
24 |
|
|
const { open, setOpen, onConfirmCallback, onCancelCallback, title, secondaryText } = props
|
25 |
|
|
const cancelText = props.cancelText ?? 'Cancel'
|
26 |
|
|
const confirmText = props.confirmText ?? 'Confirm'
|
27 |
|
|
|
28 |
|
|
// function run after the dialog is confirmed
|
29 |
|
|
const onConfirm = () => {
|
30 |
|
|
if (onConfirmCallback) {
|
31 |
|
|
onConfirmCallback()
|
32 |
|
|
}
|
33 |
|
|
setOpen(false)
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
const onCancel = () => {
|
37 |
|
|
if (onCancelCallback) {
|
38 |
|
|
onCancelCallback()
|
39 |
|
|
}
|
40 |
|
|
setOpen(false)
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
return (
|
44 |
|
|
<Fragment>
|
45 |
|
|
<Dialog
|
46 |
|
|
open={open}
|
47 |
|
|
fullWidth
|
48 |
|
|
onClose={() => setOpen(false)}
|
49 |
|
|
maxWidth={props.maxWidth ?? 'lg'}
|
50 |
|
|
>
|
51 |
0654438f
|
Vaclav Honzik
|
<DialogTitle sx={{pb: 1}}>{title}</DialogTitle>
|
52 |
cba453f2
|
Vaclav Honzik
|
<DialogContent>
|
53 |
0654438f
|
Vaclav Honzik
|
<Typography sx={{mb: 1.5}}>{secondaryText}</Typography>
|
54 |
cba453f2
|
Vaclav Honzik
|
<Stack direction="row" spacing={1} justifyContent="flex-end" alignItems="center">
|
55 |
|
|
<Button variant="contained" startIcon={<CancelIcon />} onClick={onCancel}>{cancelText}</Button>
|
56 |
|
|
<Button variant="contained" startIcon={<CheckIcon />} onClick={onConfirm}>{confirmText}</Button>
|
57 |
|
|
</Stack>
|
58 |
|
|
</DialogContent>
|
59 |
|
|
</Dialog>
|
60 |
|
|
</Fragment>
|
61 |
|
|
)
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
export default ConfirmationDialog
|