2025-08-05 14:12:51 +03:00
|
|
|
import ReactBasicWidget from "../react/ReactBasicWidget";
|
|
|
|
|
import Modal from "../react/Modal";
|
|
|
|
|
import Button from "../react/Button";
|
|
|
|
|
import { closeActiveDialog, openDialog } from "../../services/dialog";
|
|
|
|
|
import { t } from "../../services/i18n";
|
|
|
|
|
import { useState } from "react";
|
2025-08-05 15:13:09 +03:00
|
|
|
import FormCheckbox from "../react/FormCheckbox";
|
2025-08-05 14:12:51 +03:00
|
|
|
|
|
|
|
|
interface ConfirmDialogProps {
|
2025-08-05 15:13:09 +03:00
|
|
|
title?: string;
|
|
|
|
|
message?: string | HTMLElement;
|
2025-08-05 14:12:51 +03:00
|
|
|
callback?: ConfirmDialogCallback;
|
|
|
|
|
lastElementToFocus?: HTMLElement | null;
|
2025-08-05 15:13:09 +03:00
|
|
|
isConfirmDeleteNoteBox?: boolean;
|
2025-08-05 14:12:51 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-05 15:13:09 +03:00
|
|
|
function ConfirmDialogComponent({ title, message, callback, lastElementToFocus, isConfirmDeleteNoteBox }: ConfirmDialogProps) {
|
2025-08-05 14:12:51 +03:00
|
|
|
const [ confirmed, setConfirmed ] = useState<boolean>(false);
|
2025-08-05 15:13:09 +03:00
|
|
|
const [ isDeleteNoteChecked, setIsDeleteNoteChecked ] = useState<boolean>(false);
|
2025-08-05 14:12:51 +03:00
|
|
|
|
2025-08-05 15:13:09 +03:00
|
|
|
return (
|
2025-08-05 14:12:51 +03:00
|
|
|
<Modal
|
2025-08-05 15:13:09 +03:00
|
|
|
title={title ?? t("confirm.confirmation")}
|
2025-08-05 14:12:51 +03:00
|
|
|
size="md"
|
|
|
|
|
zIndex={2000}
|
|
|
|
|
scrollable={true}
|
|
|
|
|
onHidden={() => {
|
|
|
|
|
callback?.({
|
|
|
|
|
confirmed,
|
2025-08-05 15:13:09 +03:00
|
|
|
isDeleteNoteChecked
|
2025-08-05 14:12:51 +03:00
|
|
|
});
|
|
|
|
|
lastElementToFocus?.focus();
|
|
|
|
|
}}
|
|
|
|
|
footer={<>
|
|
|
|
|
<Button text={t("confirm.cancel")} onClick={() => closeActiveDialog()} />
|
|
|
|
|
<Button text={t("confirm.ok")} onClick={() => {
|
|
|
|
|
setConfirmed(true);
|
|
|
|
|
closeActiveDialog();
|
|
|
|
|
}} />
|
|
|
|
|
</>}
|
|
|
|
|
>
|
2025-08-05 15:13:09 +03:00
|
|
|
{!message || typeof message === "string"
|
|
|
|
|
? <div>{(message as string) ?? ""}</div>
|
2025-08-05 14:12:51 +03:00
|
|
|
: <div dangerouslySetInnerHTML={{ __html: message.outerHTML ?? "" }} />}
|
2025-08-05 15:13:09 +03:00
|
|
|
|
|
|
|
|
{isConfirmDeleteNoteBox && (
|
|
|
|
|
<FormCheckbox
|
|
|
|
|
name="confirm-dialog-delete-note"
|
|
|
|
|
label={t("confirm.also_delete_note")}
|
|
|
|
|
hint={t("confirm.if_you_dont_check")}
|
|
|
|
|
currentValue={isDeleteNoteChecked} onChange={setIsDeleteNoteChecked} />
|
|
|
|
|
)}
|
2025-08-05 14:12:51 +03:00
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type ConfirmDialogResult = false | ConfirmDialogOptions;
|
|
|
|
|
export type ConfirmDialogCallback = (val?: ConfirmDialogResult) => void;
|
|
|
|
|
type MessageType = string | HTMLElement | JQuery<HTMLElement>;
|
|
|
|
|
|
|
|
|
|
export interface ConfirmDialogOptions {
|
|
|
|
|
confirmed: boolean;
|
|
|
|
|
isDeleteNoteChecked: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ConfirmWithMessageOptions {
|
|
|
|
|
message: MessageType;
|
|
|
|
|
callback: ConfirmDialogCallback;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 15:13:09 +03:00
|
|
|
// For "showConfirmDialog"
|
|
|
|
|
export interface ConfirmWithTitleOptions {
|
|
|
|
|
title: string;
|
|
|
|
|
callback: ConfirmDialogCallback;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 14:12:51 +03:00
|
|
|
export default class ConfirmDialog extends ReactBasicWidget {
|
|
|
|
|
|
|
|
|
|
private props: ConfirmDialogProps = {};
|
|
|
|
|
|
|
|
|
|
get component() {
|
|
|
|
|
return <ConfirmDialogComponent {...this.props} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showConfirmDialogEvent({ message, callback }: ConfirmWithMessageOptions) {
|
2025-08-05 15:13:09 +03:00
|
|
|
this.showDialog(null, message, callback, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showConfirmDeleteNoteBoxWithNoteDialogEvent({ title, callback }: ConfirmWithTitleOptions) {
|
|
|
|
|
const message = t("confirm.are_you_sure_remove_note", { title: title });
|
|
|
|
|
this.showDialog(title, message, callback, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private showDialog(title: string | null, message: MessageType, callback: ConfirmDialogCallback, isConfirmDeleteNoteBox: boolean) {
|
2025-08-05 14:12:51 +03:00
|
|
|
this.props = {
|
2025-08-05 15:13:09 +03:00
|
|
|
title: title,
|
2025-08-05 14:12:51 +03:00
|
|
|
message: (typeof message === "object" && "length" in message ? message[0] : message),
|
|
|
|
|
lastElementToFocus: (document.activeElement as HTMLElement),
|
2025-08-05 15:13:09 +03:00
|
|
|
callback,
|
|
|
|
|
isConfirmDeleteNoteBox
|
2025-08-05 14:12:51 +03:00
|
|
|
};
|
|
|
|
|
this.doRender();
|
|
|
|
|
openDialog(this.$widget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|