TicketCreate.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <!-- Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { isEqual } from 'lodash-es'
  4. import { computed, markRaw, reactive } from 'vue'
  5. import { useRouter, useRoute } from 'vue-router'
  6. import Form from '#shared/components/Form/Form.vue'
  7. import type { FormSubmitData } from '#shared/components/Form/types.ts'
  8. import { useForm } from '#shared/components/Form/useForm.ts'
  9. import { useConfirmation } from '#shared/composables/useConfirmation.ts'
  10. import { useTicketSignature } from '#shared/composables/useTicketSignature.ts'
  11. import { useTicketCreate } from '#shared/entities/ticket/composables/useTicketCreate.ts'
  12. import { useTicketCreateArticleType } from '#shared/entities/ticket/composables/useTicketCreateArticleType.ts'
  13. import { useTicketCreateView } from '#shared/entities/ticket/composables/useTicketCreateView.ts'
  14. import { useTicketFormOrganizationHandler } from '#shared/entities/ticket/composables/useTicketFormOrganizationHandler.ts'
  15. import type { TicketFormData } from '#shared/entities/ticket/types.ts'
  16. import { defineFormSchema } from '#shared/form/defineFormSchema.ts'
  17. import {
  18. EnumFormUpdaterId,
  19. EnumObjectManagerObjects,
  20. EnumTaskbarEntity,
  21. } from '#shared/graphql/types.ts'
  22. import { useWalker } from '#shared/router/walker.ts'
  23. import { useApplicationStore } from '#shared/stores/application.ts'
  24. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  25. import CommonContentPanel from '#desktop/components/CommonContentPanel/CommonContentPanel.vue'
  26. import LayoutContent from '#desktop/components/layout/LayoutContent.vue'
  27. import { useTaskbarTab } from '#desktop/entities/user/current/composables/useTaskbarTab.ts'
  28. import { useTaskbarTabStateUpdates } from '#desktop/entities/user/current/composables/useTaskbarTabStateUpdates.ts'
  29. import type { TaskbarTabContext } from '#desktop/entities/user/current/types.ts'
  30. import ApplyTemplate from '../components/ApplyTemplate.vue'
  31. import TicketDuplicateDetectionAlert from '../components/TicketDuplicateDetectionAlert.vue'
  32. import TicketSidebar from '../components/TicketSidebar.vue'
  33. import {
  34. useProvideTicketSidebar,
  35. useTicketSidebar,
  36. } from '../composables/useTicketSidebar.ts'
  37. import {
  38. TicketSidebarScreenType,
  39. type TicketSidebarContext,
  40. } from '../types/sidebar.ts'
  41. interface Props {
  42. tabId?: string
  43. }
  44. defineOptions({
  45. beforeRouteEnter(to) {
  46. const { ticketCreateEnabled, checkUniqueTicketCreateRoute } =
  47. useTicketCreateView()
  48. // TODO: Add real handling, when error page is available (see mobile).
  49. if (!ticketCreateEnabled.value) return '/error'
  50. return checkUniqueTicketCreateRoute(to)
  51. },
  52. beforeRouteUpdate(to) {
  53. // When route is updated we need to check again of the unique identifier.
  54. const { checkUniqueTicketCreateRoute } = useTicketCreateView()
  55. return checkUniqueTicketCreateRoute(to)
  56. },
  57. })
  58. defineProps<Props>()
  59. const router = useRouter()
  60. const walker = useWalker()
  61. const route = useRoute()
  62. const {
  63. form,
  64. isDisabled,
  65. isDirty,
  66. isInitialSettled,
  67. formNodeId,
  68. values,
  69. triggerFormUpdater,
  70. } = useForm()
  71. const application = useApplicationStore()
  72. const redirectAfterCreate = (internalId?: number) => {
  73. if (internalId) {
  74. router.replace(`/tickets/${internalId}`)
  75. } else {
  76. router.replace({ name: 'Dashboard' }) // TODO: check...?
  77. }
  78. }
  79. const goBack = () => {
  80. walker.back('/') // TODO: check what is the best fallback route path.
  81. }
  82. const { ticketArticleSenderTypeField } = useTicketCreateArticleType()
  83. const { createTicket, isTicketCustomer } = useTicketCreate(
  84. form,
  85. redirectAfterCreate,
  86. )
  87. const defaultTitle = __('New Ticket')
  88. const formSchema = defineFormSchema([
  89. {
  90. isLayout: true,
  91. component: 'CommonContentPanel',
  92. children: [
  93. {
  94. isLayout: true,
  95. element: 'h1',
  96. attrs: {
  97. class:
  98. 'py-2.5 text-center text-xl font-medium leading-snug text-black dark:text-white',
  99. ariaCurrent: 'page',
  100. },
  101. children: '$values.title || $t($defaultTitle)',
  102. },
  103. {
  104. if: '$isTicketCustomer === false',
  105. ...ticketArticleSenderTypeField,
  106. outerClass: 'flex justify-center',
  107. },
  108. {
  109. isLayout: true,
  110. element: 'div',
  111. attrs: {
  112. class: 'grid grid-cols-1 gap-2.5',
  113. role: 'tabpanel',
  114. ariaLabelledby: '$getTabLabel($values.articleSenderType)',
  115. id: '$getTabPanelId($values.articleSenderType)',
  116. },
  117. children: [
  118. {
  119. if: '$existingAdditionalCreateNotes() && $getAdditionalCreateNote($values.articleSenderType) !== undefined',
  120. isLayout: true,
  121. component: 'CommonAlert',
  122. props: {
  123. variant: 'warning',
  124. },
  125. children: '$t($getAdditionalCreateNote($values.articleSenderType))',
  126. },
  127. {
  128. if: '$values.ticket_duplicate_detection.count > 0',
  129. isLayout: true,
  130. component: 'TicketDuplicateDetectionAlert',
  131. props: {
  132. tickets: '$values.ticket_duplicate_detection.items',
  133. },
  134. children: '',
  135. },
  136. {
  137. screen: 'create_top',
  138. object: EnumObjectManagerObjects.Ticket,
  139. },
  140. // Because of the current field screen settings in the backend
  141. // seed we need to add this manually.
  142. {
  143. if: '$values.articleSenderType === "email-out"',
  144. name: 'cc',
  145. label: __('CC'),
  146. type: 'recipient',
  147. props: {
  148. multiple: true,
  149. clearable: true,
  150. },
  151. },
  152. {
  153. if: '$securityIntegration === true && $values.articleSenderType === "email-out"',
  154. name: 'security',
  155. label: __('Security'),
  156. type: 'security',
  157. },
  158. {
  159. name: 'body',
  160. screen: 'create_top',
  161. object: EnumObjectManagerObjects.TicketArticle,
  162. required: true,
  163. props: {
  164. meta: {
  165. mentionText: {
  166. customerNodeName: 'customer_id',
  167. },
  168. mentionUser: {
  169. groupNodeName: 'group_id',
  170. },
  171. mentionKnowledgeBase: {
  172. attachmentsNodeName: 'attachments',
  173. },
  174. },
  175. },
  176. },
  177. {
  178. type: 'file',
  179. name: 'attachments',
  180. label: __('Attachment'),
  181. labelSrOnly: true,
  182. props: {
  183. multiple: true,
  184. },
  185. },
  186. {
  187. name: 'ticket_duplicate_detection',
  188. type: 'hidden',
  189. value: {
  190. count: 0,
  191. items: [],
  192. },
  193. },
  194. {
  195. name: 'link_ticket_id',
  196. type: 'hidden',
  197. },
  198. {
  199. name: 'shared_draft_id',
  200. type: 'hidden',
  201. },
  202. ],
  203. },
  204. ],
  205. },
  206. {
  207. isLayout: true,
  208. component: 'CommonContentPanel',
  209. children: [
  210. {
  211. isLayout: true,
  212. element: 'div',
  213. attrs: {
  214. class: 'grid grid-cols-2-uneven gap-2.5',
  215. },
  216. children: [
  217. {
  218. screen: 'create_middle',
  219. object: EnumObjectManagerObjects.Ticket,
  220. },
  221. ],
  222. },
  223. {
  224. screen: 'create_bottom',
  225. object: EnumObjectManagerObjects.Ticket,
  226. },
  227. ],
  228. },
  229. ])
  230. const securityIntegration = computed<boolean>(
  231. () =>
  232. (application.config.smime_integration ||
  233. application.config.pgp_integration) ??
  234. false,
  235. )
  236. const additionalCreateNotes = computed(
  237. () =>
  238. (application.config.ui_ticket_create_notes as Record<string, string>) || {},
  239. )
  240. const schemaData = reactive({
  241. defaultTitle,
  242. isTicketCustomer,
  243. securityIntegration,
  244. getTabLabel: (value: string) => `tab-label-${value}`,
  245. getTabPanelId: (value: string) => `tab-panel-${value}`,
  246. existingAdditionalCreateNotes: () => {
  247. return Object.keys(additionalCreateNotes).length > 0
  248. },
  249. getAdditionalCreateNote: (value: string) => {
  250. return additionalCreateNotes.value[value]
  251. },
  252. })
  253. const changedFields = reactive({
  254. // Workaround until the object attribute for body is required so core worklow is returning it correctly.
  255. body: {
  256. required: true,
  257. },
  258. })
  259. const { signatureHandling } = useTicketSignature()
  260. const sidebarContext = computed<TicketSidebarContext>(() => ({
  261. screenType: TicketSidebarScreenType.TicketCreate,
  262. form: form.value,
  263. formValues: values.value,
  264. }))
  265. useProvideTicketSidebar(sidebarContext)
  266. const { hasSidebar } = useTicketSidebar()
  267. const tabContext = computed<TaskbarTabContext>((currentContext) => {
  268. if (!isInitialSettled.value) return {}
  269. const newContext = {
  270. formValues: values.value,
  271. formIsDirty: isDirty.value,
  272. }
  273. if (currentContext && isEqual(newContext, currentContext))
  274. return currentContext
  275. return newContext
  276. })
  277. const { activeTaskbarTab, activeTaskbarTabFormId, activeTaskbarTabDelete } =
  278. useTaskbarTab(EnumTaskbarEntity.TicketCreate, tabContext)
  279. const { setSkipNextStateUpdate } = useTaskbarTabStateUpdates(triggerFormUpdater)
  280. const { waitForVariantConfirmation } = useConfirmation()
  281. const discardChanges = async () => {
  282. const confirm = await waitForVariantConfirmation('unsaved')
  283. if (confirm) {
  284. goBack()
  285. activeTaskbarTabDelete()
  286. }
  287. }
  288. const applyTemplate = (templateId: string) => {
  289. // Skip subscription for the current tab, to avoid not needed form updater requests.
  290. setSkipNextStateUpdate(true)
  291. triggerFormUpdater({
  292. includeDirtyFields: true,
  293. additionalParams: {
  294. templateId,
  295. },
  296. })
  297. }
  298. const formAdditionalRouteQueryParams = computed(() => ({
  299. taskbarId: activeTaskbarTab.value?.taskbarTabId,
  300. ...(route.query || {}),
  301. }))
  302. const submitCreateTicket = async (event: FormSubmitData<TicketFormData>) => {
  303. createTicket(event).then((result) => {
  304. if (!result || result === null || result === undefined) return
  305. if (typeof result === 'function') result()
  306. activeTaskbarTabDelete()
  307. })
  308. }
  309. </script>
  310. <template>
  311. <LayoutContent
  312. name="ticket-create"
  313. background-variant="primary"
  314. content-alignment="center"
  315. :show-sidebar="hasSidebar"
  316. >
  317. <div class="w-full max-w-screen-xl px-28 py-3.5">
  318. <Form
  319. id="ticket-create"
  320. ref="form"
  321. :key="tabId"
  322. :form-id="activeTaskbarTabFormId"
  323. :schema="formSchema"
  324. :schema-component-library="{
  325. CommonContentPanel: markRaw(CommonContentPanel),
  326. TicketDuplicateDetectionAlert: markRaw(TicketDuplicateDetectionAlert),
  327. }"
  328. :schema-data="schemaData"
  329. :form-updater-id="EnumFormUpdaterId.FormUpdaterUpdaterTicketCreate"
  330. :handlers="[
  331. useTicketFormOrganizationHandler(),
  332. signatureHandling('body'),
  333. ]"
  334. :change-fields="changedFields"
  335. :form-updater-additional-params="formAdditionalRouteQueryParams"
  336. use-object-attributes
  337. form-class="flex flex-col gap-3"
  338. @submit="submitCreateTicket($event as FormSubmitData<TicketFormData>)"
  339. @changed="setSkipNextStateUpdate(true)"
  340. />
  341. </div>
  342. <template #sideBar="{ isCollapsed, toggleCollapse }">
  343. <TicketSidebar
  344. :context="sidebarContext"
  345. :is-collapsed="isCollapsed"
  346. :toggle-collapse="toggleCollapse"
  347. />
  348. </template>
  349. <template #bottomBar>
  350. <template v-if="isInitialSettled">
  351. <CommonButton
  352. v-if="isDirty"
  353. size="large"
  354. variant="danger"
  355. :disabled="isDisabled"
  356. @click="discardChanges"
  357. >{{ __('Discard Changes') }}</CommonButton
  358. >
  359. <CommonButton v-else size="large" variant="secondary" @click="goBack">{{
  360. __('Cancel & Go Back')
  361. }}</CommonButton>
  362. </template>
  363. <ApplyTemplate @select-template="applyTemplate" />
  364. <CommonButton
  365. size="large"
  366. variant="submit"
  367. type="submit"
  368. :form="formNodeId"
  369. :disabled="isDisabled"
  370. >{{ __('Create') }}</CommonButton
  371. >
  372. </template>
  373. </LayoutContent>
  374. </template>