]> git.digitality.be Git - pdw25-26/commitdiff
mise à jour branchement strat oceane-web
author[oceane] <[e21497@eps-marche.be]>
Sun, 1 Mar 2026 18:45:46 +0000 (19:45 +0100)
committer[oceane] <[e21497@eps-marche.be]>
Sun, 1 Mar 2026 18:45:46 +0000 (19:45 +0100)
Wallette/web/script.js

index ab63ed69a2290d255aa1116b66f847585879d470..0b873cb2ebf742b9753c1b3b55d57640e24a9454 100644 (file)
@@ -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();