useOnlineNotificationActions.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { cloneDeep } from 'lodash-es'
  3. import { useOnlineNotificationDeleteMutation } from '#shared/entities/online-notification/graphql/mutations/delete.api.ts'
  4. import { useOnlineNotificationMarkAllAsSeenMutation } from '#shared/entities/online-notification/graphql/mutations/markAllAsSeen.api.ts'
  5. import { useOnlineNotificationSeenMutation } from '#shared/entities/online-notification/graphql/mutations/seen.api.ts'
  6. import { OnlineNotificationsDocument } from '#shared/entities/online-notification/graphql/queries/onlineNotifications.api.ts'
  7. import type {
  8. OnlineNotificationsQuery,
  9. Scalars,
  10. } from '#shared/graphql/types.ts'
  11. import { getApolloClient } from '#shared/server/apollo/client.ts'
  12. import { MutationHandler } from '#shared/server/apollo/handler/index.ts'
  13. export const useOnlineNotificationActions = () => {
  14. const { cache } = getApolloClient()
  15. const getCacheData = () => {
  16. const queryOptions = {
  17. query: OnlineNotificationsDocument,
  18. }
  19. const existingQueryCache =
  20. cache.readQuery<OnlineNotificationsQuery>(queryOptions)
  21. if (!existingQueryCache?.onlineNotifications) return null
  22. const oldQueryCache = cloneDeep(existingQueryCache)
  23. return { queryOptions, oldQueryCache, existingQueryCache }
  24. }
  25. const removeNotificationCacheUpdate = (id: Scalars['ID']['output']) => {
  26. const data = getCacheData()
  27. if (!data) return
  28. const { queryOptions, oldQueryCache, existingQueryCache } = data
  29. cache.writeQuery({
  30. ...queryOptions,
  31. data: {
  32. onlineNotifications: {
  33. edges: existingQueryCache.onlineNotifications.edges.filter(
  34. (edge) => edge.node.id !== id,
  35. ),
  36. pageInfo: existingQueryCache.onlineNotifications.pageInfo,
  37. },
  38. },
  39. })
  40. return () => {
  41. cache.writeQuery({
  42. ...queryOptions,
  43. data: oldQueryCache,
  44. })
  45. }
  46. }
  47. const updateAllSeenNotificationCache = (ids: Scalars['ID']['output'][]) => {
  48. const data = getCacheData()
  49. if (!data) return
  50. const { queryOptions, oldQueryCache, existingQueryCache } = data
  51. const clonedQueryCache = cloneDeep(existingQueryCache)
  52. ids.forEach((id) =>
  53. clonedQueryCache.onlineNotifications.edges.forEach(({ node }) => {
  54. if (node.id === id) {
  55. node.seen = true
  56. }
  57. }),
  58. )
  59. cache.writeQuery({
  60. ...queryOptions,
  61. data: {
  62. onlineNotifications: {
  63. ...clonedQueryCache.onlineNotifications,
  64. },
  65. },
  66. })
  67. return () => {
  68. cache.writeQuery({
  69. ...queryOptions,
  70. data: oldQueryCache,
  71. })
  72. }
  73. }
  74. const seenNotificationMutation = new MutationHandler(
  75. useOnlineNotificationSeenMutation(),
  76. {
  77. errorNotificationMessage: __(
  78. 'The online notification could not be marked as seen.',
  79. ),
  80. },
  81. )
  82. const seenNotification = async (id: Scalars['ID']['output']) =>
  83. seenNotificationMutation.send({ objectId: id })
  84. const markAllSeenMutation = new MutationHandler(
  85. useOnlineNotificationMarkAllAsSeenMutation(),
  86. {
  87. errorNotificationMessage: __('Cannot set online notifications as seen'),
  88. },
  89. )
  90. const markAllRead = (ids: Scalars['ID']['output'][]) => {
  91. const revertCache = updateAllSeenNotificationCache(ids)
  92. return markAllSeenMutation
  93. .send({ onlineNotificationIds: ids })
  94. .catch(() => revertCache)
  95. }
  96. const deleteNotificationMutation = new MutationHandler(
  97. useOnlineNotificationDeleteMutation(),
  98. )
  99. const deleteNotification = async (id: Scalars['ID']['output']) => {
  100. const revertCache = removeNotificationCacheUpdate(id)
  101. return deleteNotificationMutation
  102. .send({
  103. onlineNotificationId: id,
  104. })
  105. .catch(() => revertCache)
  106. }
  107. return {
  108. seenNotification,
  109. deleteNotification,
  110. deleteNotificationMutation,
  111. markAllRead,
  112. }
  113. }