ActivityYear.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <main class="d-flex flex-row align-center">
  3. <span v-show="mdAndUp"
  4. class="mr-2 font-weight-bold on-neutral">
  5. {{ $t(label) }} :
  6. </span>
  7. <UiXeditableText
  8. class="activity-year-input"
  9. type="number"
  10. :data="currentActivityYear"
  11. @update="setActivityYear"
  12. >
  13. <template #xeditable.read="{inputValue}">
  14. <div class="d-flex align-center on-neutral--clickable">
  15. <v-icon aria-hidden="false" size="small" class="mr-1" icon="fas fa-edit" />
  16. <strong >
  17. {{ inputValue }}
  18. <span v-if="yearPlusOne">
  19. / {{ parseInt(inputValue) + 1 }}
  20. </span>
  21. </strong>
  22. </div>
  23. </template>
  24. </UiXeditableText>
  25. </main>
  26. </template>
  27. <script setup lang="ts">
  28. import {useEntityManager} from "~/composables/data/useEntityManager";
  29. import {useFormStore} from "~/stores/form";
  30. import {useOrganizationProfileStore} from "~/stores/organizationProfile";
  31. import {useAccessProfileStore} from "~/stores/accessProfile";
  32. import Access from "~/models/Access/Access";
  33. import {Ref, ref} from "@vue/reactivity";
  34. import {useDisplay} from "vuetify";
  35. const { em } = useEntityManager()
  36. const accessProfileStore = useAccessProfileStore()
  37. const organizationProfileStore = useOrganizationProfileStore()
  38. const formStore = useFormStore()
  39. const { mdAndUp } = useDisplay()
  40. const currentActivityYear: Ref<number | undefined> = ref(accessProfileStore.activityYear ?? undefined)
  41. const yearPlusOne: boolean = !organizationProfileStore.isManagerProduct
  42. const label: string = organizationProfileStore.isSchool ? 'schooling_year' : organizationProfileStore.isArtist ? 'season_year' : 'cotisation_year'
  43. /**
  44. * Persist a new activityYear
  45. * @param activityYear
  46. */
  47. const setActivityYear = async (activityYear: number) => {
  48. if (!(1900 < activityYear) || !(activityYear <= 2100)) {
  49. throw new Error("Error: 'year' shall be a valid year")
  50. }
  51. if (accessProfileStore.id === null) {
  52. throw new Error("Error: invalid access id")
  53. }
  54. formStore.setDirty(false)
  55. const access = await em.fetch(Access, accessProfileStore.id)
  56. access.activityYear = activityYear
  57. await em.persist(Access, access)
  58. // Update the store
  59. // TODO: voir si mieux d'automatiser ces maj du profil, ou de les faire à la main au cas par cas?
  60. accessProfileStore.$patch({ activityYear: activityYear })
  61. window.location.reload()
  62. }
  63. </script>
  64. <style lang="scss">
  65. .activity-year-input {
  66. width: 120px;
  67. max-height: 20px;
  68. .v-input {
  69. min-width: 70px;
  70. }
  71. input{
  72. font-size: 14px;
  73. width: 55px !important;
  74. padding: 0 !important;
  75. margin-top: 0 !important;
  76. min-height: 24px;
  77. height: 24px;
  78. }
  79. }
  80. </style>