Notification.vue 7.8 KB

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