Notification.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <v-btn
  3. ref="btn"
  4. icon
  5. width="48px"
  6. size="small"
  7. >
  8. <v-badge
  9. color="orange"
  10. offset-x="-4"
  11. offset-y="17"
  12. :model-value="unreadNotification.length > 0"
  13. :content="unreadNotification.length">
  14. <v-icon class="text-ot-white">
  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. >
  26. <v-card max-width="400">
  27. <v-card-title class="ot-header-menu text-body-2 font-weight-bold">
  28. {{ $t('notification') }}
  29. </v-card-title>
  30. <v-card-text class="ma-0 pa-0 header-menu">
  31. <v-list density="compact" :subheader="true">
  32. <v-list-item
  33. v-for="(notification, index) in notifications"
  34. :key="index"
  35. :class="`${notification.notificationUsers.length === 0 ? 'unread' : ''}`"
  36. >
  37. <v-list-item-title
  38. class="list_item mt-2 mb-2"
  39. v-text="getMessage(notification)"
  40. />
  41. <template #append>
  42. <v-icon
  43. v-if="notification.link"
  44. icon="mdi:mdi-download"
  45. @click="download(notification.link)"
  46. class="pt-4"
  47. />
  48. </template>
  49. </v-list-item>
  50. <v-divider></v-divider>
  51. </v-list>
  52. <!--suppress VueUnrecognizedDirective -->
  53. <v-card v-intersect="update"></v-card>
  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="grey lighten-1"
  63. />
  64. </v-row>
  65. </v-card-text>
  66. <v-card-actions class="ma-0 pa-0">
  67. <v-list-item
  68. id="all_notifications"
  69. :key="$t('all_notification')"
  70. :href="notificationUrl"
  71. router
  72. style="width: 100%; height: 52px;"
  73. >
  74. <v-list-item-title
  75. class="text-body-2 text-ot-white"
  76. v-text="$t('all_notification')"
  77. />
  78. </v-list-item>
  79. </v-card-actions>
  80. </v-card>
  81. </v-menu>
  82. </template>
  83. <script setup lang="ts">
  84. import {NOTIFICATION_TYPE} from "~/types/enum/enums";
  85. import {Notification} from "~/models/Core/Notification";
  86. import {NotificationUsers} from "~/models/Core/NotificationUsers";
  87. import {useAccessProfileStore} from "~/stores/accessProfile";
  88. import {ComputedRef, Ref, ref} from "@vue/reactivity";
  89. import {useEntityFetch} from "~/composables/data/useEntityFetch";
  90. import {Pagination} from "~/types/data";
  91. import {useEntityManager} from "~/composables/data/useEntityManager";
  92. import Url from "~/services/utils/url";
  93. import ArrayUtils from "~/services/utils/arrayUtils";
  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. let { data: collection, pending, refresh } = await fetchCollection(Notification)
  104. /**
  105. * On récupère les Notifications via le store
  106. */
  107. const notifications: ComputedRef = computed(() => {
  108. const items = collection.value !== null ? collection.value.items : []
  109. return ArrayUtils.sortArrayOfObject(items, 'id')
  110. })
  111. /**
  112. * Les metadata dépendront de la dernière valeur du GET lancé
  113. */
  114. const pagination: ComputedRef<Pagination> = computed(() => {
  115. return collection.value !== null ? collection.value.pagination : {}
  116. })
  117. /**
  118. * On calcule le nombre de notifications non lues
  119. */
  120. const unreadNotification: ComputedRef<Array<Notification>> = computed(() => {
  121. return notifications.value.filter((notification: Notification) => {
  122. return notification.notificationUsers.length === 0
  123. })
  124. })
  125. /**
  126. * Lorsque l'utilisateur scroll on regarde la nextPage a charger et on le fait que si le pending du fetch est false
  127. * (si on a fini de télécharger les éléments précédents)
  128. */
  129. const update = async () => {
  130. if (
  131. !pending.value &&
  132. pagination.value &&
  133. pagination.value.next &&
  134. pagination.value.next > 0
  135. ) {
  136. pending.value = true
  137. page.value = pagination.value.next
  138. await refresh()
  139. // Si des notifications n'avaient pas été marquées comme lues, on le fait immédiatement.
  140. markNotificationsAsRead()
  141. }
  142. }
  143. /**
  144. * On construit le message qui va devoir s'afficher pour une notification
  145. * @param notification
  146. */
  147. const getMessage = (notification: Notification) => {
  148. switch (notification.type){
  149. case NOTIFICATION_TYPE.FILE :
  150. return `${i18n.t('your_file')} ${notification.message?.fileName} ${i18n.t('is_ready_to_be_downloaded')}`
  151. case NOTIFICATION_TYPE.MESSAGE:
  152. if (notification.message?.action)
  153. return `${i18n.t('your_message')} ${notification.message?.fileName} ${i18n.t('is_ready_to_be')} ${notification.message.action}`
  154. return `${i18n.t('your_message')} ${notification.message?.about ?? ''} ${i18n.t('has_been_sent')} `
  155. case NOTIFICATION_TYPE.SYSTEM :
  156. if (notification.message?.about)
  157. return `${i18n.t(notification.message.about)}`
  158. break;
  159. default:
  160. return i18n.t(notification.name)
  161. }
  162. }
  163. /**
  164. * Dès l'ouverture du menu, on indique que les notifications non lues, le sont.
  165. */
  166. const unwatch = watch(isOpen, (newValue, oldValue) => {
  167. if (newValue){
  168. markNotificationsAsRead()
  169. }
  170. })
  171. onUnmounted(() => {
  172. unwatch()
  173. })
  174. /**
  175. * Créer une nouvelle notification users coté back.
  176. * @param notification
  177. * @param accessId
  178. */
  179. const createNewNotificationUsers = (notification: Notification, accessId: number) => {
  180. const notificationUsers = em.newInstance(NotificationUsers, {
  181. access:`/api/accesses/${accessId}`,
  182. notification:`/api/notifications/${notification.id}`,
  183. isRead: true
  184. })
  185. em.persist(NotificationUsers, notificationUsers)
  186. notification.notificationUsers = ['read']
  187. }
  188. /**
  189. * Marque les notifications non lues comme lues
  190. */
  191. const markNotificationsAsRead = () => {
  192. unreadNotification.value.map((notification: Notification) => {
  193. if (accessProfileStore.id === null) {
  194. throw new Error('Current access id is null')
  195. }
  196. createNewNotificationUsers(notification, accessProfileStore.id)
  197. })
  198. }
  199. /**
  200. * Download la cible du lien
  201. * @param link
  202. */
  203. const download = (link: string) => {
  204. if (accessProfileStore.id === null) {
  205. throw new Error('Current access id is null')
  206. }
  207. const url_parts: Array<string> = link.split('/api');
  208. if(accessProfileStore.originalAccess)
  209. url_parts[0] = Url.join('api', String(accessProfileStore.originalAccess.id), String(accessProfileStore.id))
  210. else
  211. url_parts[0] = Url.join('api', String(accessProfileStore.id))
  212. window.open(Url.join(runtimeConfig.baseUrlLegacy, url_parts.join('')));
  213. }
  214. const notificationUrl = Url.join(runtimeConfig.baseUrlAdminLegacy, 'notifications/list/')
  215. </script>
  216. <style scoped lang="scss">
  217. #all_notifications{
  218. background: rgb(var(--v-theme-ot-green, white));
  219. color: white;
  220. }
  221. .list_item{
  222. white-space: normal;
  223. }
  224. .unread{
  225. background: #ecf0f5;
  226. }
  227. </style>