useTargetTicketOptions.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { storeToRefs } from 'pinia'
  3. import { computed, ref, shallowRef } from 'vue'
  4. import type {
  5. ChangedFieldFunction,
  6. FormFieldValue,
  7. } from '#shared/components/Form/types.ts'
  8. import { useApplicationStore } from '#shared/stores/application.ts'
  9. import type { TicketRelationAndRecentListItem } from '#desktop/pages/ticket/components/TicketDetailView/TicketSimpleTable/types.ts'
  10. import { getTicketNumberWithHook } from '#desktop/pages/ticket/composables/getTicketNumber.ts'
  11. export const useTargetTicketOptions = (
  12. onChangedField: ChangedFieldFunction,
  13. updateFieldValues: (values: Record<string, FormFieldValue>) => void,
  14. ) => {
  15. const { config } = storeToRefs(useApplicationStore())
  16. const targetTicketId = ref<string>()
  17. const formListTargetTicket = shallowRef<TicketRelationAndRecentListItem>()
  18. const formListTargetTicketOptions = computed(() => {
  19. if (!formListTargetTicket.value) return
  20. return [
  21. {
  22. value: formListTargetTicket.value.id,
  23. label: `${getTicketNumberWithHook(config.value.ticket_hook, formListTargetTicket.value.number)} - ${formListTargetTicket.value.title}`,
  24. heading: formListTargetTicket.value.customer.fullname,
  25. ticket: formListTargetTicket.value,
  26. },
  27. ]
  28. })
  29. onChangedField('targetTicketId', (value) => {
  30. targetTicketId.value = (value as string) ?? undefined
  31. if (formListTargetTicket.value?.id === value) return
  32. formListTargetTicket.value = undefined
  33. })
  34. const handleTicketClick = (ticket: TicketRelationAndRecentListItem) => {
  35. updateFieldValues({
  36. targetTicketId: ticket.id,
  37. })
  38. formListTargetTicket.value = ticket
  39. }
  40. return {
  41. formListTargetTicketOptions,
  42. targetTicketId,
  43. handleTicketClick,
  44. }
  45. }