fix(plantes): submitPlant — créer/modifier PlantVariety lors de la soumission du formulaire plante
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -904,11 +904,24 @@ async function submitPlant() {
|
||||
if (submitting.value) return
|
||||
submitting.value = true
|
||||
try {
|
||||
const payload = { ...form, prix_achat: form.prix_achat ?? undefined }
|
||||
if (editPlant.value) {
|
||||
await axios.put(`/api/plants/${editPlant.value.id}`, payload)
|
||||
// Extraire les champs variété du form (non envoyés dans Plant)
|
||||
const varietyPayload: Partial<import('@/api/plants').PlantVariety> = {
|
||||
variete: form.variete || undefined,
|
||||
boutique_nom: form.boutique_nom || undefined,
|
||||
boutique_url: form.boutique_url || undefined,
|
||||
prix_achat: form.prix_achat ?? undefined,
|
||||
date_achat: form.date_achat || undefined,
|
||||
poids: form.poids || undefined,
|
||||
dluo: form.dluo || undefined,
|
||||
}
|
||||
// Payload Plant pur (sans champs variété)
|
||||
const { variete: _v, boutique_nom: _bn, boutique_url: _bu, prix_achat: _pa,
|
||||
date_achat: _da, poids: _po, dluo: _d, ...plantPayload } = { ...form }
|
||||
|
||||
// Synchroniser les associations à toutes les variétés du même nom commun
|
||||
if (editPlant.value) {
|
||||
await axios.put(`/api/plants/${editPlant.value.id}`, plantPayload)
|
||||
|
||||
// Synchroniser associations aux plantes du même nom commun
|
||||
const nomKey = form.nom_commun.toLowerCase()
|
||||
const siblings = plantsStore.plants.filter(
|
||||
p => p.id !== editPlant.value!.id && (p.nom_commun || '').toLowerCase() === nomKey
|
||||
@@ -920,10 +933,26 @@ async function submitPlant() {
|
||||
})
|
||||
}
|
||||
|
||||
// Mettre à jour ou créer la variété
|
||||
const existingVariety = editPlant.value.varieties?.[0]
|
||||
const hasVarietyData = Object.values(varietyPayload).some(v => v !== undefined)
|
||||
const plantId = editPlant.value.id!
|
||||
if (existingVariety?.id) {
|
||||
await plantsStore.updateVariety(plantId, existingVariety.id!, varietyPayload)
|
||||
} else if (hasVarietyData) {
|
||||
await plantsStore.createVariety(plantId, varietyPayload)
|
||||
}
|
||||
|
||||
await plantsStore.fetchAll()
|
||||
toast.success('Plante modifiée')
|
||||
} else {
|
||||
await plantsStore.create(payload)
|
||||
const created = await plantsStore.create(plantPayload)
|
||||
// Créer la variété si des données variété sont présentes
|
||||
const hasVarietyData = Object.values(varietyPayload).some(v => v !== undefined)
|
||||
if (created.id && hasVarietyData) {
|
||||
await plantsStore.createVariety(created.id, varietyPayload)
|
||||
}
|
||||
await plantsStore.fetchAll()
|
||||
toast.success('Plante créée')
|
||||
}
|
||||
closeForm()
|
||||
|
||||
Reference in New Issue
Block a user