Move confirm dialog to its own service

This commit is contained in:
Ramon Wenger 2023-05-02 16:22:43 +02:00
parent ba2d4df639
commit 0ac4f7b395
3 changed files with 62 additions and 50 deletions

View File

@ -1,5 +1,5 @@
<template>
<Dialog :open="isOpen" class="relative z-50" @close="close">
<Dialog :open="true" class="relative z-50" @close="close">
<div class="fixed inset-0 bg-black/30" aria-hidden="true"></div>
<div class="fixed inset-0 flex items-center justify-center p-4">
<DialogPanel class="w-full max-w-2xl bg-white pt-8">

View File

@ -48,29 +48,6 @@
/>
</template>
</ItModal>
<ItModal v-model="showDeleteModal">
<template #title>{{ $t("circlePage.documents.deleteModalTitle") }}</template>
<template #body>
<p
class="mb-4"
v-html="$t('circlePage.documents.deleteModalWarning', { title: 'bla' })"
/>
</template>
<template #footer>
<div class="flex flex-row-reverse gap-x-4 border-t border-t-gray-500 p-4">
<button class="btn-primary">Löschen</button>
<button class="btn-secondary">Abbrechen</button>
</div>
</template>
</ItModal>
<ConfirmDialog
:is-open="showConfirm"
:title="$t('circlePage.documents.deleteModalTitle')"
:content="$t('circlePage.documents.deleteModalWarning', { title: documentName })"
@close="close"
@confirm="confirm"
/>
<div
v-if="courseSessionsStore.canUploadCircleDocuments"
class="mt-8 flex flex-col gap-y-4 border p-6"
@ -94,10 +71,13 @@ import { uploadCircleDocument } from "@/services/files";
import { useCircleStore } from "@/stores/circle";
import { useCourseSessionsStore } from "@/stores/courseSessions";
import type { CircleDocument, DocumentUploadData } from "@/types";
import dialog from "@/utils/confirm-dialog";
import log from "loglevel";
import { computed, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import DocumentListItem from "./DocumentListItem.vue";
import DocumentUploadForm from "./DocumentUploadForm.vue";
const courseSessionsStore = useCourseSessionsStore();
const circleStore = useCircleStore();
const showUploadModal = ref(false);
@ -110,33 +90,20 @@ const dropdownLearningSequences = computed(() =>
name: sequence.title,
}))
);
// confirm dialog
// todo: refactor to own service
type ResolveReject = (v: unknown) => void;
const showConfirm = ref(false);
const confirm = ref<ResolveReject | undefined>();
const close = ref<ResolveReject | undefined>();
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) {

View File

@ -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;
},
};