Notification.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. style="font-size: 13px; line-height: 16px;"
  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-divider v-if="index < (notifications.length - 1)"/>-->
  51. </v-list-item>
  52. </v-list>
  53. <!--suppress VueUnrecognizedDirective -->
  54. <v-card v-intersect="update"></v-card>
  55. <v-row
  56. v-if="pending"
  57. class="fill-height mt-3 mb-3"
  58. align="center"
  59. justify="center"
  60. >
  61. <v-progress-circular
  62. indeterminate
  63. color="grey lighten-1"
  64. />
  65. </v-row>
  66. </v-card-text>
  67. <v-card-actions class="ma-0 pa-0">
  68. <v-list-item
  69. id="all_notifications"
  70. :key="$t('all_notification')"
  71. :href="notificationUrl"
  72. router
  73. style="width: 100%; height: 52px;"
  74. >
  75. <v-list-item-title
  76. class="text-body-2 text-ot-white"
  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, QUERY_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 {ComputedRef, Ref, ref} from "@vue/reactivity";
  90. import {useEntityFetch} from "~/composables/data/useEntityFetch";
  91. import {Pagination} from "~/types/data";
  92. import {useEntityManager} from "~/composables/data/useEntityManager";
  93. import Url from "~/services/utils/url";
  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. items.sort((a, b) => b.id - a.id ) // reverse sort by id
  110. return items
  111. })
  112. /**
  113. * Les metadata dépendront de la dernière valeur du GET lancé
  114. */
  115. const pagination: ComputedRef<Pagination> = computed(() => {
  116. return collection.value !== null ? collection.value.pagination : {}
  117. })
  118. /**
  119. * On calcule le nombre de notifications non lues
  120. */
  121. const unreadNotification: ComputedRef<Array<Notification>> = computed(() => {
  122. return notifications.value.filter((notification: Notification) => {
  123. return notification.notificationUsers.length === 0
  124. })
  125. })
  126. /**
  127. * Lorsque l'utilisateur scroll on regarde la nextPage a charger et on le fait que si le pending du fetch est false
  128. * (si on a fini de télécharger les éléments précédents)
  129. */
  130. const update = async () => {
  131. if (
  132. !pending &&
  133. pagination.value &&
  134. pagination.value.next &&
  135. pagination.value.next > 0
  136. ) {
  137. loading.value = true
  138. page.value = pagination.value.next
  139. await refresh()
  140. // Si des notifications n'avaient pas été marquées comme lues, on le fait immédiatement.
  141. markNotificationsAsRead()
  142. }
  143. }
  144. /**
  145. * On construit le message qui va devoir s'afficher pour une notification
  146. * @param notification
  147. */
  148. const getMessage = (notification: Notification) => {
  149. switch (notification.type){
  150. case NOTIFICATION_TYPE.FILE :
  151. return `${i18n.t('your_file')} ${notification.message?.fileName} ${i18n.t('is_ready_to_be_downloaded')}`
  152. case NOTIFICATION_TYPE.MESSAGE:
  153. if (notification.message?.action)
  154. return `${i18n.t('your_message')} ${notification.message?.fileName} ${i18n.t('is_ready_to_be')} ${notification.message.action}`
  155. return `${i18n.t('your_message')} ${notification.message?.about ?? ''} ${i18n.t('has_been_sent')} `
  156. case NOTIFICATION_TYPE.SYSTEM :
  157. if (notification.message?.about)
  158. return `${i18n.t(notification.message.about)}`
  159. break;
  160. default:
  161. return i18n.t(notification.name)
  162. }
  163. }
  164. /**
  165. * Dès l'ouverture du menu, on indique que les notifications non lues, le sont.
  166. */
  167. const unwatch = watch(isOpen, (newValue, oldValue) => {
  168. if (newValue){
  169. markNotificationsAsRead()
  170. }
  171. })
  172. onUnmounted(() => {
  173. unwatch()
  174. })
  175. /**
  176. * Créer une nouvelle notification users coté back.
  177. * @param notification
  178. * @param accessId
  179. */
  180. const createNewNotificationUsers = (notification: Notification, accessId: number) => {
  181. const notificationUsers = em.newInstance(NotificationUsers, {
  182. access:`/api/accesses/${accessId}`,
  183. notification:`/api/notifications/${notification.id}`,
  184. isRead: true
  185. })
  186. em.persist(NotificationUsers, notificationUsers)
  187. notification.notificationUsers = ['read']
  188. }
  189. /**
  190. * Marque les notifications non lues comme lues
  191. */
  192. const markNotificationsAsRead = () => {
  193. unreadNotification.value.map((notification: Notification) => {
  194. if (accessProfileStore.id === null) {
  195. throw new Error('Current access id is null')
  196. }
  197. createNewNotificationUsers(notification, accessProfileStore.id)
  198. })
  199. }
  200. /**
  201. * Download la cible du lien
  202. * @param link
  203. */
  204. const download = (link: string) => {
  205. if (accessProfileStore.id === null) {
  206. throw new Error('Current access id is null')
  207. }
  208. const url_parts: Array<string> = link.split('/api');
  209. if(accessProfileStore.originalAccess)
  210. url_parts[0] = Url.join('api', String(accessProfileStore.originalAccess.id), String(accessProfileStore.id))
  211. else
  212. url_parts[0] = Url.join('api', String(accessProfileStore.id))
  213. window.open(Url.join(runtimeConfig.baseUrlLegacy, url_parts.join('')));
  214. }
  215. const notificationUrl = Url.join(runtimeConfig.baseUrlAdminLegacy, 'notifications/list/')
  216. </script>
  217. <style scoped lang="scss">
  218. #all_notifications{
  219. background: rgb(var(--v-theme-ot-green, white));
  220. color: white;
  221. }
  222. .list_item{
  223. white-space: normal;
  224. }
  225. .unread{
  226. background: #ecf0f5;
  227. }
  228. </style>