useApplyTemplate.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed } from 'vue'
  3. import {
  4. type TemplatesQuery,
  5. type TemplateUpdatesSubscription,
  6. type TemplateUpdatesSubscriptionVariables,
  7. } from '#shared/graphql/types.ts'
  8. import QueryHandler from '#shared/server/apollo/handler/QueryHandler.ts'
  9. import { useSessionStore } from '#shared/stores/session.ts'
  10. import { useTemplatesQuery } from '../graphql/queries/templates.api.ts'
  11. import { TemplateUpdatesDocument } from '../graphql/subscriptions/templateUpdates.api.ts'
  12. export const useApplyTemplate = () => {
  13. const session = useSessionStore()
  14. const templateListQuery = new QueryHandler(
  15. useTemplatesQuery(
  16. () => ({
  17. onlyActive: true,
  18. }),
  19. () => ({ enabled: session.hasPermission('ticket.agent') }),
  20. ),
  21. )
  22. templateListQuery.subscribeToMore<
  23. TemplateUpdatesSubscriptionVariables,
  24. TemplateUpdatesSubscription
  25. >({
  26. document: TemplateUpdatesDocument,
  27. variables: {
  28. onlyActive: true,
  29. },
  30. updateQuery: (prev, { subscriptionData }) => {
  31. if (!subscriptionData.data?.templateUpdates.templates) {
  32. return null as unknown as TemplatesQuery
  33. }
  34. return {
  35. templates: subscriptionData.data.templateUpdates.templates,
  36. }
  37. },
  38. })
  39. const result = templateListQuery.result()
  40. const templateList = computed(() => result.value?.templates || [])
  41. return {
  42. templateList,
  43. }
  44. }