DataTimingRange.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. <UiDateRangePicker
  8. :model-value="datesRange"
  9. :max-height="28"
  10. @update:model-value="updateDateTimeRange"
  11. />
  12. </div>
  13. </main>
  14. </template>
  15. <script setup lang="ts">
  16. import {Ref} from "@vue/reactivity";
  17. import {useAccessProfileStore} from "~/stores/accessProfile";
  18. import {useFormStore} from "~/stores/form";
  19. import {useEntityManager} from "~/composables/data/useEntityManager";
  20. import Access from "~/models/Access/Access";
  21. import DateUtils from "~/services/utils/dateUtils";
  22. import {usePageStore} from "~/stores/page";
  23. const { setDirty } = useFormStore()
  24. const accessProfileStore = useAccessProfileStore()
  25. const { em } = useEntityManager()
  26. const pageStore = usePageStore()
  27. const start = accessProfileStore.historical.dateStart
  28. const end = accessProfileStore.historical.dateEnd
  29. const datesRange: Ref<Array<Date> | null> = ref((start && end) ? [new Date(start), new Date(end)] : null)
  30. const updateDateTimeRange = async (dates: Array<Date>): Promise<any> => {
  31. const accessId = accessProfileStore.switchId ?? accessProfileStore.id
  32. if (accessId === null) {
  33. throw new Error('Invalid profile id')
  34. }
  35. datesRange.value = dates
  36. if (datesRange.value !== null && datesRange.value[0] !== null && datesRange.value[1] !== null) {
  37. accessProfileStore.setHistoricalRange(
  38. DateUtils.formatIsoShortDate(datesRange.value[0]),
  39. DateUtils.formatIsoShortDate(datesRange.value[1])
  40. )
  41. } else {
  42. accessProfileStore.setHistorical(false, true, false)
  43. }
  44. setDirty(false)
  45. pageStore.loading = true
  46. await em.patch(Access, accessId, {'historical': accessProfileStore.historical})
  47. if (process.server) {
  48. // Force profile refresh server side to avoid a bug where server and client stores diverge on profile refresh
  49. await em.refreshProfile()
  50. }
  51. window.location.reload()
  52. }
  53. </script>
  54. <style scoped lang="scss">
  55. .label {
  56. min-width: 150px;
  57. }
  58. </style>