-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathconfirm.store.ts
More file actions
53 lines (45 loc) · 1.27 KB
/
confirm.store.ts
File metadata and controls
53 lines (45 loc) · 1.27 KB
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
50
51
52
53
// Copyright (c) 2025 The Linux Foundation and each contributor.
// SPDX-License-Identifier: MIT
import { defineStore } from 'pinia';
import { ref } from 'vue';
export interface ConfirmOptions {
title?: string;
message: string;
confirmLabel?: string;
cancelLabel?: string;
}
const defaultOptions: ConfirmOptions = {
title: 'Confirm',
message: '',
confirmLabel: 'Ok',
cancelLabel: 'Cancel',
};
export const useConfirmStore = defineStore('confirm', () => {
const isConfirmModalOpen = ref(false);
const confirmOptions = ref<ConfirmOptions>(defaultOptions);
const resolvePromise = ref<((value: boolean) => void) | null>(null);
const openConfirmModal = (options: ConfirmOptions): Promise<boolean> => {
confirmOptions.value = { ...defaultOptions, ...options };
isConfirmModalOpen.value = true;
return new Promise((resolve) => {
resolvePromise.value = resolve;
});
};
const confirm = () => {
isConfirmModalOpen.value = false;
resolvePromise.value?.(true);
resolvePromise.value = null;
};
const cancel = () => {
isConfirmModalOpen.value = false;
resolvePromise.value?.(false);
resolvePromise.value = null;
};
return {
isConfirmModalOpen,
confirmOptions,
openConfirmModal,
confirm,
cancel,
};
});