From 0ac4f7b395cd84b6ea911ec5d7e978c9705fdb4a Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Tue, 2 May 2023 16:22:43 +0200 Subject: [PATCH] Move confirm dialog to its own service --- client/src/components/ui/ConfirmDialog.vue | 2 +- .../circlePage/DocumentSection.vue | 65 +++++-------------- client/src/utils/confirm-dialog.ts | 45 +++++++++++++ 3 files changed, 62 insertions(+), 50 deletions(-) create mode 100644 client/src/utils/confirm-dialog.ts diff --git a/client/src/components/ui/ConfirmDialog.vue b/client/src/components/ui/ConfirmDialog.vue index ea04a72b..b138b1e8 100644 --- a/client/src/components/ui/ConfirmDialog.vue +++ b/client/src/components/ui/ConfirmDialog.vue @@ -1,5 +1,5 @@ - - - - - - -
name: sequence.title, })) ); + // confirm dialog -// todo: refactor to own service -type ResolveReject = (v: unknown) => void; -const showConfirm = ref(false); -const confirm = ref(); -const close = ref(); -const documentName = ref(""); -const deleteDocument = (doc: CircleDocument) => { - showConfirm.value = true; - documentName.value = doc.name; - const modalDialog = new Promise((resolve, reject) => { - confirm.value = resolve; - close.value = reject; - }); - modalDialog - .then( - () => { - courseSessionsStore.removeDocument(doc.id); - showConfirm.value = false; - }, - () => { - showConfirm.value = false; - } - ) - .finally(() => { - documentName.value = ""; - }); +const { t } = useI18n(); +const deleteDocument = async (doc: CircleDocument) => { + const options = { + title: t("circlePage.documents.deleteModalTitle"), + content: t("circlePage.documents.deleteModalWarning", { title: doc.name }), + }; + try { + await dialog.confirm(options); + courseSessionsStore.removeDocument(doc.id); + } catch (e) { + log.debug("rejected"); + } }; watch(showUploadModal, () => (showUploadErrorMessage.value = false)); async function uploadDocument(data: DocumentUploadData) { diff --git a/client/src/utils/confirm-dialog.ts b/client/src/utils/confirm-dialog.ts new file mode 100644 index 00000000..a3ab6a6d --- /dev/null +++ b/client/src/utils/confirm-dialog.ts @@ -0,0 +1,45 @@ +import ConfirmDialog from "@/components/ui/ConfirmDialog.vue"; +import { createApp } from "vue"; + +interface ConfirmDialogOptions { + title: string; + content: string; +} +type ResolveReject = (v?: unknown) => void; + +export default { + confirm(options: ConfirmDialogOptions) { + const mountEl = document.createElement("div"); + document.body.appendChild(mountEl); + + let _resolve: ResolveReject, _reject: ResolveReject; + + const promise = new Promise((resolve, reject) => { + _resolve = resolve; + _reject = reject; + }); + + const cleanUp = () => { + mountEl?.parentNode?.removeChild(mountEl); + dialog.unmount(); + }; + + const dialog = createApp(ConfirmDialog, { + isOpen: true, + title: options.title, + content: options.content, + onClose() { + console.log("close"); + cleanUp(); + _reject(); + }, + onConfirm() { + console.log("confirm"); + cleanUp(); + _resolve(); + }, + }); + dialog.mount(mountEl); + return promise; + }, +};