Mise à jour

This commit is contained in:
2026-06-03 14:47:35 +02:00
parent 085cf33114
commit ad0d86e734
16 changed files with 669 additions and 889 deletions

View File

@@ -1,13 +1,4 @@
/**
* ============================================
* DASHBOARD.JS - Gestion du dashboard
* Smart Parking - BTS CIEL IR
* CORRIGÉ : cancelReservation utilisait spotNumber
* au lieu de spotId pour libérer la place
* ============================================
*/
const API_URL = 'http://localhost:3000/api';
const API_URL = '/api';
let dashboardState = {
user: null,
@@ -93,10 +84,10 @@ function loadUserData() {
document.getElementById('userName').textContent = user.name;
document.getElementById('userRole').textContent = user.role === 'admin' ? 'Administrateur' : 'Client';
document.getElementById('profileName').textContent = user.name;
document.getElementById('profileNameInput').value = user.name;
document.getElementById('profileEmailInput').value = user.email;
document.getElementById('profilePhoneInput').value = user.phone || '';
document.getElementById('profileName').textContent = user.name;
document.getElementById('profileNameInput').value = user.name;
document.getElementById('profileEmailInput').value = user.email;
document.getElementById('profilePhoneInput').value = user.phone || '';
const roleBadge = document.getElementById('profileRole');
if (roleBadge) {
@@ -137,7 +128,7 @@ function loadMyReservations() {
<h4>Place ${res.spotNumber}</h4>
<div class="reservation-details">
<span>📅 ${res.date}</span>
<span>🕐 ${res.startTime}</span>
<span>🕐 ${res.startTime} - ${res.endTime}</span>
<span>⏱️ ${formatDurationLabel(res.duration)}</span>
<span>🚗 ${res.vehicle || 'N/A'}</span>
</div>
@@ -155,22 +146,16 @@ function loadMyReservations() {
`).join('');
}
/**
* CORRIGÉ : utilisait reservation.spotNumber (le numéro visible)
* au lieu de reservation.spotId (l'id interne), ce qui empêchait
* la place d'être libérée sur la carte.
*/
function cancelReservation(reservationId) {
if (!confirm('Êtes-vous sûr de vouloir annuler cette réservation ?')) return;
let reservations = JSON.parse(localStorage.getItem('smart_parking_reservations') || '[]');
let reservations = JSON.parse(localStorage.getItem('smart_parking_reservations') || '[]');
const reservation = reservations.find(r => r.id === reservationId);
if (reservation) {
reservation.status = 'cancelled';
localStorage.setItem('smart_parking_reservations', JSON.stringify(reservations));
// CORRIGÉ : spotId au lieu de spotNumber
if (window.ParkingMap) {
window.ParkingMap.setSpotStatus(reservation.spotId, 'free');
}
@@ -181,26 +166,82 @@ function cancelReservation(reservationId) {
}
}
// Mise à jour du profil
document.getElementById('profileForm')?.addEventListener('submit', (e) => {
document.getElementById('profileForm')?.addEventListener('submit', async (e) => {
e.preventDefault();
const phone = document.getElementById('profilePhoneInput').value;
const newPassword = document.getElementById('profileNewPassword').value;
const confirmPass = document.getElementById('profileNewPassword').value;
let user = dashboardState.user;
user.phone = phone;
if (newPassword) user.password = newPassword;
localStorage.setItem('smart_parking_user', JSON.stringify(user));
let users = JSON.parse(localStorage.getItem('smart_parking_users') || '[]');
const idx = users.findIndex(u => u.id === user.id);
if (idx !== -1) {
users[idx] = user;
localStorage.setItem('smart_parking_users', JSON.stringify(users));
if (newPassword && newPassword.length < 8) {
showToast('Le mot de passe doit faire au moins 8 caractères', 'error');
return;
}
showToast('Profil mis à jour', 'success');
const token = localStorage.getItem('smart_parking_token');
if (token) {
try {
const updateData = { phone };
if (newPassword) {
updateData.password = newPassword;
}
const response = await fetch(`${API_URL}/users/profile`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify(updateData)
});
const data = await response.json();
if (data.success) {
let user = dashboardState.user;
user.phone = phone;
dashboardState.user = user;
localStorage.setItem('smart_parking_user', JSON.stringify(user));
document.getElementById('profileNewPassword').value = '';
showToast('Profil mis à jour avec succès !', 'success');
} else {
showToast(data.message || 'Erreur lors de la mise à jour', 'error');
}
} catch (_err) {
let user = dashboardState.user;
user.phone = phone;
if (newPassword) user.password = newPassword;
dashboardState.user = user;
localStorage.setItem('smart_parking_user', JSON.stringify(user));
let users = JSON.parse(localStorage.getItem('smart_parking_users') || '[]');
const idx = users.findIndex(u => u.id === user.id);
if (idx !== -1) {
users[idx] = user;
localStorage.setItem('smart_parking_users', JSON.stringify(users));
}
document.getElementById('profileNewPassword').value = '';
showToast('Profil mis à jour', 'success');
}
} else {
let user = dashboardState.user;
user.phone = phone;
if (newPassword) user.password = newPassword;
dashboardState.user = user;
localStorage.setItem('smart_parking_user', JSON.stringify(user));
showToast('Profil mis à jour', 'success');
}
});
function getStatusLabel(status) {
@@ -208,7 +249,7 @@ function getStatusLabel(status) {
}
function formatDurationLabel(minutes) {
if (minutes >= 480) return 'Journée';
if (minutes >= 480) return 'Journée (8h)';
if (minutes >= 60) return Math.floor(minutes / 60) + 'h' + (minutes % 60 ? (minutes % 60) + 'min' : '');
return minutes + ' min';
}