From: [oceane] <[e21497@eps-marche.be]> Date: Sun, 1 Mar 2026 18:45:46 +0000 (+0100) Subject: mise à jour branchement strat X-Git-Url: https://git.digitality.be/?a=commitdiff_plain;h=refs%2Fheads%2Foceane-web;p=pdw25-26 mise à jour branchement strat --- diff --git a/Wallette/web/script.js b/Wallette/web/script.js index ab63ed6..0b873cb 100644 --- a/Wallette/web/script.js +++ b/Wallette/web/script.js @@ -128,6 +128,73 @@ async function loadCurrentSignal() { } } + +// CHARGER LES STRATÉGIES DE SACHA +async function loadStrategies() { + try { + const res = await fetch(`${SERVER_URL}/api/strategy?userId=${userId}`); + const data = await res.json(); + + const select = document.getElementById('strategySelect'); + if (!select) return; + + select.innerHTML = ''; + + // Si le backend renvoie un tableau direct + const strategies = Array.isArray(data) ? data : (data.strategies || []); + + if (!strategies.length) { + const opt = document.createElement('option'); + opt.value = ''; + opt.textContent = 'Aucune stratégie disponible'; + select.appendChild(opt); + return; + } + + strategies.forEach(strategy => { + const opt = document.createElement('option'); + opt.value = strategy.id || strategy.name; + opt.textContent = strategy.name || strategy.id; + select.appendChild(opt); + }); + + } catch (err) { + console.error("Erreur chargement stratégies :", err); + } +} + + +// Appliquer la stratégie sélectionnée +async function applySelectedStrategy() { + try { + const select = document.getElementById('strategySelect'); + if (!select) return; + + const strategyId = select.value; + if (!strategyId) { + alert("Choisis une stratégie."); + return; + } + + const res = await fetch(`${SERVER_URL}/api/strategy/apply`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ userId, strategyId }) + }); + + if (!res.ok) { + throw new Error("Erreur serveur"); + } + + alert("Stratégie appliquée ✅"); + + } catch (err) { + console.error("Erreur application stratégie :", err); + alert("Impossible d'appliquer la stratégie."); + } +} + + // socket ------------------------------------------------------------------------------------------------------- // Quand une alerte arrive, l'ajouter dans la liste #alertList function handleIncomingAlert(alert) { @@ -280,6 +347,21 @@ document.getElementById("disconnectBtn")?.addEventListener("click", () => { document.getElementById("connStatus").textContent = "Statut : off"; }); +//button stratégie + +(function bindStrategyButton() { + const select = document.getElementById('strategySelect'); + if (!select) return; + + const card = select.closest('.card'); + const button = card ? card.querySelector('button.btn-primary') : null; + + if (button) { + button.addEventListener('click', applySelectedStrategy); + } +})(); + +loadStrategies(); loadAlertHistory(); loadCurrentPrice(); loadWallet();