EventCategories.vue 3.8 KB

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