ActivityYear.vue 2.9 KB

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