-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathuse-confirm.ts
49 lines (42 loc) · 1.42 KB
/
use-confirm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { UseConfirmDialogRevealResult, UseConfirmDialogReturn } from "@vueuse/core";
import type { Ref } from "vue";
type Store = UseConfirmDialogReturn<any, boolean, boolean> & {
text: Ref<string>;
setup: boolean;
open: (text: string) => Promise<UseConfirmDialogRevealResult<boolean, boolean>>;
};
const store: Partial<Store> = {
text: ref("Are you sure you want to delete this item? "),
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;
const { isRevealed, reveal, confirm, cancel } = useConfirmDialog<any, boolean, boolean>();
store.isRevealed = isRevealed;
store.reveal = reveal;
store.confirm = confirm;
store.cancel = cancel;
}
async function openDialog(msg: string): Promise<UseConfirmDialogRevealResult<boolean, boolean>> {
if (!store.reveal) {
throw new Error("reveal is not defined");
}
if (!store.text) {
throw new Error("text is not defined");
}
store.text.value = msg;
return await store.reveal();
}
return {
...(store as Store),
open: openDialog,
};
}