ActivityYear.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 type { ComputedRef } from 'vue'
  31. import { useDisplay } from 'vuetify'
  32. import { useEntityManager } from '~/composables/data/useEntityManager'
  33. import { useFormStore } from '~/stores/form'
  34. import { useOrganizationProfileStore } from '~/stores/organizationProfile'
  35. import { useAccessProfileStore } from '~/stores/accessProfile'
  36. import Access from '~/models/Access/Access'
  37. import { usePageStore } from '~/stores/page'
  38. import { useRefreshProfile } from '~/composables/data/useRefreshProfile'
  39. const { em } = useEntityManager()
  40. const accessProfileStore = useAccessProfileStore()
  41. const organizationProfileStore = useOrganizationProfileStore()
  42. const formStore = useFormStore()
  43. const pageStore = usePageStore()
  44. const { mdAndUp } = useDisplay()
  45. const { refreshProfile } = useRefreshProfile()
  46. const currentActivityYear: ComputedRef<number | undefined> = computed(
  47. () => accessProfileStore.activityYear ?? undefined,
  48. )
  49. const yearPlusOne: boolean = !organizationProfileStore.isManagerProduct
  50. const label: string = organizationProfileStore.isSchool
  51. ? 'schooling_year'
  52. : organizationProfileStore.isArtist
  53. ? 'season_year'
  54. : 'cotisation_year'
  55. /**
  56. * Persist a new activityYear
  57. * @param event
  58. */
  59. const setActivityYear = async (event: string) => {
  60. const activityYear = parseInt(event)
  61. if (!(activityYear > 1900) || !(activityYear <= 2100)) {
  62. throw new Error("Error: 'year' shall be a valid year")
  63. }
  64. formStore.setDirty(false)
  65. pageStore.loading = true
  66. await em.patch(Access, accessProfileStore.currentAccessId, {
  67. activityYear,
  68. })
  69. if (import.meta.server) {
  70. // Force profile refresh server side to avoid a bug where server and client stores diverge on profile refresh
  71. await refreshProfile()
  72. }
  73. window.location.reload()
  74. }
  75. </script>
  76. <style lang="scss">
  77. .activity-year-input {
  78. width: 120px;
  79. max-height: 20px;
  80. .v-input {
  81. min-width: 70px;
  82. }
  83. input {
  84. font-size: 14px;
  85. width: 55px !important;
  86. padding: 0 2px 0 6px !important;
  87. margin-top: 0 !important;
  88. min-height: 24px;
  89. height: 24px;
  90. }
  91. }
  92. </style>