CommonConfirmationDialog.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. interface Props {
  10. uniqueId: string
  11. }
  12. const props = defineProps<Props>()
  13. const currentConfirmationOptions = computed(() => {
  14. return confirmationOptions.value?.get(props.uniqueId)
  15. })
  16. const handleConfirmation = (isCancel?: boolean) => {
  17. if (isCancel) {
  18. currentConfirmationOptions.value?.cancelCallback()
  19. } else if (isCancel === false) {
  20. currentConfirmationOptions.value?.confirmCallback()
  21. } else {
  22. currentConfirmationOptions.value?.closeCallback()
  23. }
  24. confirmationOptions.value.delete(props.uniqueId)
  25. }
  26. const confirmationVariant = computed<ConfirmationVariantOptions>(() => {
  27. switch (currentConfirmationOptions.value?.confirmationVariant) {
  28. case 'delete':
  29. return {
  30. headerTitle: __('Delete Object'),
  31. headerIcon: 'trash3',
  32. content: __('Are you sure you want to delete this object?'),
  33. footerActionOptions: {
  34. actionLabel:
  35. currentConfirmationOptions.value?.buttonLabel ||
  36. __('Delete Object'),
  37. actionButton: {
  38. variant: 'danger',
  39. },
  40. },
  41. }
  42. case 'unsaved':
  43. return {
  44. headerTitle: __('Unsaved Changes'),
  45. content: __(
  46. 'Are you sure? You have unsaved changes that will get lost.',
  47. ),
  48. footerActionOptions: {
  49. actionLabel: __('Discard Changes'),
  50. actionButton: {
  51. variant: 'danger',
  52. },
  53. },
  54. }
  55. default:
  56. return {
  57. headerTitle: __('Confirmation'),
  58. content: __('Do you want to continue?'),
  59. footerActionOptions: {
  60. actionLabel:
  61. currentConfirmationOptions.value?.buttonLabel || __('Yes'),
  62. actionButton: {
  63. variant:
  64. currentConfirmationOptions.value?.buttonVariant || 'primary',
  65. },
  66. cancelLabel: currentConfirmationOptions.value?.cancelLabel,
  67. },
  68. }
  69. }
  70. })
  71. const headerTitle = computed(() => {
  72. if (currentConfirmationOptions.value?.headerTitle) {
  73. return i18n.t(
  74. currentConfirmationOptions.value?.headerTitle,
  75. ...(currentConfirmationOptions.value?.headerTitlePlaceholder || []),
  76. )
  77. }
  78. return confirmationVariant.value.headerTitle
  79. })
  80. </script>
  81. <template>
  82. <CommonDialog
  83. :name="`confirmation:${props.uniqueId}`"
  84. :header-title="headerTitle"
  85. :header-icon="
  86. currentConfirmationOptions?.headerIcon || confirmationVariant.headerIcon
  87. "
  88. :content="currentConfirmationOptions?.text || confirmationVariant.content"
  89. :content-placeholder="currentConfirmationOptions?.textPlaceholder"
  90. :footer-action-options="confirmationVariant.footerActionOptions"
  91. global
  92. @close="handleConfirmation"
  93. />
  94. </template>