ActivityYear.vue 2.6 KB

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