Notification.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // TODO: revoir pour reprendre le order by et tout
  109. return collection.value !== null ? collection.value.items : []
  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 &&
  132. pagination.value &&
  133. pagination.value.next &&
  134. pagination.value.next > 0
  135. ) {
  136. loading.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>