useTicketSharedDraftStart.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, type Ref } from 'vue'
  3. import {
  4. type TicketSharedDraftStartListQuery,
  5. type TicketSharedDraftStartUpdateByGroupSubscription,
  6. type TicketSharedDraftStartUpdateByGroupSubscriptionVariables,
  7. } from '#shared/graphql/types.ts'
  8. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  9. import type { GraphQLHandlerError } from '#shared/types/error.ts'
  10. import { useTicketSharedDraftStartListQuery } from '../graphql/queries/ticketSharedDraftStartList.api.ts'
  11. import { TicketSharedDraftStartUpdateByGroupDocument } from '../graphql/subscriptions/ticketSharedDraftStartUpdateByGroup.api.ts'
  12. export const useTicketSharedDraftStart = (
  13. groupId: Ref<string>,
  14. errorCallback?: (error: GraphQLHandlerError) => boolean,
  15. ) => {
  16. const sharedDraftStartListQuery = new QueryHandler(
  17. useTicketSharedDraftStartListQuery(
  18. () => ({
  19. groupId: groupId.value,
  20. }),
  21. // Commented out because cache-first policy breaks subscribing to updates.
  22. {
  23. fetchPolicy: 'cache-first',
  24. },
  25. ),
  26. {
  27. errorCallback,
  28. },
  29. )
  30. const sharedDraftStartListResult = sharedDraftStartListQuery.result()
  31. const loading = sharedDraftStartListQuery.loading()
  32. const sharedDraftStartList = computed(
  33. () => sharedDraftStartListResult.value?.ticketSharedDraftStartList,
  34. )
  35. sharedDraftStartListQuery.subscribeToMore<
  36. TicketSharedDraftStartUpdateByGroupSubscriptionVariables,
  37. TicketSharedDraftStartUpdateByGroupSubscription
  38. >(() => ({
  39. document: TicketSharedDraftStartUpdateByGroupDocument,
  40. variables: {
  41. groupId: groupId.value,
  42. },
  43. updateQuery: (_, { subscriptionData }) => {
  44. if (
  45. !subscriptionData.data?.ticketSharedDraftStartUpdateByGroup
  46. ?.sharedDraftStarts
  47. ) {
  48. return null as unknown as TicketSharedDraftStartListQuery
  49. }
  50. return {
  51. ticketSharedDraftStartList:
  52. subscriptionData.data.ticketSharedDraftStartUpdateByGroup
  53. .sharedDraftStarts,
  54. }
  55. },
  56. }))
  57. return {
  58. loading,
  59. sharedDraftStartList,
  60. sharedDraftStartListQuery,
  61. }
  62. }