CommonConfirmationDialog.vue 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed } from 'vue'
  4. import { useConfirmation } from '#shared/composables/useConfirmation.ts'
  5. import { i18n } from '#shared/i18n.ts'
  6. import CommonDialog from '#desktop/components/CommonDialog/CommonDialog.vue'
  7. import type { ConfirmationVariantOptions } from './types.ts'
  8. const { confirmationOptions } = useConfirmation()
  9. const handleConfirmation = (isCancel?: boolean) => {
  10. if (isCancel) {
  11. confirmationOptions.value?.cancelCallback()
  12. } else if (isCancel === false) {
  13. confirmationOptions.value?.confirmCallback()
  14. } else {
  15. confirmationOptions.value?.closeCallback()
  16. }
  17. confirmationOptions.value = undefined
  18. }
  19. const confirmationVariant = computed<ConfirmationVariantOptions>(() => {
  20. switch (confirmationOptions.value?.confirmationVariant) {
  21. case 'delete':
  22. return {
  23. headerTitle: __('Delete Object'),
  24. headerIcon: 'trash3',
  25. content: __('Are you sure you want to delete this object?'),
  26. footerActionOptions: {
  27. actionLabel:
  28. confirmationOptions.value?.buttonLabel || __('Delete Object'),
  29. actionButton: {
  30. variant: 'danger',
  31. },
  32. },
  33. }
  34. case 'unsaved':
  35. return {
  36. headerTitle: __('Unsaved Changes'),
  37. content: __(
  38. 'Are you sure? You have unsaved changes that will get lost.',
  39. ),
  40. footerActionOptions: {
  41. actionLabel: __('Discard Changes'),
  42. actionButton: {
  43. variant: 'danger',
  44. },
  45. },
  46. }
  47. default:
  48. return {
  49. headerTitle: __('Confirmation'),
  50. content: __('Do you want to continue?'),
  51. footerActionOptions: {
  52. actionLabel: confirmationOptions.value?.buttonLabel || __('Yes'),
  53. actionButton: {
  54. variant: confirmationOptions.value?.buttonVariant || 'primary',
  55. },
  56. cancelLabel: confirmationOptions.value?.cancelLabel,
  57. },
  58. }
  59. }
  60. })
  61. const headerTitle = computed(() => {
  62. if (confirmationOptions.value?.headerTitle) {
  63. return i18n.t(
  64. confirmationOptions.value?.headerTitle,
  65. ...(confirmationOptions.value?.headerTitlePlaceholder || []),
  66. )
  67. }
  68. return confirmationVariant.value.headerTitle
  69. })
  70. </script>
  71. <template>
  72. <CommonDialog
  73. name="confirmation"
  74. :header-title="headerTitle"
  75. :header-icon="
  76. confirmationOptions?.headerIcon || confirmationVariant.headerIcon
  77. "
  78. :content="confirmationOptions?.text || confirmationVariant.content"
  79. :content-placeholder="confirmationOptions?.textPlaceholder"
  80. :footer-action-options="confirmationVariant.footerActionOptions"
  81. @close="handleConfirmation"
  82. ></CommonDialog>
  83. </template>