NotificationItem.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import ActivityMessage from '#shared/components/ActivityMessage/ActivityMessage.vue'
  4. import type { AvatarUser } from '#shared/components/CommonUserAvatar/types.ts'
  5. import { useOnlineNotificationDeleteMutation } from '#shared/entities/online-notification/graphql/mutations/delete.api.ts'
  6. import type {
  7. ActivityMessageMetaObject,
  8. Scalars,
  9. } from '#shared/graphql/types.ts'
  10. import { MutationHandler } from '#shared/server/apollo/handler/index.ts'
  11. import type { ApolloCache, InMemoryCache } from '@apollo/client/core'
  12. export interface Props {
  13. itemId: Scalars['ID']['output']
  14. objectName: string
  15. typeName: string
  16. seen: boolean
  17. metaObject?: Maybe<ActivityMessageMetaObject>
  18. createdAt: string
  19. createdBy?: Maybe<AvatarUser>
  20. }
  21. const props = defineProps<Props>()
  22. const emit = defineEmits<{
  23. remove: [id: Scalars['ID']['output']]
  24. seen: [id: Scalars['ID']['output']]
  25. }>()
  26. const updateCacheAfterRemoving = (
  27. cache: ApolloCache<InMemoryCache>,
  28. id: Scalars['ID']['output'],
  29. ) => {
  30. const normalizedId = cache.identify({ id, __typename: 'OnlineNotification' })
  31. cache.evict({ id: normalizedId })
  32. cache.gc()
  33. }
  34. const removeNotificationMutation = new MutationHandler(
  35. useOnlineNotificationDeleteMutation(() => ({
  36. variables: { onlineNotificationId: props.itemId },
  37. update(cache) {
  38. updateCacheAfterRemoving(cache, props.itemId)
  39. },
  40. })),
  41. {
  42. errorNotificationMessage: __('The notifcation could not be deleted.'),
  43. },
  44. )
  45. const loading = removeNotificationMutation.loading()
  46. const removeNotification = () => {
  47. emit('remove', props.itemId)
  48. return removeNotificationMutation.send()
  49. }
  50. </script>
  51. <template>
  52. <div class="flex">
  53. <button
  54. class="flex items-center ltr:pr-2 rtl:pl-2"
  55. :class="{ 'cursor-pointer': !loading, 'opacity-50': loading }"
  56. :disabled="loading"
  57. @click="removeNotification()"
  58. >
  59. <CommonIcon name="delete" class="text-red" size="tiny" />
  60. </button>
  61. <div class="flex items-center ltr:pr-2 rtl:pl-2">
  62. <div
  63. role="status"
  64. class="h-3 w-3 rounded-full"
  65. :class="{ 'bg-blue': !seen }"
  66. :aria-label="seen ? $t('Notification read') : $t('Unread notification')"
  67. ></div>
  68. </div>
  69. <ActivityMessage
  70. :type-name="typeName"
  71. :object-name="objectName"
  72. :created-at="createdAt"
  73. :created-by="createdBy"
  74. :meta-object="metaObject"
  75. @seen="$emit('seen', itemId)"
  76. />
  77. </div>
  78. </template>