page.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { defineStore } from 'pinia'
  2. import { ref } from 'vue'
  3. import type { Ref } from 'vue'
  4. import type { TYPE_ALERT } from '~/types/enum/enums'
  5. import type { Alert } from '~/types/interfaces'
  6. export const usePageStore = defineStore('page', () => {
  7. const alerts: Ref<Array<Alert>> = ref([])
  8. const loading: Ref<boolean> = ref(false)
  9. let timeoutId: ReturnType<typeof setTimeout> | null = null
  10. const removeSlowlyAlert = () => {
  11. clearAlertTimeout()
  12. timeoutId = setTimeout(() => {
  13. alerts.value.shift()
  14. timeoutId = null
  15. }, 300)
  16. }
  17. const clearAlertTimeout = () => {
  18. if (timeoutId !== null) {
  19. clearTimeout(timeoutId)
  20. timeoutId = null
  21. }
  22. }
  23. /**
  24. * Ajout des alerts dans le store
  25. * @param type
  26. * @param messages
  27. */
  28. const addAlert = (type: TYPE_ALERT, messages: Array<string>) => {
  29. const alert: Alert = {
  30. type,
  31. messages,
  32. }
  33. alerts.value.push(alert)
  34. }
  35. return {
  36. alerts,
  37. loading,
  38. removeSlowlyAlert,
  39. addAlert,
  40. clearAlertTimeout,
  41. }
  42. })