initializeConfirmationDialog.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { watch } from 'vue'
  3. import { useRoute } from 'vue-router'
  4. import { useConfirmation } from '#shared/composables/useConfirmation.ts'
  5. import { closeDialog, useDialog } from '../CommonDialog/useDialog.ts'
  6. const confirmationDialogPerRoute = new Map<string, Set<string>>()
  7. export const initializeConfirmationDialog = () => {
  8. const { triggerConfirmation, lastConfirmationUuid } = useConfirmation()
  9. const route = useRoute()
  10. const confirmationDialog = useDialog({
  11. name: 'confirmation',
  12. component: () => import('./CommonConfirmationDialog.vue'),
  13. global: true,
  14. afterClose: (uniqueId) => {
  15. if (!uniqueId) return
  16. const dialogs = confirmationDialogPerRoute.get(route.path)
  17. if (!dialogs) return
  18. dialogs.delete(uniqueId)
  19. if (dialogs.size === 0) {
  20. confirmationDialogPerRoute.delete(route.path)
  21. }
  22. },
  23. })
  24. watch(triggerConfirmation, () => {
  25. if (!lastConfirmationUuid.value) return
  26. if (!confirmationDialogPerRoute.has(route.path)) {
  27. confirmationDialogPerRoute.set(route.path, new Set<string>())
  28. }
  29. confirmationDialogPerRoute.get(route.path)!.add(lastConfirmationUuid.value)
  30. confirmationDialog.open({
  31. uniqueId: lastConfirmationUuid.value,
  32. })
  33. })
  34. }
  35. export const cleanupRouteDialogs = async (routePath: string) => {
  36. const dialogs = confirmationDialogPerRoute.get(routePath)
  37. if (!dialogs || dialogs.size === 0) return
  38. // Convert the set to an array, then map over it
  39. const closePromises = Array.from(dialogs).map((dialogUuid) =>
  40. closeDialog(`confirmation:${dialogUuid}`, true),
  41. )
  42. await Promise.all(closePromises)
  43. }