2022-09-25 22:33:13 +00:00
|
|
|
import { UseConfirmDialogRevealResult, UseConfirmDialogReturn } from "@vueuse/core";
|
2022-09-09 22:46:53 +00:00
|
|
|
import { Ref } from "vue";
|
2022-09-01 22:32:03 +00:00
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
type Store = UseConfirmDialogReturn<any, boolean, boolean> & {
|
2022-09-01 22:32:03 +00:00
|
|
|
text: Ref<string>;
|
|
|
|
setup: boolean;
|
2022-09-27 23:52:13 +00:00
|
|
|
open: (text: string) => Promise<UseConfirmDialogRevealResult<boolean, boolean>>;
|
2022-09-01 22:32:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const store: Partial<Store> = {
|
2022-09-09 22:46:53 +00:00
|
|
|
text: ref("Are you sure you want to delete this item? "),
|
2022-09-01 22:32:03 +00:00
|
|
|
setup: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used to wrap the ModalConfirmation which is a "Singleton" component
|
|
|
|
* that is used to confirm actions. It's mounded once on the root of the page and reused
|
|
|
|
* for every confirmation action that is required.
|
|
|
|
*
|
|
|
|
* This is in an experimental phase of development and may have unknown or unexpected side effects.
|
|
|
|
*/
|
|
|
|
export function useConfirm(): Store {
|
|
|
|
if (!store.setup) {
|
|
|
|
store.setup = true;
|
2023-02-18 06:41:01 +00:00
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
const { isRevealed, reveal, confirm, cancel } = useConfirmDialog<any, boolean, boolean>();
|
2022-09-01 22:32:03 +00:00
|
|
|
store.isRevealed = isRevealed;
|
|
|
|
store.reveal = reveal;
|
|
|
|
store.confirm = confirm;
|
|
|
|
store.cancel = cancel;
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
async function openDialog(msg: string): Promise<UseConfirmDialogRevealResult<boolean, boolean>> {
|
2022-12-30 01:19:15 +00:00
|
|
|
if (!store.reveal) {
|
|
|
|
throw new Error("reveal is not defined");
|
|
|
|
}
|
|
|
|
if (!store.text) {
|
|
|
|
throw new Error("text is not defined");
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:32:03 +00:00
|
|
|
store.text.value = msg;
|
|
|
|
return await store.reveal();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...(store as Store),
|
2022-09-25 22:33:13 +00:00
|
|
|
open: openDialog,
|
2022-09-01 22:32:03 +00:00
|
|
|
};
|
|
|
|
}
|