diff --git a/test.html b/test.html index 5f2f24a..db2e311 100644 --- a/test.html +++ b/test.html @@ -37,14 +37,14 @@ let fullProductList = []; let categories = {}; - // Chargement initial - function loadData() { - return Promise.all([ - fetch('translations/fr.json').then(r => r.json()), - fetch('api/index.php?action=products_list').then(r => r.json()) - ]).then(([trans, prods]) => { - categories = trans.categories || {}; - fullProductList = prods.inventory || prods.products || prods; + async function loadData() { + try { + const [transRes, prodsRes] = await Promise.all([ + fetch('translations/fr.json').then(r => r.json()), + fetch('api/index.php?action=products_list').then(r => r.json()) + ]); + categories = transRes.categories || {}; + fullProductList = prodsRes.inventory || prodsRes.products || prodsRes; const catSel = document.getElementById('cat-mass'); catSel.innerHTML = ''; @@ -54,7 +54,7 @@ catSel.appendChild(opt); }); renderTable(); - }); + } catch (e) { console.error("Erreur chargement:", e); } } loadData(); @@ -78,7 +78,7 @@ async function applyToSelected() { const newCat = document.getElementById('cat-mass').value; - if(!newCat) return alert("Sélectionne une catégorie d'abord !"); + if(!newCat) return alert("Sélectionne une catégorie !"); const checkedBoxes = document.querySelectorAll('.prod-check:checked'); if(checkedBoxes.length === 0) return alert("Coche des produits !"); @@ -88,23 +88,24 @@ btn.textContent = "Traitement en cours..."; for (const checkbox of checkedBoxes) { - let p = fullProductList[checkbox.dataset.index]; + // On crée une COPIE de l'objet pour ne pas modifier la référence originale par erreur + let p = { ...fullProductList[checkbox.dataset.index] }; p.category = newCat; try { - await fetch('api/index.php?action=product_save', { + let res = await fetch('api/index.php?action=product_save', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(p) }); - // Pause de 250ms pour laisser le serveur souffler - await new Promise(r => setTimeout(r, 250)); + + if (!res.ok) console.error("Erreur serveur sur " + p.name); + await new Promise(r => setTimeout(r, 200)); } catch (e) { - console.error("Erreur sur " + p.name, e); + console.error("Erreur réseau sur " + p.name, e); } } - // Rechargement total pour synchroniser l'affichage await loadData(); btn.disabled = false; btn.textContent = "APPLIQUER AUX COCHÉS";