TicketCreate.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. // - handover context to useTaskbarTab composable
  45. // - Default output for TicketCreate-TabEntity without a "entity/state"
  46. defineOptions({
  47. beforeRouteEnter(to) {
  48. const { ticketCreateEnabled, checkUniqueTicketCreateRoute } =
  49. useTicketCreateView()
  50. // TODO: Add real handling, when error page is available (see mobile).
  51. if (!ticketCreateEnabled.value) return '/error'
  52. return checkUniqueTicketCreateRoute(to)
  53. },
  54. beforeRouteUpdate(to) {
  55. // When route is updated we need to check again of the unique identifier.
  56. const { checkUniqueTicketCreateRoute } = useTicketCreateView()
  57. return checkUniqueTicketCreateRoute(to)
  58. },
  59. })
  60. defineProps<Props>()
  61. const router = useRouter()
  62. const walker = useWalker()
  63. const route = useRoute()
  64. const {
  65. form,
  66. isDisabled,
  67. isDirty,
  68. isInitialSettled,
  69. formNodeId,
  70. values,
  71. triggerFormUpdater,
  72. } = useForm()
  73. const application = useApplicationStore()
  74. const redirectAfterCreate = (internalId?: number) => {
  75. if (internalId) {
  76. router.replace(`/tickets/${internalId}`)
  77. } else {
  78. router.replace({ name: 'Dashboard' }) // TODO: check...?
  79. }
  80. }
  81. const goBack = () => {
  82. walker.back('/') // TODO: check what is the best fallback route path.
  83. }
  84. const { ticketArticleSenderTypeField } = useTicketCreateArticleType()
  85. const { createTicket, isTicketCustomer } = useTicketCreate(
  86. form,
  87. redirectAfterCreate,
  88. )
  89. const defaultTitle = __('New Ticket')
  90. const formSchema = defineFormSchema([
  91. {
  92. isLayout: true,
  93. component: 'CommonContentPanel',
  94. children: [
  95. {
  96. isLayout: true,
  97. element: 'h1',
  98. attrs: {
  99. class:
  100. 'py-2.5 text-center text-xl font-medium leading-snug text-black dark:text-white',
  101. ariaCurrent: 'page',
  102. },
  103. children: '$values.title || $t($defaultTitle)',
  104. },
  105. {
  106. if: '$isTicketCustomer === false',
  107. ...ticketArticleSenderTypeField,
  108. outerClass: 'flex justify-center',
  109. },
  110. {
  111. isLayout: true,
  112. element: 'div',
  113. attrs: {
  114. class: 'grid grid-cols-1 gap-2.5',
  115. role: 'tabpanel',
  116. ariaLabelledby: '$getTabLabel($values.articleSenderType)',
  117. id: '$getTabPanelId($values.articleSenderType)',
  118. },
  119. children: [
  120. {
  121. if: '$existingAdditionalCreateNotes() && $getAdditionalCreateNote($values.articleSenderType) !== undefined',
  122. isLayout: true,
  123. component: 'CommonAlert',
  124. props: {
  125. variant: 'warning',
  126. },
  127. children: '$t($getAdditionalCreateNote($values.articleSenderType))',
  128. },
  129. {
  130. if: '$values.ticket_duplicate_detection.count > 0',
  131. isLayout: true,
  132. component: 'TicketDuplicateDetectionAlert',
  133. props: {
  134. tickets: '$values.ticket_duplicate_detection.items',
  135. },
  136. children: '',
  137. },
  138. {
  139. screen: 'create_top',
  140. object: EnumObjectManagerObjects.Ticket,
  141. },
  142. // Because of the current field screen settings in the backend
  143. // seed we need to add this manually.
  144. {
  145. if: '$values.articleSenderType === "email-out"',
  146. name: 'cc',
  147. label: __('CC'),
  148. type: 'recipient',
  149. props: {
  150. multiple: true,
  151. clearable: true,
  152. },
  153. },
  154. {
  155. if: '$securityIntegration === true && $values.articleSenderType === "email-out"',
  156. name: 'security',
  157. label: __('Security'),
  158. type: 'security',
  159. },
  160. {
  161. name: 'body',
  162. screen: 'create_top',
  163. object: EnumObjectManagerObjects.TicketArticle,
  164. required: true,
  165. props: {
  166. meta: {
  167. mentionText: {
  168. customerNodeName: 'customer_id',
  169. },
  170. mentionUser: {
  171. groupNodeName: 'group_id',
  172. },
  173. mentionKnowledgeBase: {
  174. attachmentsNodeName: 'attachments',
  175. },
  176. },
  177. },
  178. },
  179. {
  180. type: 'file',
  181. name: 'attachments',
  182. label: __('Attachment'),
  183. labelSrOnly: true,
  184. props: {
  185. multiple: true,
  186. },
  187. },
  188. {
  189. name: 'ticket_duplicate_detection',
  190. type: 'hidden',
  191. value: {
  192. count: 0,
  193. items: [],
  194. },
  195. },
  196. {
  197. name: 'link_ticket_id',
  198. type: 'hidden',
  199. },
  200. {
  201. name: 'shared_draft_id',
  202. type: 'hidden',
  203. },
  204. ],
  205. },
  206. ],
  207. },
  208. {
  209. isLayout: true,
  210. component: 'CommonContentPanel',
  211. children: [
  212. {
  213. isLayout: true,
  214. element: 'div',
  215. attrs: {
  216. class: 'grid grid-cols-2-uneven gap-2.5',
  217. },
  218. children: [
  219. {
  220. screen: 'create_middle',
  221. object: EnumObjectManagerObjects.Ticket,
  222. },
  223. ],
  224. },
  225. {
  226. screen: 'create_bottom',
  227. object: EnumObjectManagerObjects.Ticket,
  228. },
  229. ],
  230. },
  231. ])
  232. const securityIntegration = computed<boolean>(
  233. () =>
  234. (application.config.smime_integration ||
  235. application.config.pgp_integration) ??
  236. false,
  237. )
  238. const additionalCreateNotes = computed(
  239. () =>
  240. (application.config.ui_ticket_create_notes as Record<string, string>) || {},
  241. )
  242. const schemaData = reactive({
  243. defaultTitle,
  244. isTicketCustomer,
  245. securityIntegration,
  246. getTabLabel: (value: string) => `tab-label-${value}`,
  247. getTabPanelId: (value: string) => `tab-panel-${value}`,
  248. existingAdditionalCreateNotes: () => {
  249. return Object.keys(additionalCreateNotes).length > 0
  250. },
  251. getAdditionalCreateNote: (value: string) => {
  252. return additionalCreateNotes.value[value]
  253. },
  254. })
  255. const changedFields = reactive({
  256. // Workaround until the object attribute for body is required so core worklow is returning it correctly.
  257. body: {
  258. required: true,
  259. },
  260. })
  261. const { signatureHandling } = useTicketSignature()
  262. const sidebarContext = computed<TicketSidebarContext>(() => ({
  263. screenType: TicketSidebarScreenType.TicketCreate,
  264. form: form.value,
  265. formValues: values.value,
  266. }))
  267. useProvideTicketSidebar(sidebarContext)
  268. const { hasSidebar } = useTicketSidebar()
  269. const tabContext = computed<TaskbarTabContext>((currentContext) => {
  270. if (!isInitialSettled.value) return {}
  271. const newContext = {
  272. formValues: values.value,
  273. formIsDirty: isDirty.value,
  274. }
  275. if (currentContext && isEqual(newContext, currentContext))
  276. return currentContext
  277. return newContext
  278. })
  279. const { activeTaskbarTab, activeTaskbarTabFormId, activeTaskbarTabDelete } =
  280. useTaskbarTab(EnumTaskbarEntity.TicketCreate, tabContext)
  281. const { setSkipNextStateUpdate } = useTaskbarTabStateUpdates(triggerFormUpdater)
  282. const { waitForVariantConfirmation } = useConfirmation()
  283. const discardChanges = async () => {
  284. const confirm = await waitForVariantConfirmation('unsaved')
  285. if (confirm) {
  286. goBack()
  287. activeTaskbarTabDelete()
  288. }
  289. }
  290. const applyTemplate = (templateId: string) => {
  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-2xl px-28 pt-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>