DataTiming.vue 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <main class="d-flex align-baseline">
  3. <span v-show="mdAndUp" class="mr-2 font-weight-bold on-neutral">{{ $t('display_data') }} : </span>
  4. <v-btn-toggle
  5. ref="toggle"
  6. :model-value="historicalValue"
  7. density="compact"
  8. :color="color"
  9. multiple
  10. divider
  11. border
  12. :rounded="true"
  13. class="toggle-btn"
  14. @update:modelValue="onUpdate"
  15. >
  16. <v-btn
  17. v-for="choice in historicalChoices"
  18. :value="choice"
  19. max-height="25"
  20. :class="'font-weight-normal text-caption ' + (historicalValue.includes(choice) ? 'theme-primary' : 'theme-neutral-soft')"
  21. >
  22. <!-- TODO: on ne devrait pas avoir besoin du if et de la classe 'btn-selected' dans v-btn, mais à l'heure
  23. qu'il est, le component ne fonctionne pas comme attendu. A revoir quand vuetify 3 sera plus stable -->
  24. {{ $t(choice) }}
  25. </v-btn>
  26. </v-btn-toggle>
  27. </main>
  28. </template>
  29. <script setup lang="ts">
  30. import {useFormStore} from "~/stores/form";
  31. import {useAccessProfileStore} from "~/stores/accessProfile";
  32. import type {Ref} from "@vue/reactivity";
  33. import {useEntityManager} from "~/composables/data/useEntityManager";
  34. import {useDisplay, useTheme} from "vuetify";
  35. import Access from "~/models/Access/Access";
  36. import {usePageStore} from "~/stores/page";
  37. import {useRefreshProfile} from "~/composables/data/useRefreshProfile";
  38. // TODO: en v3.0.5, pas de solution documentée pour renseigner directement la couleur dans le template, à revoir
  39. const color = useTheme().current.value.colors['primary']
  40. const { setDirty } = useFormStore()
  41. const accessProfileStore = useAccessProfileStore()
  42. const { em } = useEntityManager()
  43. const { mdAndUp } = useDisplay()
  44. const pageStore = usePageStore()
  45. const { refreshProfile } = useRefreshProfile()
  46. const toggle = ref(null)
  47. const historicalChoices: Array<'past' | 'present' | 'future'> = ['past', 'present', 'future']
  48. const historicalValue: Ref<Array<string>> = ref(historicalChoices.filter((item) => accessProfileStore.historical[item]))
  49. const onUpdate = async (newValue: Array<string>) => {
  50. historicalValue.value = newValue
  51. const accessId = accessProfileStore.switchId ?? accessProfileStore.id
  52. if (accessId === null) {
  53. throw new Error('Invalid profile id')
  54. }
  55. accessProfileStore.setHistorical(
  56. historicalValue.value.includes('past'),
  57. historicalValue.value.includes('present'),
  58. historicalValue.value.includes('future')
  59. )
  60. setDirty(false)
  61. pageStore.loading = true
  62. await em.patch(Access, accessId, {'historical': accessProfileStore.historical})
  63. if (process.server) {
  64. // Force profile refresh server side to avoid a bug where server and client stores diverge on profile refresh
  65. await refreshProfile()
  66. }
  67. window.location.reload()
  68. }
  69. </script>
  70. <style scoped lang="scss">
  71. .v-btn-group {
  72. max-height: 22px;
  73. }
  74. .v-btn {
  75. padding: 0 8px;
  76. }
  77. .v-btn.btn-selected {
  78. background-color: rgb(var(--v-theme-primary)) !important;
  79. }
  80. </style>