diff --git a/test.html b/test.html index 6293550..5f2f24a 100644 --- a/test.html +++ b/test.html @@ -18,7 +18,7 @@

Modification en masse

- +
@@ -37,24 +37,28 @@ let fullProductList = []; let categories = {}; - // 1. Initialisation : Chargement des données - 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; - - const catSel = document.getElementById('cat-mass'); - Object.entries(categories).forEach(([key, val]) => { - let opt = document.createElement('option'); - opt.value = key; opt.textContent = val; - catSel.appendChild(opt); + // 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; + + const catSel = document.getElementById('cat-mass'); + catSel.innerHTML = ''; + Object.entries(categories).forEach(([key, val]) => { + let opt = document.createElement('option'); + opt.value = key; opt.textContent = val; + catSel.appendChild(opt); + }); + renderTable(); }); - renderTable(); - }); + } + + loadData(); - // 2. Affichage du tableau function renderTable() { const tbody = document.querySelector('#prod-table tbody'); tbody.innerHTML = ""; @@ -72,32 +76,39 @@ document.querySelectorAll('.prod-check').forEach(c => c.checked = source.checked); } - // 3. Sauvegarde en masse async function applyToSelected() { const newCat = document.getElementById('cat-mass').value; if(!newCat) return alert("Sélectionne une catégorie d'abord !"); const checkedBoxes = document.querySelectorAll('.prod-check:checked'); if(checkedBoxes.length === 0) return alert("Coche des produits !"); - + + const btn = document.getElementById('save-btn'); + btn.disabled = true; + btn.textContent = "Traitement en cours..."; + for (const checkbox of checkedBoxes) { let p = fullProductList[checkbox.dataset.index]; - p.category = newCat; + p.category = newCat; - console.log("Envoi au serveur :", JSON.stringify(p)); // <--- Affiche ce qui est envoyé - - let response = await fetch('api/index.php?action=product_save', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(p) - }); - - let result = await response.json(); - console.log("Résultat serveur :", result); // <--- Affiche ce que le serveur répond + try { + 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)); + } catch (e) { + console.error("Erreur sur " + p.name, e); + } } - alert("Traitement terminé. Regarde la console (F12) pour voir si le serveur a validé."); - renderTable(); // Recharge l'affichage + // Rechargement total pour synchroniser l'affichage + await loadData(); + btn.disabled = false; + btn.textContent = "APPLIQUER AUX COCHÉS"; + alert("Traitement terminé."); }