Fix fournisseur handling across catalog flows
This commit is contained in:
@@ -109,6 +109,7 @@
|
||||
v-if="isEditMode"
|
||||
class="w-full"
|
||||
:model-value="componentConstructeurIds"
|
||||
:initial-options="componentConstructeursDisplay"
|
||||
@update:model-value="handleConstructeurChange"
|
||||
/>
|
||||
<div v-else class="input input-bordered input-sm bg-base-200">
|
||||
|
||||
@@ -142,13 +142,22 @@ const props = defineProps({
|
||||
type: String,
|
||||
default: 'Sélectionner ou créer un fournisseur...',
|
||||
},
|
||||
initialOptions: {
|
||||
type: Array as PropType<ConstructeurSummary[]>,
|
||||
default: () => [],
|
||||
},
|
||||
})
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'update:modelValue', value: string[]): void
|
||||
}>()
|
||||
|
||||
const { constructeurs, searchConstructeurs, createConstructeur } = useConstructeurs()
|
||||
const {
|
||||
constructeurs,
|
||||
searchConstructeurs,
|
||||
createConstructeur,
|
||||
ensureConstructeurs,
|
||||
} = useConstructeurs()
|
||||
const searchTerm = ref('')
|
||||
const openDropdown = ref(false)
|
||||
const openCreateModal = ref(false)
|
||||
@@ -168,8 +177,15 @@ const uniqueOptions = (items: ConstructeurSummary[] = []) => {
|
||||
return Array.from(seen.values())
|
||||
}
|
||||
|
||||
const normalizedInitialOptions = computed(() =>
|
||||
uniqueOptions((props.initialOptions as ConstructeurSummary[]) || []),
|
||||
)
|
||||
|
||||
const applyOptions = (items: ConstructeurSummary[] = []) => {
|
||||
const normalized = uniqueOptions(items)
|
||||
const normalized = uniqueOptions([
|
||||
...normalizedInitialOptions.value,
|
||||
...items,
|
||||
])
|
||||
const limited = normalized.slice(0, 10)
|
||||
|
||||
selectedIds.value.forEach((id) => {
|
||||
@@ -186,7 +202,10 @@ const applyOptions = (items: ConstructeurSummary[] = []) => {
|
||||
}
|
||||
})
|
||||
|
||||
options.value = uniqueOptions(limited)
|
||||
options.value = uniqueOptions([
|
||||
...normalizedInitialOptions.value,
|
||||
...limited,
|
||||
])
|
||||
}
|
||||
|
||||
const createForm = ref({
|
||||
@@ -197,6 +216,9 @@ const createForm = ref({
|
||||
|
||||
const optionLookup = computed(() => {
|
||||
const map = new Map<string, ConstructeurSummary>()
|
||||
normalizedInitialOptions.value.forEach((item) => {
|
||||
map.set(item.id, item)
|
||||
})
|
||||
constructeurs.value.forEach((item: ConstructeurSummary) => {
|
||||
map.set(item.id, item)
|
||||
})
|
||||
@@ -336,7 +358,10 @@ watch(
|
||||
}
|
||||
const missing = ids.some((id) => !optionLookup.value.get(id))
|
||||
if (missing) {
|
||||
await ensureOptionsLoaded(true)
|
||||
const fetched = await ensureConstructeurs(ids)
|
||||
if (fetched.length) {
|
||||
applyOptions([...options.value, ...fetched])
|
||||
}
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
@@ -353,6 +378,14 @@ watch(
|
||||
{ immediate: true },
|
||||
)
|
||||
|
||||
watch(
|
||||
normalizedInitialOptions,
|
||||
() => {
|
||||
applyOptions(options.value)
|
||||
},
|
||||
{ immediate: true },
|
||||
)
|
||||
|
||||
onMounted(() => {
|
||||
window.addEventListener('click', clickHandler)
|
||||
ensureOptionsLoaded()
|
||||
|
||||
@@ -100,6 +100,7 @@
|
||||
v-else
|
||||
class="w-full"
|
||||
:model-value="pieceConstructeurIds"
|
||||
:initial-options="pieceConstructeursDisplay"
|
||||
placeholder="Sélectionner un ou plusieurs fournisseurs..."
|
||||
@update:model-value="handleConstructeurChange"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user