useTicketInformation.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, inject, provide } from 'vue'
  3. import { useTicketQuery } from '#shared/entities/ticket/graphql/queries/ticket.api.ts'
  4. import type { TicketById } from '#shared/entities/ticket/types.ts'
  5. import { useErrorHandler } from '#shared/errors/useErrorHandler.ts'
  6. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  7. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  8. import type { TicketInformation } from '#desktop/entities/ticket/types.ts'
  9. import type { Ref, InjectionKey } from 'vue'
  10. export const TICKET_KEY = Symbol('ticket') as InjectionKey<TicketInformation>
  11. export const initializeTicketInformation = (
  12. internalId: Ref<number | string>,
  13. ) => {
  14. const ticketId = computed(() =>
  15. convertToGraphQLId('Ticket', internalId.value),
  16. )
  17. // TODO: stay with his for now, but need to be re-implemented for the tab situation.
  18. const { createQueryErrorHandler } = useErrorHandler()
  19. const ticketQuery = new QueryHandler(
  20. // Currently we need no subscribeToMore here, because the tab registration holds the ticket subscription.
  21. useTicketQuery(
  22. () => ({
  23. ticketId: ticketId.value,
  24. }),
  25. { fetchPolicy: 'cache-first' },
  26. ),
  27. {
  28. errorCallback: createQueryErrorHandler({
  29. notFound: __(
  30. 'Ticket with specified ID was not found. Try checking the URL for errors.',
  31. ),
  32. forbidden: __('You have insufficient rights to view this ticket.'),
  33. }),
  34. },
  35. )
  36. const result = ticketQuery.result()
  37. const ticket = computed(() => result.value?.ticket as TicketById)
  38. return {
  39. ticket,
  40. ticketId,
  41. ticketInternalId: internalId as Ref<number>,
  42. }
  43. }
  44. export const provideTicketInformation = (data: TicketInformation) => {
  45. provide(TICKET_KEY, data)
  46. }
  47. export const useTicketInformation = () => {
  48. return inject(TICKET_KEY) as TicketInformation
  49. }