From 9eeb97a6614909429904cd4a98d364b043e33ce4 Mon Sep 17 00:00:00 2001 From: Cedric Date: Fri, 26 Jun 2026 09:01:22 +0200 Subject: [PATCH] Actualiser js/admin.js --- js/admin.js | 86 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 23 deletions(-) diff --git a/js/admin.js b/js/admin.js index c92f4e6..1f92a5a 100644 --- a/js/admin.js +++ b/js/admin.js @@ -446,30 +446,70 @@ function normalizeVideothequeRow(row) { }); } -async function handleVideothequeUpload(input) { - if (!input.files || input.files.length === 0) return; - 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.'); } - allData = allData.map(normalizeVideothequeRow); - showImportModal(allData.length, 'videotheque'); - let processed = 0; - for (let i = 0; i < allData.length; i += 3) { - const batch = allData.slice(i, i + 3); - try { - 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' }) }); - if (!response.ok) throw new Error(`Erreur serveur ${response.status}`); - const result = await response.json(); - if (!result.success) throw new Error(result.error || "Erreur inconnue."); - } catch (err) { - closeImportModal(); alert("❌ Échec de l'import : " + err.message); input.value = ''; return; +// ── 2. Importation et Mapping du CSV Blu-ray.com ── +function handleVideothequeUpload(input) { + const file = input.files[0]; + if (!file) return; + + const reader = new FileReader(); + reader.onload = async (e) => { + const text = e.target.result; + + // On suppose que ta fonction parseCSV retourne un tableau d'objets avec les en-têtes en clés + const parsedData = parseCSV(text); + + const items = parsedData.map(row => { + // Lecture des colonnes spécifiques du CSV de Blu-ray.com + const title = row['title'] ? row['title'].trim() : ''; + if (!title) return null; + + // On priorise l'EAN, avec un fallback sur l'UPC si l'EAN est vide + let ean = row['ean_isbn13'] ? row['ean_isbn13'].trim() : ''; + if (!ean && row['upc_isbn10']) { + ean = row['upc_isbn10'].trim(); + } + + // Extraction de l'année depuis la date complète + const publishDate = row['publish_date'] ? row['publish_date'].trim() : ''; + const year = publishDate ? publishDate.split('-')[0] : ''; + + const description = row['description'] ? row['description'].trim() : ''; + + return { title, ean, year, description }; + }).filter(item => item !== null); + + if (items.length === 0) { + alert("Aucun film valide trouvé dans le fichier CSV."); + return; } - processed += batch.length; - updateImportModal(processed, allData.length); - } - input.value = ''; closeImportModal(); - showSuccessModal(`${allData.length} support(s) importé(s) avec succès.`); - loadDashboardData(); + + try { + const response = await fetch(`${API_URL}?action=import_batch`, { + method: 'POST', + headers: { + 'Authorization': localStorage.getItem('token'), + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ type: 'videotheque', items }) + }); + const data = await response.json(); + + if (data.success) { + // Utilisation de ta modale de succès + if (typeof showSuccessModal === 'function') { + showSuccessModal(`${data.imported} éditions physiques importées !`); + } else { + alert(`${data.imported} éditions physiques importées !`); + } + loadDashboardData(); + } else { + alert("Erreur lors de l'importation côté serveur."); + } + } catch (err) { + console.error("Erreur d'importation :", err); + } + }; + reader.readAsText(file); } function showImportModal(total, type) {