diff --git a/js/admin.js b/js/admin.js index d01a804..ec91901 100644 --- a/js/admin.js +++ b/js/admin.js @@ -392,16 +392,39 @@ async function handleCritiqueUpload(input) { let allData = []; for (const file of input.files) { try { allData = allData.concat(parseCSV(await file.text())); } catch(e) {} } if (allData.length === 0) { input.value = ''; return alert('❌ Fichier vide.'); } + showImportModal(allData.length, 'critique'); let processed = 0; + for (let i = 0; i < allData.length; i += 10) { const batch = allData.slice(i, i + 10); try { - await fetch(`${API_URL}?action=import_batch`, { method: 'POST', headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' }, body: JSON.stringify({ items: batch, type: 'critique' }) }); - } catch (err) { console.error(err); } + const response = await fetch(`${API_URL}?action=import_batch`, { + method: 'POST', + headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' }, + body: JSON.stringify({ items: batch, type: 'critique' }) + }); + + // 🔥 VÉRIFICATION DE LA RÉPONSE SERVEUR + if (!response.ok) { + const errText = await response.text(); + throw new Error(`Erreur serveur ${response.status}`); + } + const result = await response.json(); + if (!result.success) { + throw new Error(result.error || "Erreur inconnue lors de l'import."); + } + } catch (err) { + console.error(err); + closeImportModal(); + alert("❌ Échec de l'import : " + err.message); + input.value = ''; + return; // Stoppe l'import en cas d'erreur + } processed += batch.length; updateImportModal(processed, allData.length); } + input.value = ''; closeImportModal(); showSuccessModal(`${allData.length} critique(s) importée(s) avec succès.`); @@ -439,19 +462,39 @@ async function handleVideothequeUpload(input) { for (const file of input.files) { try { allData = allData.concat(parseCSV(await file.text())); } catch(e) {} } if (allData.length === 0) { input.value = ''; return alert('❌ Fichier vide.'); } - // 🔥 Application du nettoyage avant envoi au serveur allData = allData.map(normalizeVideothequeRow); - showImportModal(allData.length, 'videotheque'); let processed = 0; + for (let i = 0; i < allData.length; i += 10) { const batch = allData.slice(i, i + 10); try { - await fetch(`${API_URL}?action=import_batch`, { method: 'POST', headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' }, body: JSON.stringify({ items: batch, type: 'videotheque' }) }); - } catch (err) { console.error(err); } + const response = await fetch(`${API_URL}?action=import_batch`, { + method: 'POST', + headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' }, + body: JSON.stringify({ items: batch, type: 'videotheque' }) + }); + + // 🔥 VÉRIFICATION DE LA RÉPONSE SERVEUR + if (!response.ok) { + const errText = await response.text(); + throw new Error(`Erreur serveur ${response.status}`); + } + const result = await response.json(); + if (!result.success) { + throw new Error(result.error || "Erreur inconnue lors de l'import."); + } + } catch (err) { + console.error(err); + closeImportModal(); + alert("❌ Échec de l'import : " + err.message); + input.value = ''; + return; // Stoppe l'import en cas d'erreur + } processed += batch.length; updateImportModal(processed, allData.length); } + input.value = ''; closeImportModal(); showSuccessModal(`${allData.length} support(s) importé(s) avec succès.`);