From 1e090cf9cacaeab87828c22d8cee7e178caa4d95 Mon Sep 17 00:00:00 2001 From: "fefe.clochette" Date: Sat, 20 Jun 2026 13:05:50 +0200 Subject: [PATCH] opti save --- modules/holidays/holidays.js | 29 ++++++++++++++----- .../holidays/includes/api/save_checkpoint.php | 13 +++++---- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/modules/holidays/holidays.js b/modules/holidays/holidays.js index b692c72..6c0eb5c 100644 --- a/modules/holidays/holidays.js +++ b/modules/holidays/holidays.js @@ -948,7 +948,7 @@ function toggleStepDates(type) { } } -// Ajout magique d'une dépense d'essence SÉCURISÉE +// Ajout magique d'une dépense d'essence SÉCURISÉE et INSTANTANÉE function addQuickTransitExpense( holidayId, sortOrder, @@ -963,11 +963,19 @@ function addQuickTransitExpense( ) return; - if (btnElement) { - btnElement.disabled = true; - btnElement.innerText = "⏳..."; - btnElement.style.cursor = "not-allowed"; - btnElement.style.opacity = "0.7"; + // 1. UI OPTIMISTE : On change visuellement le bouton tout de suite sans attendre le serveur + const parentContainer = btnElement.parentElement; + if (parentContainer) { + parentContainer.innerHTML = `✓ Ajouté`; + } + + // 2. On met à jour discrètement le compteur global en haut de page (+ montant) + const totalTransitEl = document.querySelector(".hol-summary-value strong"); + if (totalTransitEl) { + const currentTotal = + parseFloat(totalTransitEl.innerText.replace(" €", "").replace(" ", "")) || + 0; + totalTransitEl.innerText = Math.round(currentTotal + amount) + " €"; } const fd = new FormData(); @@ -979,10 +987,17 @@ function addQuickTransitExpense( fd.append("amount", amount); fd.append("context", "transit"); + // 3. Envoi en arrière-plan sans bloquer l'utilisateur fetch("/modules/holidays/includes/api/save_checkpoint.php", { method: "POST", body: fd, - }).then(() => window.location.reload()); + }) + .then((res) => res.json()) + .then((data) => { + if (!data.success) { + alert("Erreur lors de la sauvegarde en arrière-plan : " + data.error); + } + }); } // Permet de modifier le prix du carburant à la volée diff --git a/modules/holidays/includes/api/save_checkpoint.php b/modules/holidays/includes/api/save_checkpoint.php index 1ab8209..e459caa 100644 --- a/modules/holidays/includes/api/save_checkpoint.php +++ b/modules/holidays/includes/api/save_checkpoint.php @@ -4,24 +4,25 @@ require dirname(__DIR__, 4) . '/includes/auth.php'; require dirname(__DIR__, 4) . '/includes/db.php'; require_login(); -// --------------------------------=========================================== -// INTERCEPTIONS AJAX (Execution ultra rapide sans rechargement de page) -// --------------------------------=========================================== +// --------------------------------------------------------------------------- +// INTERCEPTIONS AJAX (Exécution ultra rapide sans rechargement lourd) +// --------------------------------------------------------------------------- if (isset($_POST['action']) && in_array($_POST['action'], ['update_item_datetime', 'update_item_duration', 'add_single_item'])) { header('Content-Type: application/json'); + session_write_close(); // 🚀 LIBÈRE LA SESSION : Permet d'autres requêtes simultanées sans bloquer le navigateur try { - // 🔥 AJOUT SÉCURISÉ D'UNE DÉPENSE UNIQUE (Essence OSRM) if ($_POST['action'] === 'add_single_item') { $holiday_id = (int)$_POST['holiday_id']; $sort_order = (int)$_POST['sort_order']; - // On récupère proprement les métadonnées de l'étape pour lier le frais $stmt = $pdo->prepare("SELECT location_name, lat, lng, step_start_date, step_end_date, step_type FROM pf_holidays_items WHERE holiday_id = ? AND sort_order = ? LIMIT 1"); $stmt->execute([$holiday_id, $sort_order]); $stepInfo = $stmt->fetch(); if ($stepInfo) { + // Utilisation de transactions isolées pour l'AJAX + $pdo->beginTransaction(); $ins = $pdo->prepare("INSERT INTO pf_holidays_items (holiday_id, category, name, amount, is_paid, location_name, lat, lng, sort_order, step_start_date, step_end_date, step_type, expense_context, duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)"); $ins->execute([ $holiday_id, $_POST['category'], $_POST['name'], (float)$_POST['amount'], 0, @@ -29,6 +30,7 @@ if (isset($_POST['action']) && in_array($_POST['action'], ['update_item_datetime $sort_order, $stepInfo['step_start_date'], $stepInfo['step_end_date'], $stepInfo['step_type'], $_POST['context'] ]); + $pdo->commit(); echo json_encode(['success' => true]); exit; } @@ -57,6 +59,7 @@ if (isset($_POST['action']) && in_array($_POST['action'], ['update_item_datetime exit; } } catch (Exception $e) { + if ($pdo->inTransaction()) { $pdo->rollBack(); } echo json_encode(['success' => false, 'error' => $e->getMessage()]); exit; }