DataTimingRange.vue 2.9 KB

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