useUserDetail.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { UserUpdatesDocument } from '@shared/graphql/subscriptions/userUpdates.api'
  3. import type {
  4. UserUpdatesSubscriptionVariables,
  5. UserUpdatesSubscription,
  6. } from '@shared/graphql/types'
  7. import { QueryHandler } from '@shared/server/apollo/handler'
  8. import { computed, nextTick, ref, watch } from 'vue'
  9. import { useUserObjectAttributesStore } from '@shared/entities/user/stores/objectAttributes'
  10. import { useUserLazyQuery } from '../graphql/queries/user.api'
  11. export const useUserDetail = () => {
  12. const internalId = ref(0)
  13. const userQuery = new QueryHandler(
  14. useUserLazyQuery(
  15. () => ({
  16. userInternalId: internalId.value,
  17. secondaryOrganizationsCount: 3,
  18. }),
  19. () => ({ enabled: internalId.value > 0 }),
  20. ),
  21. )
  22. const loadUser = (id: number) => {
  23. internalId.value = id
  24. nextTick(() => {
  25. userQuery.load()
  26. })
  27. }
  28. const loadAllSecondaryOrganizations = () => {
  29. userQuery.refetch({
  30. userInternalId: internalId.value,
  31. secondaryOrganizationsCount: null,
  32. })
  33. }
  34. const userResult = userQuery.result()
  35. const loading = userQuery.loading()
  36. const user = computed(() => userResult.value?.user)
  37. const objectAttributesManager = useUserObjectAttributesStore()
  38. const objectAttributes = computed(
  39. () => objectAttributesManager.viewScreenAttributes || [],
  40. )
  41. watch(
  42. () => user.value?.id,
  43. (userId) => {
  44. if (!userId) return
  45. userQuery.subscribeToMore<
  46. UserUpdatesSubscriptionVariables,
  47. UserUpdatesSubscription
  48. >({
  49. document: UserUpdatesDocument,
  50. variables: {
  51. userId,
  52. },
  53. })
  54. },
  55. { immediate: true },
  56. )
  57. return {
  58. loading,
  59. user,
  60. objectAttributes,
  61. loadAllSecondaryOrganizations,
  62. loadUser,
  63. }
  64. }