Notification.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <v-btn
  3. ref="btn"
  4. icon
  5. size="small"
  6. class="ml-2"
  7. >
  8. <v-badge
  9. color="warning"
  10. offset-x="-4"
  11. offset-y="17"
  12. :model-value="unreadNotification.length > 0"
  13. :content="unreadNotification.length">
  14. <v-icon class="on-primary">
  15. fa fa-bell
  16. </v-icon>
  17. </v-badge>
  18. </v-btn>
  19. <v-tooltip :activator="btn" location="bottom">
  20. <span>{{ $t('notification') }}</span>
  21. </v-tooltip>
  22. <v-menu
  23. :activator="btn"
  24. v-model="isOpen"
  25. location="bottom left"
  26. >
  27. <v-card max-width="400">
  28. <v-card-title class="bg-neutral text-body-2 font-weight-bold">
  29. {{ $t('notification') }}
  30. </v-card-title>
  31. <v-card-text class="ma-0 pa-0 header-menu">
  32. <v-list density="compact" :subheader="true" class="pa-0">
  33. <v-list-item
  34. v-for="(notification, index) in notifications"
  35. :key="index"
  36. :class="'list_item py-3' + `${notification.notificationUsers.length === 0 ? ' unread' : ''}`"
  37. >
  38. <span class="">{{ getMessage(notification) }}</span>
  39. <template #append>
  40. <v-icon
  41. v-if="notification.link"
  42. icon="mdi:mdi-download"
  43. @click="download(notification.link)"
  44. class="pt-4"
  45. />
  46. </template>
  47. </v-list-item>
  48. <v-divider></v-divider>
  49. <!--suppress VueUnrecognizedDirective -->
  50. <span v-intersect="onLastNotificationIntersect" />
  51. <v-row
  52. v-if="pending"
  53. class="fill-height mt-3 mb-3"
  54. align="center"
  55. justify="center"
  56. >
  57. <v-progress-circular
  58. indeterminate
  59. color="neutral"
  60. />
  61. </v-row>
  62. </v-list>
  63. </v-card-text>
  64. <v-card-actions class="ma-0 pa-0">
  65. <v-list-item
  66. :key="$t('all_notification')"
  67. :href="notificationUrl"
  68. router
  69. class="theme-primary"
  70. style="width: 100%; height: 52px;"
  71. >
  72. <v-list-item-title
  73. class="text-body-2"
  74. v-text="$t('all_notification')"
  75. />
  76. </v-list-item>
  77. </v-card-actions>
  78. </v-card>
  79. </v-menu>
  80. </template>
  81. <script setup lang="ts">
  82. import {NOTIFICATION_TYPE} from "~/types/enum/enums";
  83. import Notification from "~/models/Core/Notification";
  84. import NotificationUsers from "~/models/Core/NotificationUsers";
  85. import {useAccessProfileStore} from "~/stores/accessProfile";
  86. import {computed, ComputedRef, Ref, ref} from "@vue/reactivity";
  87. import {useEntityFetch} from "~/composables/data/useEntityFetch";
  88. import {AnyJson, Pagination} from "~/types/data";
  89. import {useEntityManager} from "~/composables/data/useEntityManager";
  90. import UrlUtils from "~/services/utils/urlUtils";
  91. import {useRepo} from "pinia-orm";
  92. import NotificationRepository from "~/stores/repositories/NotificationRepository";
  93. const accessProfileStore = useAccessProfileStore()
  94. const loading: Ref<Boolean> = ref(true)
  95. const isOpen: Ref<Boolean> = ref(false)
  96. const page: Ref<number> = ref(1)
  97. const i18n = useI18n()
  98. const runtimeConfig = useRuntimeConfig()
  99. const btn = ref(null)
  100. const { em } = useEntityManager()
  101. const { fetchCollection } = useEntityFetch()
  102. const notificationRepo = useRepo(NotificationRepository)
  103. const query: ComputedRef<AnyJson> = computed(() => {
  104. return { 'page': page.value }
  105. })
  106. let { data: collection, pending, refresh } = await fetchCollection(Notification, null, query)
  107. /**
  108. * On récupère les Notifications via le store (sans ça, les mises à jour SSE ne seront pas prises en compte)
  109. */
  110. const notifications: ComputedRef<Array<Notification>> = computed(() => {
  111. return notificationRepo.getNotifications()
  112. })
  113. /**
  114. * Retourne les notifications non lues
  115. */
  116. const unreadNotification: ComputedRef<Array<Notification>> = computed(() => {
  117. return notificationRepo.getUnreadNotifications()
  118. })
  119. /**
  120. * Les metadata dépendront de la dernière valeur du GET lancé
  121. */
  122. const pagination: ComputedRef<Pagination> = computed(() => {
  123. return collection.value !== null ? collection.value.pagination : {}
  124. })
  125. const notificationUrl = UrlUtils.join(runtimeConfig.baseUrlAdminLegacy, 'notifications/list/')
  126. /**
  127. * L'utilisateur a fait défiler le menu jusqu'à la dernière notification affichée
  128. * @param isIntersecting
  129. */
  130. const onLastNotificationIntersect = (isIntersecting: boolean) => {
  131. if (isIntersecting) {
  132. update()
  133. }
  134. }
  135. /**
  136. * Lorsque l'utilisateur scroll on regarde la nextPage à charger et on le fait que si le pending du fetch est false
  137. * (si on a fini de télécharger les éléments précédents)
  138. */
  139. const update = async () => {
  140. if (
  141. !pending.value &&
  142. pagination.value &&
  143. pagination.value.next &&
  144. pagination.value.next > 0
  145. ) {
  146. pending.value = true
  147. page.value = pagination.value.next
  148. await refresh()
  149. // Si des notifications n'avaient pas été marquées comme lues, on le fait immédiatement.
  150. markNotificationsAsRead()
  151. }
  152. }
  153. /**
  154. * On construit le message qui va devoir s'afficher pour une notification
  155. * @param notification
  156. */
  157. const getMessage = (notification: Notification) => {
  158. switch (notification.type){
  159. case NOTIFICATION_TYPE.FILE :
  160. return `${i18n.t('your_file')} ${notification.message?.fileName} ${i18n.t('is_ready_to_be_downloaded')}`
  161. case NOTIFICATION_TYPE.MESSAGE:
  162. if (notification.message?.action)
  163. return `${i18n.t('your_message')} ${notification.message?.fileName} ${i18n.t('is_ready_to_be')} ${notification.message.action}`
  164. return `${i18n.t('your_message')} ${notification.message?.about ?? ''} ${i18n.t('has_been_sent')} `
  165. case NOTIFICATION_TYPE.SYSTEM :
  166. if (notification.message?.about)
  167. return `${i18n.t(notification.message.about)}`
  168. break;
  169. default:
  170. return i18n.t(notification.name)
  171. }
  172. }
  173. /**
  174. * Dès la fermeture du menu, on indique que les notifications non lues, le sont.
  175. */
  176. const unwatch = watch(isOpen, (newValue, oldValue) => {
  177. if (!newValue){
  178. markNotificationsAsRead()
  179. }
  180. })
  181. onUnmounted(() => {
  182. unwatch()
  183. })
  184. /**
  185. * Marque une notification comme lue
  186. */
  187. const markNotificationAsRead = (notification: Notification) => {
  188. if (accessProfileStore.id === null) {
  189. throw new Error('Current access id is null')
  190. }
  191. const notificationUsers = em.newInstance(NotificationUsers, {
  192. access:`/api/accesses/${accessProfileStore.id}`,
  193. notification:`/api/notifications/${notification.id}`,
  194. isRead: true
  195. })
  196. em.persist(NotificationUsers, notificationUsers)
  197. notification.notificationUsers = ['read']
  198. em.save(Notification, notification)
  199. }
  200. /**
  201. * Marque toutes les notifications non lues comme lues
  202. */
  203. const markNotificationsAsRead = () => {
  204. unreadNotification.value.map((notification: Notification) => {
  205. markNotificationAsRead(notification)
  206. })
  207. }
  208. /**
  209. * Download la cible du lien
  210. * @param link
  211. */
  212. const download = (link: string) => {
  213. if (accessProfileStore.id === null) {
  214. throw new Error('Current access id is null')
  215. }
  216. const url_parts: Array<string> = link.split('/api');
  217. if(accessProfileStore.originalAccess)
  218. url_parts[0] = UrlUtils.join('api', String(accessProfileStore.originalAccess.id), String(accessProfileStore.id))
  219. else
  220. url_parts[0] = UrlUtils.join('api', String(accessProfileStore.id))
  221. window.open(UrlUtils.join(runtimeConfig.baseUrlLegacy, url_parts.join('')));
  222. }
  223. </script>
  224. <style scoped lang="scss">
  225. .list_item{
  226. white-space: normal;
  227. }
  228. .unread{
  229. background: rgb(var(--v-theme-neutral-soft, white));
  230. }
  231. </style>