TicketCreate.vue 12 KB

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