fix : page de modification reception qui crash en prod

This commit is contained in:
tristan
2026-02-26 10:28:00 +01:00
parent c48cc477da
commit 59d76c5f14
4 changed files with 236 additions and 57 deletions
+51 -5
View File
@@ -48,8 +48,38 @@ const emit = defineEmits<{
const bovineTypes = ref<BovineTypeData[]>([])
const localQuantities = reactive<Record<string, number | null>>({})
const localOtherQuantity = ref<number | null>(props.otherQuantity ?? 0)
// Verrou pour éviter les boucles props -> local -> emit -> props.
const isSyncing = ref(false)
function entriesEqualByTypeAndQuantity(
left: ReceptionBovineTypeData[],
right: ReceptionBovineTypeData[]
): boolean {
const toMap = (entries: ReceptionBovineTypeData[]) => {
const map = new Map<number, number>()
for (const entry of entries) {
const typeId = entry.bovineType?.id ?? 0
map.set(typeId, entry.quantity ?? 0)
}
return map
}
const a = toMap(left)
const b = toMap(right)
if (a.size !== b.size) {
return false
}
for (const [typeId, quantity] of a.entries()) {
if ((b.get(typeId) ?? 0) !== quantity) {
return false
}
}
return true
}
function buildEntriesFromLocal(): ReceptionBovineTypeData[] {
return bovineTypes.value.map((type) => {
const existing = props.modelValue.find((entry) => entry.bovineType.id === type.id)
@@ -80,20 +110,33 @@ function syncLocalFromProps() {
watch(
() => props.otherQuantity,
(value) => {
localOtherQuantity.value = value ?? 0
if (isSyncing.value) {
return
}
const next = value ?? 0
isSyncing.value = true
localOtherQuantity.value = next
isSyncing.value = false
}
)
watch(localOtherQuantity, (value) => {
emit('update:otherQuantity', value ?? 0)
if (isSyncing.value) {
return
}
const next = value ?? 0
emit('update:otherQuantity', next)
})
watch(
() => props.modelValue,
() => {
// Hydratation locale uniquement quand le parent change.
syncLocalFromProps()
},
{ deep: true }
{ immediate: true }
)
watch(
@@ -102,7 +145,11 @@ watch(
if (isSyncing.value) {
return
}
emit('update:modelValue', buildEntriesFromLocal())
// N'émet que si les quantités diffèrent réellement du parent.
const nextEntries = buildEntriesFromLocal()
if (!entriesEqualByTypeAndQuantity(nextEntries, props.modelValue)) {
emit('update:modelValue', nextEntries)
}
},
{ deep: true }
)
@@ -110,6 +157,5 @@ watch(
onMounted(async () => {
bovineTypes.value = await getBovineTypeList()
syncLocalFromProps()
emit('update:modelValue', buildEntriesFromLocal())
})
</script>