ActivityYear.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <main class="d-flex flex-row align-center">
  3. <span v-show="mdAndUp" class="mr-2 font-weight-bold on-neutral">
  4. {{ $t(label) }} :
  5. </span>
  6. <UiXeditableText
  7. class="activity-year-input"
  8. type="number"
  9. :data="currentActivityYear"
  10. @update="setActivityYear"
  11. >
  12. <template #xeditable.read="{ inputValue }">
  13. <div class="d-flex align-center on-neutral--clickable">
  14. <v-icon
  15. aria-hidden="false"
  16. size="small"
  17. class="mr-1"
  18. icon="fas fa-edit"
  19. />
  20. <strong>
  21. {{ inputValue }}
  22. <span v-if="yearPlusOne"> / {{ parseInt(inputValue) + 1 }} </span>
  23. </strong>
  24. </div>
  25. </template>
  26. </UiXeditableText>
  27. </main>
  28. </template>
  29. <script setup lang="ts">
  30. import { useDisplay } from 'vuetify'
  31. import { useEntityManager } from '~/composables/data/useEntityManager'
  32. import { useFormStore } from '~/stores/form'
  33. import { useOrganizationProfileStore } from '~/stores/organizationProfile'
  34. import { useAccessProfileStore } from '~/stores/accessProfile'
  35. import Access from '~/models/Access/Access'
  36. import { usePageStore } from '~/stores/page'
  37. import { useRefreshProfile } from '~/composables/data/useRefreshProfile'
  38. const { em } = useEntityManager()
  39. const accessProfileStore = useAccessProfileStore()
  40. const organizationProfileStore = useOrganizationProfileStore()
  41. const formStore = useFormStore()
  42. const pageStore = usePageStore()
  43. const { mdAndUp } = useDisplay()
  44. const { refreshProfile } = useRefreshProfile()
  45. const currentActivityYear: ComputedRef<number | undefined> = computed(
  46. () => accessProfileStore.activityYear ?? undefined,
  47. )
  48. const yearPlusOne: boolean = !organizationProfileStore.isManagerProduct
  49. const label: string = organizationProfileStore.isSchool
  50. ? 'schooling_year'
  51. : organizationProfileStore.isArtist
  52. ? 'season_year'
  53. : 'cotisation_year'
  54. /**
  55. * Persist a new activityYear
  56. * @param event
  57. */
  58. const setActivityYear = async (event: string) => {
  59. const activityYear = parseInt(event)
  60. if (!(activityYear > 1900) || !(activityYear <= 2100)) {
  61. throw new Error("Error: 'year' shall be a valid year")
  62. }
  63. formStore.setDirty(false)
  64. pageStore.loading = true
  65. await em.patch(Access, accessProfileStore.currentAccessId, {
  66. activityYear,
  67. })
  68. if (import.meta.server) {
  69. // Force profile refresh server side to avoid a bug where server and client stores diverge on profile refresh
  70. await refreshProfile()
  71. }
  72. window.location.reload()
  73. }
  74. </script>
  75. <style lang="scss">
  76. .activity-year-input {
  77. width: 120px;
  78. max-height: 20px;
  79. .v-input {
  80. min-width: 70px;
  81. }
  82. input {
  83. font-size: 14px;
  84. width: 55px !important;
  85. padding: 0 2px 0 6px !important;
  86. margin-top: 0 !important;
  87. min-height: 24px;
  88. height: 24px;
  89. }
  90. }
  91. </style>