TicketCreate.vue 13 KB

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