TicketCreate.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. } 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. name: 'externalReferences',
  204. type: 'hidden',
  205. },
  206. ],
  207. },
  208. ],
  209. },
  210. {
  211. isLayout: true,
  212. component: 'CommonContentPanel',
  213. children: [
  214. {
  215. isLayout: true,
  216. element: 'div',
  217. attrs: {
  218. class: 'grid grid-cols-2-uneven gap-2.5',
  219. },
  220. children: [
  221. {
  222. screen: 'create_middle',
  223. object: EnumObjectManagerObjects.Ticket,
  224. },
  225. ],
  226. },
  227. {
  228. screen: 'create_bottom',
  229. object: EnumObjectManagerObjects.Ticket,
  230. },
  231. ],
  232. },
  233. ])
  234. const securityIntegration = computed<boolean>(
  235. () =>
  236. (application.config.smime_integration ||
  237. application.config.pgp_integration) ??
  238. false,
  239. )
  240. const additionalCreateNotes = computed(
  241. () =>
  242. (application.config.ui_ticket_create_notes as Record<string, string>) || {},
  243. )
  244. const schemaData = reactive({
  245. defaultTitle,
  246. isTicketCustomer,
  247. securityIntegration,
  248. getTabLabel: (value: string) => `tab-label-${value}`,
  249. getTabPanelId: (value: string) => `tab-panel-${value}`,
  250. existingAdditionalCreateNotes: () => {
  251. return Object.keys(additionalCreateNotes).length > 0
  252. },
  253. getAdditionalCreateNote: (value: string) => {
  254. return additionalCreateNotes.value[value]
  255. },
  256. })
  257. const changedFields = reactive({
  258. // Workaround until the object attribute for body is required so core worklow is returning it correctly.
  259. body: {
  260. required: true,
  261. },
  262. })
  263. const { signatureHandling } = useTicketSignature()
  264. const sidebarContext = computed<TicketSidebarContext>(() => ({
  265. screenType: TicketSidebarScreenType.TicketCreate,
  266. form: form.value,
  267. formValues: values.value,
  268. }))
  269. useProvideTicketSidebar(sidebarContext)
  270. const { hasSidebar } = useTicketSidebar()
  271. const tabContext = computed<TaskbarTabContext>((currentContext) => {
  272. if (!isInitialSettled.value) return {}
  273. const newContext = {
  274. formValues: values.value,
  275. formIsDirty: isDirty.value,
  276. }
  277. if (currentContext && isEqual(newContext, currentContext))
  278. return currentContext
  279. return newContext
  280. })
  281. const { activeTaskbarTab, activeTaskbarTabFormId, activeTaskbarTabDelete } =
  282. useTaskbarTab(EnumTaskbarEntity.TicketCreate, tabContext)
  283. const { setSkipNextStateUpdate } = useTaskbarTabStateUpdates(triggerFormUpdater)
  284. const { waitForVariantConfirmation } = useConfirmation()
  285. const discardChanges = async () => {
  286. const confirm = await waitForVariantConfirmation('unsaved')
  287. if (confirm) {
  288. goBack()
  289. activeTaskbarTabDelete()
  290. }
  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>