From d038b23e391bd980eedd863c997095de422e6433 Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 18 Jun 2026 13:41:19 +0200 Subject: [PATCH] Actualiser js/admin.js --- js/admin.js | 71 +++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/js/admin.js b/js/admin.js index df9446e..f3b1de8 100644 --- a/js/admin.js +++ b/js/admin.js @@ -64,46 +64,47 @@ document.addEventListener('change', (e) => { // Remplacez les deux fonctions de suppression par celles-ci : -async function deleteSingleFilm(id) { - if (!confirm('Supprimer ce film ?')) return; - - const token = localStorage.getItem('token'); - const res = await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, { - method: 'DELETE', - headers: { 'Authorization': token } // <-- Vérifiez que ce token est bien présent - }); - - const data = await res.json(); - console.log("Réponse suppression simple :", data); - loadDashboardData(); +// Variable pour stocker la fonction à appeler après confirmation +let pendingDeleteAction = null; + +function showConfirmModal(actionFn) { + pendingDeleteAction = actionFn; + document.getElementById('confirm-modal').classList.add('open'); } -// 3. Suppression -async function executeBulkDelete() { - const checked = document.querySelectorAll('.film-checkbox:checked'); - const ids = Array.from(checked).map(cb => cb.value); - - if (ids.length === 0) return; - if (!confirm(`Supprimer ces ${ids.length} films ?`)) return; +function closeConfirmModal() { + document.getElementById('confirm-modal').classList.remove('open'); +} - try { - const res = await fetch(`${API_URL}?action=bulk_delete`, { +// Bouton supprimer dans la modale +document.getElementById('confirm-btn').addEventListener('click', () => { + if (pendingDeleteAction) pendingDeleteAction(); + closeConfirmModal(); +}); + +// Mise à jour de la suppression de masse +async function executeBulkDelete() { + showConfirmModal(async () => { + const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value); + await fetch(`${API_URL}?action=bulk_delete`, { method: 'POST', - headers: { - 'Authorization': localStorage.getItem('token'), - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ ids: ids, type: currentAdminTab }) + headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' }, + body: JSON.stringify({ ids, type: currentAdminTab }) }); - - const data = await res.json(); - if (data.success) { - document.getElementById('bulk-actions-bar').style.display = 'none'; - loadDashboardData(); - } - } catch (err) { - console.error('Erreur bulk delete :', err); - } + loadDashboardData(); + document.getElementById('bulk-actions-bar').style.display = 'none'; + }); +} + +// Mise à jour de la suppression unitaire +async function deleteSingleFilm(id) { + showConfirmModal(async () => { + await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, { + method: 'DELETE', + headers: { 'Authorization': localStorage.getItem('token') } + }); + loadDashboardData(); + }); } function switchAdminTab(tabName) {