2020-09-25 13:17:55 +00:00
|
|
|
import React, { ReactElement, ReactNode, useState } from "react";
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
|
|
export const useConfirmDialog = (
|
|
|
|
props: ConfirmDialogProps
|
|
|
|
): [() => void, () => ReactElement] => {
|
|
|
|
const [show, setShow] = useState(false);
|
|
|
|
|
|
|
|
function toggleDialog() {
|
|
|
|
setShow((show) => !show);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Dialog = () => (
|
|
|
|
<ConfirmDialogModal
|
|
|
|
key="confirmDialog"
|
|
|
|
{...props}
|
|
|
|
open={show}
|
|
|
|
toggleDialog={toggleDialog}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
return [toggleDialog, Dialog];
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface ConfirmDialogModalProps extends ConfirmDialogProps {
|
|
|
|
open: boolean;
|
|
|
|
toggleDialog: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ConfirmDialogProps = {
|
|
|
|
titleKey: string;
|
|
|
|
messageKey?: string;
|
|
|
|
cancelButtonLabel?: string;
|
|
|
|
continueButtonLabel?: string;
|
|
|
|
continueButtonVariant?: ButtonVariant;
|
2020-10-21 12:18:41 +00:00
|
|
|
variant?: ModalVariant;
|
2020-09-25 13:17:55 +00:00
|
|
|
onConfirm: () => void;
|
|
|
|
onCancel?: () => void;
|
|
|
|
children?: ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ConfirmDialogModal = ({
|
|
|
|
titleKey,
|
|
|
|
messageKey,
|
|
|
|
cancelButtonLabel,
|
|
|
|
continueButtonLabel,
|
|
|
|
continueButtonVariant,
|
|
|
|
onConfirm,
|
|
|
|
onCancel,
|
|
|
|
children,
|
|
|
|
open = true,
|
2020-11-02 20:15:09 +00:00
|
|
|
variant = ModalVariant.small,
|
2020-09-25 13:17:55 +00:00
|
|
|
toggleDialog,
|
|
|
|
}: ConfirmDialogModalProps) => {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title={t(titleKey)}
|
|
|
|
isOpen={open}
|
|
|
|
onClose={toggleDialog}
|
2020-10-21 12:18:41 +00:00
|
|
|
variant={variant}
|
2020-09-25 13:17:55 +00:00
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
id="modal-confirm"
|
2021-04-14 18:19:39 +00:00
|
|
|
data-testid="modalConfirm"
|
2020-09-25 13:17:55 +00:00
|
|
|
key="confirm"
|
|
|
|
variant={continueButtonVariant || ButtonVariant.primary}
|
|
|
|
onClick={() => {
|
|
|
|
onConfirm();
|
|
|
|
toggleDialog();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t(continueButtonLabel || "common:continue")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
|
|
|
key="cancel"
|
2021-01-08 18:04:58 +00:00
|
|
|
variant={ButtonVariant.link}
|
2020-09-25 13:17:55 +00:00
|
|
|
onClick={() => {
|
|
|
|
if (onCancel) onCancel();
|
|
|
|
toggleDialog();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t(cancelButtonLabel || "common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{!messageKey && children}
|
|
|
|
{messageKey && t(messageKey)}
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|