conflict-nuxt-4/app/composables/useConfirm.ts
2026-02-12 11:24:27 +03:30

46 lines
1.4 KiB
TypeScript
Executable File

// ~/composables/confirm.ts
import { reactive, readonly } from "vue";
// یک state مشترک برای تمام کامپوننت‌ها
const confirmState = reactive({
isOpen: false,
title: "تأیید عملیات",
message: "آیا از انجام این عملیات اطمینان دارید؟",
resolve: null as ((value: boolean) => void) | null,
});
export const useConfirmState = () => readonly(confirmState);
export const useConfirm = () => {
const ucoShowConfirmModal = (
options: { title?: string; message?: string } = {}
): Promise<boolean> => {
confirmState.title = options.title || "تأیید عملیات";
confirmState.message =
options.message || "آیا از انجام این عملیات اطمینان دارید؟";
confirmState.isOpen = true;
return new Promise((resolve) => {
confirmState.resolve = resolve;
});
};
return { ucoShowConfirmModal };
};
export const useConfirmActions = () => {
const ucoConfirm = () => {
confirmState.isOpen = false;
if (confirmState.resolve) confirmState.resolve(true);
confirmState.resolve = null;
};
const ucoCancelConfirmModal = () => {
confirmState.isOpen = false;
if (confirmState.resolve) confirmState.resolve(false);
confirmState.resolve = null;
};
return { ucoConfirm, ucoCancelConfirmModal };
};