EventCategories.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <UiInputTreeSelect
  3. :model-value="modelValue"
  4. :items="hierarchicalItems"
  5. :label="$t(label)"
  6. v-bind="$attrs"
  7. :loading="status === FETCHING_STATUS.PENDING"
  8. @update:model-value="$emit('update:modelValue', $event)"
  9. :max-visible-chips="6"
  10. />
  11. </template>
  12. <script setup lang="ts">
  13. import { computed, onBeforeUnmount, type PropType } from 'vue'
  14. import { useEntityFetch } from '~/composables/data/useEntityFetch'
  15. import EventCategory from '~/models/Core/EventCategory'
  16. import {FETCHING_STATUS} from "~/types/enum/data";
  17. const props = defineProps({
  18. modelValue: {
  19. type: Array as PropType<string[]>,
  20. required: true,
  21. },
  22. /**
  23. * Label du champ
  24. * Si non défini, c'est le nom de propriété qui est utilisé
  25. */
  26. label: {
  27. type: String,
  28. required: false,
  29. default: null,
  30. },
  31. })
  32. const i18n = useI18n()
  33. const emit = defineEmits(['update:modelValue'])
  34. const { fetchCollection } = useEntityFetch()
  35. const { data: categories, status } = fetchCollection(EventCategory)
  36. // Transform event categories into hierarchical items for TreeSelect
  37. const hierarchicalItems = computed(() => {
  38. if (!categories.value || !categories.value.items || categories.value.items.length === 0) {
  39. return []
  40. }
  41. const result = []
  42. const familiesMap = new Map()
  43. const subFamiliesMap = new Map()
  44. // First pass: collect all unique families and subfamilies
  45. categories.value.items.forEach((category) => {
  46. if (
  47. !category.famillyLabel ||
  48. !category.subfamillyLabel ||
  49. !category.genderLabel
  50. )
  51. return
  52. // Create unique keys for families and subfamilies
  53. const familyKey = category.famillyLabel
  54. const subfamilyKey = `${category.famillyLabel}-${category.subfamillyLabel}`
  55. // Add family if not already added
  56. if (!familiesMap.has(familyKey)) {
  57. familiesMap.set(familyKey, {
  58. id: `family-${familyKey}`,
  59. label: i18n.t(category.famillyLabel),
  60. type: 'category',
  61. level: 0,
  62. })
  63. }
  64. // Add subfamily if not already added
  65. if (!subFamiliesMap.has(subfamilyKey)) {
  66. subFamiliesMap.set(subfamilyKey, {
  67. id: `subfamily-${subfamilyKey}`,
  68. label: i18n.t(category.subfamillyLabel),
  69. type: 'subcategory',
  70. parentId: `family-${familyKey}`,
  71. level: 1,
  72. })
  73. }
  74. })
  75. // Convert families map to array and sort alphabetically by label
  76. const sortedFamilies = Array.from(familiesMap.values()).sort((a, b) =>
  77. a.label.localeCompare(b.label),
  78. )
  79. // Add sorted families to result
  80. sortedFamilies.forEach((family) => {
  81. result.push(family)
  82. })
  83. // Convert subfamilies map to array and sort alphabetically by label
  84. const sortedSubfamilies = Array.from(subFamiliesMap.values()).sort((a, b) =>
  85. a.label.localeCompare(b.label),
  86. )
  87. // Add sorted subfamilies to result
  88. sortedSubfamilies.forEach((subfamily) => {
  89. result.push(subfamily)
  90. })
  91. // Collect all genders first, then sort and add to result
  92. const genders = []
  93. categories.value.items.forEach((category) => {
  94. if (
  95. !category.famillyLabel ||
  96. !category.subfamillyLabel ||
  97. !category.genderLabel
  98. )
  99. return
  100. const subfamilyKey = `${category.famillyLabel}-${category.subfamillyLabel}`
  101. genders.push({
  102. id: `gender-${category.id}`,
  103. label: i18n.t(category.genderLabel),
  104. value: category.id,
  105. type: 'item',
  106. parentId: `subfamily-${subfamilyKey}`,
  107. level: 2,
  108. })
  109. })
  110. // Sort genders alphabetically by label and add to result
  111. genders
  112. .sort((a, b) => a.label.localeCompare(b.label))
  113. .forEach((gender) => {
  114. result.push(gender)
  115. })
  116. return result
  117. })
  118. // Nettoyer les données lors du démontage du composant
  119. onBeforeUnmount(() => {
  120. // Nettoyer les références du store si nécessaire
  121. if (process.client) {
  122. clearNuxtData('/^' + EventCategory + '_many_/')
  123. useRepo(EventCategory).flush()
  124. }
  125. })
  126. </script>
  127. <style scoped lang="scss">
  128. /* No specific styles needed */
  129. </style>