71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
import { useState } from "react";
|
|
import Button from "@mui/material/Button";
|
|
import Dialog from "@mui/material/Dialog";
|
|
import DialogTitle from "@mui/material/DialogTitle";
|
|
import DialogContent from "@mui/material/DialogContent";
|
|
import DialogActions from "@mui/material/DialogActions";
|
|
import { ApiNotice } from "./ApiNotice";
|
|
export function ConfirmAction({
|
|
label,
|
|
message,
|
|
onConfirm,
|
|
disabled = false,
|
|
}: {
|
|
label: string;
|
|
message: string;
|
|
onConfirm: () => Promise<unknown>;
|
|
disabled?: boolean;
|
|
}) {
|
|
const [open, setOpen] = useState(false),
|
|
[busy, setBusy] = useState(false),
|
|
[error, setError] = useState<unknown>(null);
|
|
return (
|
|
<>
|
|
<Button
|
|
disabled={disabled}
|
|
onClick={() => {
|
|
setError(null);
|
|
setOpen(true);
|
|
}}
|
|
>
|
|
{label}
|
|
</Button>
|
|
<Dialog
|
|
open={open}
|
|
onClose={() => {
|
|
if (!busy) setOpen(false);
|
|
}}
|
|
aria-labelledby="confirm-title"
|
|
>
|
|
<DialogTitle id="confirm-title">{label}</DialogTitle>
|
|
<DialogContent>
|
|
<p>{message}</p>
|
|
<ApiNotice error={error} />
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button disabled={busy} onClick={() => setOpen(false)}>
|
|
Keep current state
|
|
</Button>
|
|
<Button
|
|
disabled={busy}
|
|
variant="contained"
|
|
onClick={async () => {
|
|
setBusy(true);
|
|
try {
|
|
await onConfirm();
|
|
setOpen(false);
|
|
} catch (error) {
|
|
setError(error);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}}
|
|
>
|
|
{busy ? "Working…" : "Confirm"}
|
|
</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
</>
|
|
);
|
|
}
|