useOrganizationDetail.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, nextTick, ref, watch } from 'vue'
  3. import { useObjectAttributes } from '#shared/entities/object-attributes/composables/useObjectAttributes.ts'
  4. import { useErrorHandler } from '#shared/errors/useErrorHandler.ts'
  5. import type {
  6. OrganizationUpdatesSubscriptionVariables,
  7. OrganizationUpdatesSubscription,
  8. } from '#shared/graphql/types.ts'
  9. import { EnumObjectManagerObjects } from '#shared/graphql/types.ts'
  10. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  11. import { useOrganizationLazyQuery } from '../graphql/queries/organization.api.ts'
  12. import { OrganizationUpdatesDocument } from '../graphql/subscriptions/organizationUpdates.api.ts'
  13. export const useOrganizationDetail = () => {
  14. const internalId = ref(0)
  15. const fetchMembersCount = ref<Maybe<number>>(3)
  16. const { createQueryErrorHandler } = useErrorHandler()
  17. const organizationQuery = new QueryHandler(
  18. useOrganizationLazyQuery(
  19. () => ({
  20. organizationInternalId: internalId.value,
  21. membersCount: 3,
  22. }),
  23. () => ({ enabled: internalId.value > 0 }),
  24. ),
  25. {
  26. errorCallback: createQueryErrorHandler({
  27. notFound: __(
  28. 'Organization with specified ID was not found. Try checking the URL for errors.',
  29. ),
  30. forbidden: __(
  31. 'You have insufficient rights to view this organization.',
  32. ),
  33. }),
  34. },
  35. )
  36. const loadOrganization = (id: number) => {
  37. internalId.value = id
  38. nextTick(() => {
  39. organizationQuery.load()
  40. })
  41. }
  42. const organizationResult = organizationQuery.result()
  43. const loading = organizationQuery.loading()
  44. const organization = computed(() => organizationResult.value?.organization)
  45. const loadAllMembers = () => {
  46. const organizationInternalId = organization.value?.internalId
  47. if (!organizationInternalId) {
  48. return
  49. }
  50. organizationQuery
  51. .refetch({
  52. organizationInternalId,
  53. membersCount: null,
  54. })
  55. .then(() => {
  56. fetchMembersCount.value = null
  57. })
  58. }
  59. const { attributes: objectAttributes } = useObjectAttributes(
  60. EnumObjectManagerObjects.Organization,
  61. )
  62. watch(
  63. () => organization.value?.id,
  64. (organizationId) => {
  65. if (!organizationId) {
  66. return
  67. }
  68. organizationQuery.subscribeToMore<
  69. OrganizationUpdatesSubscriptionVariables,
  70. OrganizationUpdatesSubscription
  71. >(() => ({
  72. document: OrganizationUpdatesDocument,
  73. variables: {
  74. organizationId,
  75. membersCount: fetchMembersCount.value,
  76. },
  77. }))
  78. },
  79. { immediate: true },
  80. )
  81. return {
  82. loading,
  83. organizationQuery,
  84. organization,
  85. objectAttributes,
  86. loadOrganization,
  87. loadAllMembers,
  88. }
  89. }