useTicketLiveUser.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { noop } from '@vueuse/shared'
  3. import { type Ref, type ComputedRef, onBeforeMount } from 'vue'
  4. import { watch } from 'vue'
  5. import { onBeforeRouteLeave, onBeforeRouteUpdate } from 'vue-router'
  6. import { useTicketLiveUserList } from '#shared/entities/ticket/composables/useTicketLiveUserList.ts'
  7. import { EnumTaskbarApp } from '#shared/graphql/types.ts'
  8. import { ensureGraphqlId } from '#shared/graphql/utils.ts'
  9. import { MutationHandler } from '#shared/server/apollo/handler/index.ts'
  10. import { useTicketLiveUserDeleteMutation } from '../graphql/mutations/live-user/delete.api.ts'
  11. import { useTicketLiveUserUpsertMutation } from '../graphql/mutations/live-user/ticketLiveUserUpsert.api.ts'
  12. export const useTicketLiveUser = (
  13. ticketInternalId: Ref<string>,
  14. isTicketAgent: ComputedRef<boolean>,
  15. editingForm: ComputedRef<boolean>,
  16. ) => {
  17. const { liveUserList } = useTicketLiveUserList(
  18. ticketInternalId,
  19. isTicketAgent,
  20. EnumTaskbarApp.Mobile,
  21. )
  22. const upsertMutation = new MutationHandler(useTicketLiveUserUpsertMutation())
  23. const deleteMutation = new MutationHandler(useTicketLiveUserDeleteMutation())
  24. const updateLiveUser = async (ticketInternalId: string, editing = false) => {
  25. await upsertMutation
  26. .send({
  27. id: ensureGraphqlId('Ticket', ticketInternalId),
  28. editing,
  29. app: EnumTaskbarApp.Mobile,
  30. })
  31. .catch(noop)
  32. }
  33. const deleteLiveUser = async (ticketInternalId: string) => {
  34. await deleteMutation
  35. .send({
  36. id: ensureGraphqlId('Ticket', ticketInternalId),
  37. app: EnumTaskbarApp.Mobile,
  38. })
  39. .catch(noop)
  40. }
  41. onBeforeRouteUpdate(async (to, from) => {
  42. const internalToId = to.params.internalId as string
  43. const internalFromId = from.params.internalId as string
  44. // update status when opening another ticket page without unmounting the page and don't block the page
  45. if (internalToId !== internalFromId) {
  46. liveUserList.value = []
  47. deleteLiveUser(internalFromId)
  48. updateLiveUser(internalToId)
  49. }
  50. })
  51. onBeforeRouteLeave(async (_, from) => {
  52. const internalId = from.params.internalId as string
  53. // update status when leaving to non-ticket page, but don't block the page
  54. deleteLiveUser(internalId)
  55. })
  56. onBeforeMount(async () => {
  57. // update status on opening the page. it is possible that this code will run,
  58. // when user doesn't have access to the ticket, because we fail after the route is rendered
  59. await updateLiveUser(ticketInternalId.value)
  60. })
  61. // Update live user editing status, when can submit value changes
  62. watch(editingForm, async (canSubmit) => {
  63. await updateLiveUser(ticketInternalId.value, canSubmit)
  64. })
  65. return {
  66. liveUserList,
  67. }
  68. }