DataTimingRange.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <main class="d-flex align-center data-timing-range">
  3. <div class="d-flex align-center" style="max-height: 100%">
  4. <span class="label pl-2 mr-2 font-weight-bold on-neutral">
  5. {{ $t('period_choose') }}
  6. </span>
  7. <UiDatePicker
  8. :model-value="datesRange"
  9. :max-height="28"
  10. :range="true"
  11. :auto-apply="false"
  12. @update:model-value="updateDateTimeRange"
  13. />
  14. </div>
  15. </main>
  16. </template>
  17. <script setup lang="ts">
  18. import type { Ref } from 'vue'
  19. import { useAccessProfileStore } from '~/stores/accessProfile'
  20. import { useFormStore } from '~/stores/form'
  21. import { useEntityManager } from '~/composables/data/useEntityManager'
  22. import Access from '~/models/Access/Access'
  23. import DateUtils from '~/services/utils/dateUtils'
  24. import { usePageStore } from '~/stores/page'
  25. import { useRefreshProfile } from '~/composables/data/useRefreshProfile'
  26. const { setDirty } = useFormStore()
  27. const accessProfileStore = useAccessProfileStore()
  28. const { em } = useEntityManager()
  29. const pageStore = usePageStore()
  30. const { refreshProfile } = useRefreshProfile()
  31. const start = accessProfileStore.historical.dateStart
  32. const end = accessProfileStore.historical.dateEnd
  33. const datesRange: Ref<Array<Date> | null> = ref(
  34. start && end ? [new Date(start), new Date(end)] : null,
  35. )
  36. const updateDateTimeRange = async (dates: Array<Date>): Promise<any> => {
  37. const accessId = accessProfileStore.currentAccessId
  38. datesRange.value = dates
  39. if (
  40. datesRange.value !== null &&
  41. datesRange.value[0] !== null &&
  42. datesRange.value[1] !== null
  43. ) {
  44. accessProfileStore.setHistoricalRange(
  45. DateUtils.formatIsoShortDate(datesRange.value[0]),
  46. DateUtils.formatIsoShortDate(datesRange.value[1]),
  47. )
  48. } else {
  49. accessProfileStore.setHistorical(false, true, false)
  50. }
  51. setDirty(false)
  52. pageStore.loading = true
  53. await em.patch(Access, accessId, {
  54. historical: accessProfileStore.historical,
  55. })
  56. if (import.meta.server) {
  57. // Force profile refresh server side to avoid a bug where server and client stores diverge on profile refresh
  58. await refreshProfile()
  59. }
  60. window.location.reload()
  61. }
  62. </script>
  63. <style scoped lang="scss">
  64. .label {
  65. min-width: 150px;
  66. }
  67. :deep(.dp__input) {
  68. max-height: 28px;
  69. }
  70. </style>