DataTimingRange.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <main class="d-flex align-center data-timing-range">
  3. <div v-if="show" class="d-flex align-center" style="max-height: 100%">
  4. <span class="pl-2 mr-2 font-weight-bold on-neutral" style="min-width: 150px;">
  5. {{ $t('period_choose') }}
  6. </span>
  7. <UiInputDateRangePicker
  8. :model-value="datesRange"
  9. :max-height="28"
  10. @update:model-value="updateDateTimeRange"
  11. />
  12. </div>
  13. <v-btn
  14. ref="btn"
  15. class="time-btn ml-1 theme-neutral-soft"
  16. height="22" min-height="22" max-height="22"
  17. width="25" min-width="25" max-width="25"
  18. elevation="0"
  19. @click="show = !show"
  20. >
  21. <v-icon icon="fas fa-history" class="font-weight-normal" style="font-size: 14px;" />
  22. </v-btn>
  23. <v-tooltip location="bottom" :activator="btn">
  24. <span>{{ $t('history_help') }}</span>
  25. </v-tooltip>
  26. </main>
  27. </template>
  28. <script setup lang="ts">
  29. import {Ref} from "@vue/reactivity";
  30. import {useAccessProfileStore} from "~/stores/accessProfile";
  31. import {useFormStore} from "~/stores/form";
  32. import {WatchStopHandle} from "@vue/runtime-core";
  33. import {useEntityManager} from "~/composables/data/useEntityManager";
  34. import Access from "~/models/Access/Access";
  35. import DateUtils from "~/services/utils/dateUtils";
  36. const btn: Ref = ref(null)
  37. const show: Ref<boolean> = ref(false)
  38. const { setDirty } = useFormStore()
  39. const accessProfileStore = useAccessProfileStore()
  40. const { em } = useEntityManager()
  41. const start = accessProfileStore.historical.dateStart
  42. const end = accessProfileStore.historical.dateEnd
  43. const datesRange: Ref<Array<Date> | null> = ref((start && end) ? [new Date(start), new Date(end)] : null)
  44. const updateDateTimeRange = async (dates: Array<Date>): Promise<any> => {
  45. if (accessProfileStore.id === null) {
  46. throw new Error('Invalid profile id')
  47. }
  48. datesRange.value = dates
  49. if (datesRange.value !== null && datesRange.value[0] !== null && datesRange.value[1] !== null) {
  50. accessProfileStore.setHistoricalRange(
  51. DateUtils.formatIsoShortDate(datesRange.value[0]),
  52. DateUtils.formatIsoShortDate(datesRange.value[1])
  53. )
  54. } else {
  55. accessProfileStore.setHistorical(false, true, false)
  56. }
  57. setDirty(false)
  58. await em.patch(
  59. Access,
  60. accessProfileStore.id,
  61. {'historical': accessProfileStore.historical}
  62. )
  63. window.location.reload()
  64. }
  65. /**
  66. * Emit event when component is hidden / shown
  67. */
  68. const emit = defineEmits(['showDateTimeRange'])
  69. const unwatch: WatchStopHandle = watch(show, (newValue) => {
  70. emit('showDateTimeRange', newValue)
  71. })
  72. onUnmounted(() => {
  73. unwatch()
  74. })
  75. // Show by default if a date range is defined in store
  76. if (accessProfileStore.historical.dateStart || accessProfileStore.historical.dateEnd) {
  77. show.value = true
  78. emit('showDateTimeRange', true)
  79. }
  80. </script>
  81. <style scoped lang="scss">
  82. .time-btn {
  83. border-width: 1px 1px 1px 0;
  84. border-style: solid;
  85. }
  86. </style>