diff --git a/src/components/MacchineTree.tsx b/src/components/MacchineTree.tsx index 42014e1..fe9b13c 100644 --- a/src/components/MacchineTree.tsx +++ b/src/components/MacchineTree.tsx @@ -585,10 +585,42 @@ export default function MacchineTree() { macchinaStabilimentoId={showArticlesModal.macchinaStabilimentoId} articoli={showArticlesModal.articoli} onClose={() => setShowArticlesModal(null)} - onArticoloRimosso={() => { - fetchArticoliPerMacchina(); + onArticoloRimosso={async () => { + await fetchArticoliPerMacchina(); window.dispatchEvent(new CustomEvent("articoli-updated")); - setShowArticlesModal(null); + // Aggiorna gli articoli nella modale invece di chiuderla + const macchina = nodes.find((n) => n.id === showArticlesModal.macchinaId); + if (macchina) { + // Ricarica gli articoli per questa macchina + try { + const res = await fetch("/api/art-macchine-parti"); + if (res.ok) { + const data = await res.json(); + const newArticlesByMacchina = data.articoliPerMacchina || {}; + + const getDescendantArticles = (id: number): ArticoloAssegnatoMacchina[] => { + const children = nodes.filter((n) => n.id_padre === id); + let result: ArticoloAssegnatoMacchina[] = []; + for (const child of children) { + const childArticles = newArticlesByMacchina[child.id] || []; + result = [...result, ...childArticles, ...getDescendantArticles(child.id)]; + } + return result; + }; + + const articoliNodo = newArticlesByMacchina[showArticlesModal.macchinaId] || []; + const allArticoli = [...articoliNodo, ...getDescendantArticles(showArticlesModal.macchinaId)]; + + setShowArticlesModal({ + ...showArticlesModal, + articoli: allArticoli, + }); + } + } catch (err) { + console.error("Errore ricarica articoli:", err); + setShowArticlesModal(null); + } + } }} /> )}