NotificationsList.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed, ref } from 'vue'
  4. import { useOnlineNotificationActions } from '#shared/entities/online-notification/composables/useOnlineNotificationActions.ts'
  5. import { useOnlineNotificationCount } from '#shared/entities/online-notification/composables/useOnlineNotificationCount.ts'
  6. import { useOnlineNotificationsQuery } from '#shared/entities/online-notification/graphql/queries/onlineNotifications.api.ts'
  7. import type { OnlineNotification, Scalars } from '#shared/graphql/types.ts'
  8. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  9. import { edgesToArray } from '#shared/utils/helpers.ts'
  10. import CommonLoader from '#mobile/components/CommonLoader/CommonLoader.vue'
  11. import { useHeader } from '#mobile/composables/useHeader.ts'
  12. import NotificationItem from '../components/NotificationItem.vue'
  13. const notificationsHandler = new QueryHandler(useOnlineNotificationsQuery())
  14. const loading = notificationsHandler.loading()
  15. const notificationsResult = notificationsHandler.result()
  16. let mutationTriggered = false
  17. useHeader({
  18. backUrl: '/',
  19. backAvoidHomeButton: true,
  20. refetch: computed(() => loading.value && notificationsResult.value != null),
  21. })
  22. const notifications = computed(
  23. () =>
  24. edgesToArray(
  25. notificationsResult.value?.onlineNotifications,
  26. ) as OnlineNotification[],
  27. )
  28. const { seenNotification, markAllRead } = useOnlineNotificationActions()
  29. const runSeenNotification = (id: Scalars['ID']['output']) => {
  30. mutationTriggered = true
  31. seenNotification(id)
  32. }
  33. const markingAsSeen = ref(false)
  34. const runMarkAllRead = async () => {
  35. markingAsSeen.value = true
  36. const onlineNotificationIds = notifications.value
  37. .filter((elem) => !elem.seen)
  38. .map((elem) => elem.id)
  39. mutationTriggered = true
  40. await markAllRead(onlineNotificationIds)
  41. markingAsSeen.value = false
  42. }
  43. const notificationRemoved = () => {
  44. mutationTriggered = true
  45. }
  46. // TODO: currently this triggered in some situtations a real subscription on the server: https://github.com/apollographql/apollo-client/issues/10117
  47. const { unseenCount, notificationsCountSubscription } =
  48. useOnlineNotificationCount()
  49. notificationsCountSubscription.watchOnResult(() => {
  50. notificationsHandler.refetch()
  51. if (!mutationTriggered) notificationsHandler.refetch()
  52. mutationTriggered = false
  53. })
  54. const haveUnread = computed(() => unseenCount.value > 0)
  55. </script>
  56. <template>
  57. <CommonLoader :loading="!notifications.length && loading">
  58. <div class="ltr:pl-3 ltr:pr-4 rtl:pl-4 rtl:pr-3">
  59. <NotificationItem
  60. v-for="notification of notifications"
  61. :key="notification.id"
  62. :activity="notification"
  63. @remove="notificationRemoved"
  64. @seen="runSeenNotification"
  65. />
  66. <div v-if="!notifications.length" class="px-4 py-3 text-center text-base">
  67. {{ $t('No entries') }}
  68. </div>
  69. <!-- TODO: Add some better solution when mark as seen is running.
  70. Maybe disabled state that it can not be clicked twice or hidding the action completley. -->
  71. <div
  72. v-if="haveUnread"
  73. class="text-blue flex flex-1 cursor-pointer justify-center px-4 py-3 text-base"
  74. :class="{ 'text-red': markingAsSeen }"
  75. role="button"
  76. tabindex="0"
  77. @keydown.enter="runMarkAllRead"
  78. @click="runMarkAllRead"
  79. >
  80. {{ $t('Mark all as read') }}
  81. </div>
  82. </div>
  83. </CommonLoader>
  84. </template>