Actualiser js/admin.js

This commit is contained in:
2026-06-18 16:50:19 +02:00
parent 63a590756f
commit a6249a37c7
+45 -22
View File
@@ -20,12 +20,6 @@ function initEventListeners() {
const filmForm = document.getElementById('film-form'); const filmForm = document.getElementById('film-form');
if (filmForm) filmForm.addEventListener('submit', saveFilmForm); if (filmForm) filmForm.addEventListener('submit', saveFilmForm);
const csvInput = document.getElementById('csv-file');
if (csvInput) csvInput.addEventListener('change', (e) => handleCsvUpload(e.target));
const savePwdBtn = document.getElementById('save-password-btn');
if (savePwdBtn) savePwdBtn.addEventListener('click', saveNewPassword);
// Fermeture des modales en cliquant sur le fond ou la croix // Fermeture des modales en cliquant sur le fond ou la croix
document.addEventListener('click', (e) => { document.addEventListener('click', (e) => {
if (e.target.classList.contains('modal-close') || e.target.closest('.modal-close')) { if (e.target.classList.contains('modal-close') || e.target.closest('.modal-close')) {
@@ -207,7 +201,10 @@ function openEditModal(id) {
function closeAdminModal() { document.getElementById('admin-modal').classList.remove('open'); } function closeAdminModal() { document.getElementById('admin-modal').classList.remove('open'); }
function openConfigModal() { document.getElementById('config-modal').classList.add('open'); } function openConfigModal() { document.getElementById('config-modal').classList.add('open'); }
function closeConfigModal() { document.getElementById('config-modal').classList.remove('open'); } function closeConfigModal() { document.getElementById('config-modal').classList.remove('open'); }
function openPasswordModal() { document.getElementById('password-modal').classList.add('open'); } function openPasswordModal() {
document.getElementById('pwd-error').style.display = 'none';
document.getElementById('password-modal').classList.add('open');
}
function closePasswordModal() { document.getElementById('password-modal').classList.remove('open'); } function closePasswordModal() { document.getElementById('password-modal').classList.remove('open'); }
function logout() { function logout() {
@@ -225,11 +222,9 @@ async function saveFilmForm(e) {
year: document.getElementById('f-year').value, year: document.getElementById('f-year').value,
director: document.getElementById('f-director').value, director: document.getElementById('f-director').value,
poster: document.getElementById('f-poster').value, poster: document.getElementById('f-poster').value,
// Champs "critiques"
rating: document.getElementById('f-rating') ? document.getElementById('f-rating').value : '', rating: document.getElementById('f-rating') ? document.getElementById('f-rating').value : '',
review: document.getElementById('f-review') ? document.getElementById('f-review').value : '', review: document.getElementById('f-review') ? document.getElementById('f-review').value : '',
streaming: document.getElementById('f-streaming') ? document.getElementById('f-streaming').value : '', streaming: document.getElementById('f-streaming') ? document.getElementById('f-streaming').value : '',
// Champs "vidéothèque"
format: document.getElementById('f-format') ? document.getElementById('f-format').value : '', format: document.getElementById('f-format') ? document.getElementById('f-format').value : '',
length: document.getElementById('f-length') ? document.getElementById('f-length').value : '', length: document.getElementById('f-length') ? document.getElementById('f-length').value : '',
publisher: document.getElementById('f-publisher') ? document.getElementById('f-publisher').value : '', publisher: document.getElementById('f-publisher') ? document.getElementById('f-publisher').value : '',
@@ -255,7 +250,7 @@ async function saveFilmForm(e) {
} }
} }
// ── GESTION DES FICHIERS CSV ── // ── IMPORT CSV ──
async function handleCsvUpload(input) { async function handleCsvUpload(input) {
if (!input.files || input.files.length === 0) return; if (!input.files || input.files.length === 0) return;
@@ -270,34 +265,62 @@ async function handleCsvUpload(input) {
headers: { 'Authorization': localStorage.getItem('token') }, headers: { 'Authorization': localStorage.getItem('token') },
body: formData body: formData
}); });
input.value = ''; // Réinitialiser le champ input.value = '';
closeConfigModal();
loadDashboardData(); loadDashboardData();
} catch (err) { } catch (err) {
console.error('Erreur lors de l\'importation du fichier CSV :', err); console.error('Erreur lors de l\'import CSV :', err);
}
}
// ── SAUVEGARDE DE LA CLÉ TMDB ──
function saveTmdbKey() {
const input = document.getElementById('tmdb-key-input');
if (input && input.value) {
localStorage.setItem('tmdb_key', input.value);
alert('Clé TMDB sauvegardée localement avec succès.');
closeConfigModal();
} }
} }
// ── MISE A JOUR DU MOT DE PASSE ── // ── MISE A JOUR DU MOT DE PASSE ──
async function saveNewPassword() { async function saveNewPassword() {
const pwdInput = document.getElementById('new-password'); // Assurez-vous que l'ID correspond à votre HTML const pwdInput = document.getElementById('new-password-input');
if (!pwdInput) return; const pwdConfirm = document.getElementById('new-password-confirm');
const errorMsg = document.getElementById('pwd-error');
const newPassword = pwdInput.value; if (!pwdInput || !pwdConfirm) return;
if (!newPassword) return;
if (pwdInput.value !== pwdConfirm.value) {
errorMsg.textContent = "Les mots de passe ne correspondent pas.";
errorMsg.style.display = "block";
return;
}
if (pwdInput.value.length < 4) {
errorMsg.textContent = "Le mot de passe doit contenir au moins 4 caractères.";
errorMsg.style.display = "block";
return;
}
try { try {
await fetch(`${API_URL}?action=update_password`, { const response = await fetch(`${API_URL}?action=update_password`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Authorization': localStorage.getItem('token'), 'Authorization': localStorage.getItem('token'),
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify({ new_password: newPassword }) body: JSON.stringify({ new_password: pwdInput.value })
}); });
pwdInput.value = ''; // Vider le champ
closePasswordModal(); const data = await response.json();
alert('Mot de passe mis à jour avec succès.'); if(data.success) {
pwdInput.value = '';
pwdConfirm.value = '';
errorMsg.style.display = "none";
closePasswordModal();
alert('Mot de passe mis à jour avec succès.');
loadDashboardData(); // Recharge pour faire disparaître le bandeau orange si existant
}
} catch (err) { } catch (err) {
console.error('Erreur lors de la mise à jour du mot de passe :', err); console.error('Erreur lors de la mise à jour du mot de passe :', err);
} }