DataTimingRange.vue 2.8 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.dateStart
  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. return
  51. }
  52. accessProfileStore.setHistoricalRange(
  53. DateUtils.formatIsoShortDate(datesRange.value[0]),
  54. DateUtils.formatIsoShortDate(datesRange.value[1])
  55. )
  56. setDirty(false)
  57. await em.patch(
  58. Access,
  59. accessProfileStore.id,
  60. {'historical': accessProfileStore.historical}
  61. )
  62. window.location.reload()
  63. }
  64. /**
  65. * Emit event when component is hidden / shown
  66. */
  67. const emit = defineEmits(['showDateTimeRange'])
  68. const unwatch: WatchStopHandle = watch(show, (newValue) => {
  69. emit('showDateTimeRange', newValue)
  70. })
  71. onUnmounted(() => {
  72. unwatch()
  73. })
  74. // Show by default if a date range is defined in store
  75. if (accessProfileStore.historical.dateStart || accessProfileStore.historical.dateEnd) {
  76. show.value = true
  77. emit('showDateTimeRange', true)
  78. }
  79. </script>
  80. <style scoped lang="scss">
  81. .time-btn {
  82. border-width: 1px 1px 1px 0;
  83. border-style: solid;
  84. }
  85. </style>