EventCategories.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <UiInputTreeSelect
  3. :model-value="modelValue"
  4. :items="hierarchicalItems"
  5. v-bind="$attrs"
  6. :loading="pending"
  7. @update:model-value="$emit('update:modelValue', $event)"
  8. />
  9. </template>
  10. <script setup lang="ts">
  11. import { useEntityFetch } from '~/composables/data/useEntityFetch'
  12. import EventCategory from '~/models/Core/EventCategory'
  13. const props = defineProps({
  14. modelValue: {
  15. type: Array as PropType<string[]>,
  16. required: true
  17. }
  18. })
  19. const i18n = useI18n()
  20. const { fetchCollection } = useEntityFetch()
  21. const { data: categories, pending } = fetchCollection(EventCategory)
  22. // Transform event categories into hierarchical items for TreeSelect
  23. const hierarchicalItems = computed(() => {
  24. if (!categories.value || categories.value.length === 0) {
  25. return []
  26. }
  27. const result = []
  28. const familiesMap = new Map()
  29. const subFamiliesMap = new Map()
  30. // First pass: collect all unique families and subfamilies
  31. categories.value.items.forEach(category => {
  32. if (!category.famillyLabel || !category.subfamillyLabel || !category.genderLabel) return
  33. // Create unique keys for families and subfamilies
  34. const familyKey = category.famillyLabel
  35. const subfamilyKey = `${category.famillyLabel}-${category.subfamillyLabel}`
  36. // Add family if not already added
  37. if (!familiesMap.has(familyKey)) {
  38. familiesMap.set(familyKey, {
  39. id: `family-${familyKey}`,
  40. label: i18n.t(category.famillyLabel),
  41. type: 'category',
  42. level: 0
  43. })
  44. }
  45. // Add subfamily if not already added
  46. if (!subFamiliesMap.has(subfamilyKey)) {
  47. subFamiliesMap.set(subfamilyKey, {
  48. id: `subfamily-${subfamilyKey}`,
  49. label: i18n.t(category.subfamillyLabel),
  50. type: 'subcategory',
  51. parentId: `family-${familyKey}`,
  52. level: 1
  53. })
  54. }
  55. })
  56. // Convert families map to array and sort alphabetically by label
  57. const sortedFamilies = Array.from(familiesMap.values()).sort((a, b) =>
  58. a.label.localeCompare(b.label)
  59. )
  60. // Add sorted families to result
  61. sortedFamilies.forEach(family => {
  62. result.push(family)
  63. })
  64. // Convert subfamilies map to array and sort alphabetically by label
  65. const sortedSubfamilies = Array.from(subFamiliesMap.values()).sort((a, b) =>
  66. a.label.localeCompare(b.label)
  67. )
  68. // Add sorted subfamilies to result
  69. sortedSubfamilies.forEach(subfamily => {
  70. result.push(subfamily)
  71. })
  72. // Collect all genders first, then sort and add to result
  73. const genders = []
  74. categories.value.items.forEach(category => {
  75. if (!category.famillyLabel || !category.subfamillyLabel || !category.genderLabel) return
  76. const familyKey = category.famillyLabel
  77. const subfamilyKey = `${category.famillyLabel}-${category.subfamillyLabel}`
  78. genders.push({
  79. id: `gender-${category.id}`,
  80. label: i18n.t(category.genderLabel),
  81. value: category.id.toString(),
  82. type: 'item',
  83. parentId: `subfamily-${subfamilyKey}`,
  84. level: 2
  85. })
  86. })
  87. // Sort genders alphabetically by label and add to result
  88. genders.sort((a, b) => a.label.localeCompare(b.label)).forEach(gender => {
  89. result.push(gender)
  90. })
  91. return result
  92. })
  93. </script>
  94. <style scoped lang="scss">
  95. /* No specific styles needed */
  96. </style>