Notification.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <v-menu offset-y v-model="isOpen">
  3. <template v-slot:activator="{ on: { click }, attrs }">
  4. <v-tooltip bottom>
  5. <template v-slot:activator="{ on: on_tooltips , attrs: attrs_tooltips }">
  6. <v-btn
  7. icon
  8. v-bind="[attrs, attrs_tooltips]"
  9. color=""
  10. v-on="on_tooltips"
  11. @click="click"
  12. >
  13. <v-badge
  14. color="orange"
  15. offset-y="10"
  16. :value="unreadNotification.length > 0"
  17. :content="unreadNotification.length"
  18. >
  19. <v-icon class="ot_white--text" small>
  20. fa-bell
  21. </v-icon>
  22. </v-badge>
  23. </v-btn>
  24. </template>
  25. <span>{{ $t('notification') }}</span>
  26. </v-tooltip>
  27. </template>
  28. <v-card scrollable max-width="400">
  29. <v-card-title class="ot_header_menu 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 dense :subheader="true">
  34. <template v-for="(notification, index) in notifications">
  35. <v-list-item :key="index" :class="`${notification.notificationUsers.length === 0 ? 'unread' : ''}`">
  36. <v-list-item-content>
  37. <v-list-item-title class="list_item mt-2 mb-2" v-text="getMessage(notification)"/>
  38. </v-list-item-content>
  39. <v-list-item-icon v-if="notification.link" class="pt-4">
  40. <v-icon @click="download(notification.link)">mdi-download</v-icon>
  41. </v-list-item-icon>
  42. </v-list-item>
  43. <v-divider></v-divider>
  44. </template>
  45. </v-list>
  46. <v-card v-intersect="update"></v-card>
  47. <v-row
  48. v-if="loading"
  49. class="fill-height mt-3 mb-3"
  50. align="center"
  51. justify="center"
  52. >
  53. <v-progress-circular
  54. indeterminate
  55. color="grey lighten-1"
  56. ></v-progress-circular>
  57. </v-row>
  58. </v-card-text>
  59. <v-card-actions class="ma-0 pa-0">
  60. <template>
  61. <v-list-item
  62. id="all_notifications"
  63. :key="$t('all_notification')"
  64. :href="notificationUrl"
  65. router
  66. >
  67. <v-list-item-title class="text-body-2 ot_white--text" v-text="$t('all_notification')"/>
  68. </v-list-item>
  69. </template>
  70. </v-card-actions>
  71. </v-card>
  72. </v-menu>
  73. </template>
  74. <script lang="ts">
  75. import {computed, ComputedRef, defineComponent, Ref, ref, useContext, useFetch, useStore, watch} from '@nuxtjs/composition-api'
  76. import {NOTIFICATION_TYPE, QUERY_TYPE} from "~/types/enums";
  77. import {Notification} from "~/models/Core/Notification";
  78. import {repositoryHelper} from "~/services/store/repository";
  79. import {AnyStore, ApiResponse, HydraMetadata} from "~/types/interfaces";
  80. import {queryHelper} from "~/services/store/query";
  81. import {NotificationUsers} from "~/models/Core/NotificationUsers";
  82. import {State} from "@vuex-orm/core";
  83. export default defineComponent({
  84. setup: function () {
  85. const {$dataProvider, $dataPersister, $config, app: { i18n }} = useContext()
  86. const store:AnyStore = useStore<State>()
  87. const profileAccess = store.state.profile.access
  88. const loading: Ref<Boolean> = ref(true)
  89. const isOpen: Ref<Boolean> = ref(false)
  90. const page: Ref<number> = ref(1)
  91. const data: Ref<ApiResponse> = ref({} as ApiResponse)
  92. /**
  93. * On récupère les notifications via l'API qui seront stockées dans le store
  94. */
  95. const {fetch, fetchState} = useFetch(async () => {
  96. data.value = await $dataProvider.invoke({
  97. type: QUERY_TYPE.MODEL,
  98. model: Notification,
  99. listArgs: {
  100. itemsPerPage: 10,
  101. page: page.value
  102. }
  103. })
  104. loading.value = false
  105. })
  106. /**
  107. * On récupère les Notifications via le store
  108. */
  109. const notifications: ComputedRef = computed(() => {
  110. const query = repositoryHelper.getRepository(Notification).with('message').orderBy('id', 'desc')
  111. return queryHelper.getCollection(query)
  112. })
  113. /**
  114. * on calcul le nombre de notification non lues
  115. */
  116. const unreadNotification: ComputedRef<Array<Notification>> = computed(() => {
  117. return notifications.value.filter((notification: Notification) => {
  118. return notification.notificationUsers.length === 0
  119. })
  120. })
  121. /**
  122. * Les metadata dépendront de la dernière valeur du GET lancé
  123. */
  124. const metadata: ComputedRef<HydraMetadata> = computed(() => {
  125. return data.value.metadata
  126. })
  127. /**
  128. * Lorsque l'utilisateur scroll on regarde la nextPage a charger et on le fait que si le pending du fetch est false
  129. * (si on a fini de télécharger les éléments précédents)
  130. */
  131. const update = async () => {
  132. if (!fetchState.pending && metadata.value?.nextPage && metadata.value.nextPage > 0) {
  133. loading.value = true
  134. page.value = metadata.value.nextPage
  135. await fetch()
  136. //Si des notifications n'avaient pas été marquées comme lues, on le fait immédiatement.
  137. markNotificationsAsRead()
  138. }
  139. }
  140. /**
  141. * On construit le message qui va devoir s'afficher pour une notification
  142. * @param notification
  143. */
  144. const getMessage = (notification:Notification) => {
  145. switch (notification.type){
  146. case NOTIFICATION_TYPE.FILE :
  147. return `${i18n.t('your_file')} ${notification.message?.fileName} ${i18n.t('is_ready_to_be_downloaded')}`
  148. break;
  149. case NOTIFICATION_TYPE.MESSAGE:
  150. if(notification.message?.action)
  151. return `${i18n.t('your_message')} ${notification.message?.fileName} ${i18n.t('is_ready_to_be')} ${notification.message.action}`
  152. return `${i18n.t('your_message')} ${notification.message?.about} ${i18n.t('has_been_sent')} `
  153. break;
  154. default:
  155. return i18n.t(notification.name)
  156. }
  157. }
  158. /**
  159. * Dès l'ouverture du menu, on indique que les notifications non lues, le sont.
  160. */
  161. watch(isOpen, (newValue, oldValue) => {
  162. if(newValue){
  163. markNotificationsAsRead()
  164. }
  165. })
  166. /**
  167. * Marque les notification non lues comme lues
  168. */
  169. const markNotificationsAsRead = () => {
  170. unreadNotification.value.map((notification:Notification)=>{
  171. notification.notificationUsers = ['read']
  172. repositoryHelper.persist(Notification, notification)
  173. createNewNotificationUsers(notification)
  174. })
  175. }
  176. /**
  177. * Créer une nouvelle notification users coté back.
  178. * @param notification
  179. */
  180. const createNewNotificationUsers = (notification: Notification) =>{
  181. const newNotificationUsers = repositoryHelper.persist(NotificationUsers, new NotificationUsers(
  182. {
  183. access:`/api/accesses/${profileAccess.id}`,
  184. notification:`/api/notifications/${notification.id}`,
  185. isRead: true
  186. }
  187. )) as NotificationUsers
  188. $dataPersister.invoke({
  189. type: QUERY_TYPE.MODEL,
  190. model: NotificationUsers,
  191. idTemp: newNotificationUsers.id,
  192. showProgress: false
  193. })
  194. }
  195. /**
  196. * Download le lien
  197. * @param link
  198. */
  199. const download = (link: string) => {
  200. const url_parts: Array<string> = link.split('/api');
  201. if(profileAccess.originalAccess)
  202. url_parts[0] = `api/${profileAccess.originalAccess}/${profileAccess.id}`
  203. else
  204. url_parts[0] = `api/${profileAccess.id}`
  205. window.open(`${$config.baseURL_Legacy}/${url_parts.join('')}`);
  206. }
  207. return {
  208. data,
  209. getMessage,
  210. notificationUrl: `${$config.baseURL_adminLegacy}/notifications/list/`,
  211. loading,
  212. notifications,
  213. update,
  214. unreadNotification,
  215. isOpen,
  216. download
  217. }
  218. }
  219. })
  220. </script>
  221. <style scoped>
  222. #all_notifications{
  223. background: var(--v-ot_green-base, white);
  224. color: white;
  225. }
  226. .list_item{
  227. white-space: normal;
  228. }
  229. .unread{
  230. background: #ecf0f5;
  231. }
  232. </style>