TicketActionChangeCustomerDialog.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <!-- Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import Form from '@shared/components/Form/Form.vue'
  4. import { type FormData, useForm } from '@shared/components/Form'
  5. import {
  6. EnumObjectManagerObjects,
  7. type TicketCustomerUpdateInput,
  8. } from '@shared/graphql/types'
  9. import { closeDialog } from '@shared/composables/useDialog'
  10. import { useTicketFormOganizationHandler } from '@shared/entities/ticket/composables/useTicketFormOrganizationHandler'
  11. import { MutationHandler } from '@shared/server/apollo/handler'
  12. import { convertToGraphQLId } from '@shared/graphql/utils'
  13. import {
  14. NotificationTypes,
  15. useNotifications,
  16. } from '@shared/components/CommonNotifications'
  17. import UserError from '@shared/errors/UserError'
  18. import { useTicketCustomerUpdateMutation } from '@shared/entities/ticket/graphql/mutations/customerUpdate.api'
  19. import CommonDialog from '@mobile/components/CommonDialog/CommonDialog.vue'
  20. import { defineFormSchema } from '@mobile/form/defineFormSchema'
  21. import type { TicketById } from '@shared/entities/ticket/types'
  22. // No usage of "type" because of: https://github.com/typescript-eslint/typescript-eslint/issues/5468
  23. import { TicketCustomerUpdateFormData } from '@shared/entities/ticket/types'
  24. import { useConfirmationDialog } from '@mobile/components/CommonConfirmation'
  25. export interface Props {
  26. name: string
  27. ticket: TicketById
  28. }
  29. const props = defineProps<Props>()
  30. const { form, isDirty, isDisabled, canSubmit } = useForm()
  31. const { waitForConfirmation } = useConfirmationDialog()
  32. const cancelDialog = async () => {
  33. if (isDirty.value) {
  34. const confirmed = await waitForConfirmation(
  35. __('Are you sure? You have unsaved changes that will get lost.'),
  36. )
  37. if (!confirmed) return
  38. }
  39. closeDialog(props.name)
  40. }
  41. const formSchema = defineFormSchema([
  42. {
  43. name: 'customer_id',
  44. screen: 'edit',
  45. object: EnumObjectManagerObjects.Ticket,
  46. required: true,
  47. },
  48. {
  49. name: 'organization_id',
  50. screen: 'edit',
  51. object: EnumObjectManagerObjects.Ticket,
  52. },
  53. ])
  54. const changeCustomerMutation = new MutationHandler(
  55. useTicketCustomerUpdateMutation({}),
  56. )
  57. const { notify } = useNotifications()
  58. const changeCustomer = async (
  59. formData: FormData<TicketCustomerUpdateFormData>,
  60. ) => {
  61. const input = {
  62. customerId: convertToGraphQLId('User', formData.customer_id),
  63. } as TicketCustomerUpdateInput
  64. if (formData.organization_id) {
  65. input.organizationId = convertToGraphQLId(
  66. 'Organization',
  67. formData.organization_id,
  68. )
  69. }
  70. try {
  71. const result = await changeCustomerMutation.send({
  72. ticketId: props.ticket.id,
  73. input,
  74. })
  75. if (result) {
  76. closeDialog(props.name)
  77. notify({
  78. type: NotificationTypes.Success,
  79. message: __('Ticket customer updated successfully.'),
  80. })
  81. }
  82. } catch (errors) {
  83. if (errors instanceof UserError) {
  84. notify({
  85. message: errors.generalErrors[0],
  86. type: NotificationTypes.Error,
  87. })
  88. }
  89. }
  90. }
  91. </script>
  92. <template>
  93. <CommonDialog
  94. class="w-full"
  95. no-autofocus
  96. :name="name"
  97. :label="__('Change customer')"
  98. >
  99. <template #before-label>
  100. <button
  101. class="text-white"
  102. :class="{ 'opacity-50': isDisabled }"
  103. @click="cancelDialog"
  104. >
  105. {{ $t('Cancel') }}
  106. </button>
  107. </template>
  108. <template #after-label>
  109. <button
  110. :form="name"
  111. class="text-blue"
  112. :disabled="!canSubmit"
  113. :class="{ 'opacity-50': !canSubmit }"
  114. >
  115. {{ $t('Save') }}
  116. </button>
  117. </template>
  118. <Form
  119. :id="name"
  120. ref="form"
  121. class="w-full p-4"
  122. autofocus
  123. :schema="formSchema"
  124. :handlers="[useTicketFormOganizationHandler()]"
  125. :initial-entity-object="ticket"
  126. use-object-attributes
  127. @submit="changeCustomer($event as FormData<TicketCustomerUpdateFormData>)"
  128. />
  129. </CommonDialog>
  130. </template>