diff --git a/app/components/ConstructeurSelect.vue b/app/components/ConstructeurSelect.vue index 7bf19d8..311f4e5 100644 --- a/app/components/ConstructeurSelect.vue +++ b/app/components/ConstructeurSelect.vue @@ -246,6 +246,16 @@ const emitSelection = (ids: string[]) => { emit('update:modelValue', normalized) } +const extractDataArray = (data: unknown): ConstructeurSummary[] => { + if (Array.isArray(data)) { + return data as ConstructeurSummary[] + } + if (data && typeof data === 'object' && 'id' in data) { + return [data as ConstructeurSummary] + } + return [] +} + const ensureOptionsLoaded = async (force = false) => { if (!force && !searchTerm.value && constructeurs.value.length) { applyOptions(constructeurs.value as ConstructeurSummary[]) @@ -262,7 +272,7 @@ const ensureOptionsLoaded = async (force = false) => { const result = await searchConstructeurs(searchTerm.value) if (result.success) { - applyOptions(result.data || []) + applyOptions(extractDataArray(result.data)) lastSearchTerm = searchTerm.value } } @@ -283,7 +293,7 @@ const onSearch = () => { } const result = await searchConstructeurs(searchTerm.value) if (result.success) { - applyOptions(result.data || []) + applyOptions(extractDataArray(result.data)) lastSearchTerm = searchTerm.value } }, 250) @@ -310,16 +320,18 @@ const closeCreateModal = () => { const handleCreate = async () => { creating.value = true - const payload = { ...createForm.value } - if (!payload.phone) { - delete payload.phone + const payload: { name: string; email?: string; phone?: string } = { + name: createForm.value.name, } - if (!payload.email) { - delete payload.email + if (createForm.value.email) { + payload.email = createForm.value.email + } + if (createForm.value.phone) { + payload.phone = createForm.value.phone } const result = await createConstructeur(payload) creating.value = false - if (result.success) { + if (result.success && result.data && !Array.isArray(result.data)) { emitSelection([...selectedIds.value, result.data.id]) searchTerm.value = '' closeCreateModal() diff --git a/app/components/ProductSelect.vue b/app/components/ProductSelect.vue index a4bc49c..34c7d21 100644 --- a/app/components/ProductSelect.vue +++ b/app/components/ProductSelect.vue @@ -1,7 +1,7 @@