Fix fournisseur handling across catalog flows
This commit is contained in:
@@ -5,6 +5,42 @@ import { useToast } from './useToast'
|
||||
const constructeurs = ref([])
|
||||
const loading = ref(false)
|
||||
|
||||
const uniqueConstructeurs = (items = []) => {
|
||||
const map = new Map()
|
||||
items.forEach((item) => {
|
||||
if (item && typeof item === 'object' && typeof item.id === 'string') {
|
||||
map.set(item.id, item)
|
||||
}
|
||||
})
|
||||
return Array.from(map.values())
|
||||
}
|
||||
|
||||
const normalizeIds = (ids = []) => {
|
||||
if (!Array.isArray(ids)) {
|
||||
return []
|
||||
}
|
||||
return Array.from(
|
||||
new Set(
|
||||
ids
|
||||
.map((value) => (typeof value === 'string' ? value.trim() : ''))
|
||||
.filter((value) => value.length > 0),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const upsertConstructeurs = (items = []) => {
|
||||
if (!Array.isArray(items) || !items.length) {
|
||||
return
|
||||
}
|
||||
const merged = uniqueConstructeurs([...constructeurs.value, ...items])
|
||||
constructeurs.value = merged
|
||||
}
|
||||
|
||||
const getIndexedConstructeur = (id) =>
|
||||
constructeurs.value.find((item) => item && item.id === id) || null
|
||||
|
||||
const pendingFetches = new Map()
|
||||
|
||||
export function useConstructeurs () {
|
||||
const { get, post, patch, delete: del } = useApi()
|
||||
const { showSuccess, showError } = useToast()
|
||||
@@ -15,7 +51,8 @@ export function useConstructeurs () {
|
||||
const query = search ? `?search=${encodeURIComponent(search)}` : ''
|
||||
const result = await get(`/constructeurs${query}`)
|
||||
if (result.success) {
|
||||
constructeurs.value = result.data
|
||||
const items = Array.isArray(result.data) ? result.data : []
|
||||
constructeurs.value = uniqueConstructeurs(items)
|
||||
}
|
||||
return result
|
||||
} catch (error) {
|
||||
@@ -35,7 +72,7 @@ export function useConstructeurs () {
|
||||
try {
|
||||
const result = await post('/constructeurs', data)
|
||||
if (result.success) {
|
||||
constructeurs.value = [result.data, ...constructeurs.value]
|
||||
upsertConstructeurs([result.data])
|
||||
showSuccess(`Fournisseur "${result.data.name}" créé`)
|
||||
} else if (result.error) {
|
||||
showError(result.error)
|
||||
@@ -50,15 +87,65 @@ export function useConstructeurs () {
|
||||
}
|
||||
}
|
||||
|
||||
const ensureConstructeurs = async (ids = []) => {
|
||||
const normalizedIds = normalizeIds(ids)
|
||||
if (!normalizedIds.length) {
|
||||
return []
|
||||
}
|
||||
|
||||
const collected = []
|
||||
const missing = []
|
||||
normalizedIds.forEach((id) => {
|
||||
const existing = getIndexedConstructeur(id)
|
||||
if (existing) {
|
||||
collected.push(existing)
|
||||
} else {
|
||||
missing.push(id)
|
||||
}
|
||||
})
|
||||
|
||||
if (missing.length) {
|
||||
const fetchTasks = missing.map((id) => {
|
||||
const cached = pendingFetches.get(id)
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
const task = get(`/constructeurs/${id}`)
|
||||
.then((result) => {
|
||||
if (result.success && result.data) {
|
||||
return result.data
|
||||
}
|
||||
return null
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Erreur lors du chargement du fournisseur:', error)
|
||||
return null
|
||||
})
|
||||
.finally(() => {
|
||||
pendingFetches.delete(id)
|
||||
})
|
||||
pendingFetches.set(id, task)
|
||||
return task
|
||||
})
|
||||
|
||||
const fetched = await Promise.all(fetchTasks)
|
||||
const validFetched = fetched.filter((item) => item && item.id)
|
||||
if (validFetched.length) {
|
||||
upsertConstructeurs(validFetched)
|
||||
}
|
||||
}
|
||||
|
||||
return normalizedIds
|
||||
.map((id) => getIndexedConstructeur(id))
|
||||
.filter((item) => Boolean(item))
|
||||
}
|
||||
|
||||
const updateConstructeur = async (id, data) => {
|
||||
loading.value = true
|
||||
try {
|
||||
const result = await patch(`/constructeurs/${id}`, data)
|
||||
if (result.success) {
|
||||
const index = constructeurs.value.findIndex(item => item.id === id)
|
||||
if (index !== -1) {
|
||||
constructeurs.value[index] = result.data
|
||||
}
|
||||
upsertConstructeurs([result.data])
|
||||
showSuccess(`Fournisseur "${result.data.name}" mis à jour`)
|
||||
} else if (result.error) {
|
||||
showError(result.error)
|
||||
@@ -93,7 +180,7 @@ export function useConstructeurs () {
|
||||
}
|
||||
}
|
||||
|
||||
const getConstructeurById = id => constructeurs.value.find(item => item.id === id)
|
||||
const getConstructeurById = (id) => getIndexedConstructeur(id)
|
||||
|
||||
return {
|
||||
constructeurs,
|
||||
@@ -103,6 +190,7 @@ export function useConstructeurs () {
|
||||
createConstructeur,
|
||||
updateConstructeur,
|
||||
deleteConstructeur,
|
||||
getConstructeurById
|
||||
getConstructeurById,
|
||||
ensureConstructeurs,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user