Notification.vue 7.6 KB

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