diff --git a/js/admin.js b/js/admin.js index 209aeae..a94fc35 100644 --- a/js/admin.js +++ b/js/admin.js @@ -32,12 +32,14 @@ function initEventListeners() { }); } +/ Remplacez la fonction loadDashboardData existante par celle-ci async function loadDashboardData() { try { - const res = await fetch(`${API_URL}?action=get_films`); + // NOUVEAU : cache: 'no-store' oblige le navigateur à récupérer la vraie donnée + const res = await fetch(`${API_URL}?action=get_films`, { cache: 'no-store' }); allItems = await res.json(); - const secRes = await fetch(`${API_URL}?action=check_security_status`); + const secRes = await fetch(`${API_URL}?action=check_security_status`, { cache: 'no-store' }); const secData = await secRes.json(); const banner = document.getElementById('security-banner'); if (banner) banner.style.display = secData.is_blank ? 'flex' : 'none'; @@ -114,35 +116,49 @@ function closeConfirmModal() { pendingDeleteAction = null; } -// ── ACTIONS CRUD (SUPPRESSION) ── +// Remplacez la fonction executeBulkDelete existante par celle-ci async function executeBulkDelete() { const ids = Array.from(document.querySelectorAll('.film-checkbox:checked')).map(cb => cb.value); if (ids.length === 0) return; showConfirmModal(async () => { try { - await fetch(`${API_URL}?action=bulk_delete`, { + const res = await fetch(`${API_URL}?action=bulk_delete`, { method: 'POST', headers: { 'Authorization': localStorage.getItem('token'), 'Content-Type': 'application/json' }, body: JSON.stringify({ ids, type: currentAdminTab }) }); + + // NOUVEAU : Si le serveur renvoie une erreur, on lève une exception + if (!res.ok) throw new Error("Erreur du serveur lors de la suppression"); + document.getElementById('bulk-actions-bar').style.display = 'none'; const selectAll = document.getElementById('select-all-checkbox'); if(selectAll) selectAll.checked = false; loadDashboardData(); - } catch (err) { console.error('Erreur bulk delete :', err); } + } catch (err) { + console.error('Erreur bulk delete :', err); + alert("Une erreur est survenue lors de la suppression."); + } }); } +// Remplacez la fonction deleteSingleFilm existante par celle-ci async function deleteSingleFilm(id) { showConfirmModal(async () => { try { - await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, { + const res = await fetch(`${API_URL}?action=delete_film&id=${id}&type=${currentAdminTab}`, { method: 'DELETE', headers: { 'Authorization': localStorage.getItem('token') } }); + + if (!res.ok) throw new Error("Erreur du serveur lors de la suppression unique"); + loadDashboardData(); - } catch (err) { console.error('Erreur delete :', err); } + } catch (err) { + console.error('Erreur delete :', err); + alert("Une erreur est survenue lors de la suppression."); + } }); }