]> git.digitality.be Git - pdw25-26/commitdiff
mise à jour branché api
author[oceane] <[e21497@eps-marche.be]>
Sat, 28 Feb 2026 20:26:57 +0000 (21:26 +0100)
committer[oceane] <[e21497@eps-marche.be]>
Sat, 28 Feb 2026 20:26:57 +0000 (21:26 +0100)
Wallette/web/script.js

index b8202869a7960ca6dfaa935d8ca659cfe461fa0d..ab63ed69a2290d255aa1116b66f847585879d470 100644 (file)
@@ -8,24 +8,37 @@ import {
 
 const SERVER_URL = 'http://localhost:3000';
 
-// Exemple : récupérer un userId (ici exemple statique, adapte-toi)
-const userId = 'user-123';
+//récupérer un userId (ici exemple statique, adapte-toi)
+let userId = 'user-123';
 
 // Connecter automatiquement à l'ouverture
 connectToAlerts(userId);
 
 
-//function api 
+//------------------function api ---------------------------------------------------------------
 
 // Charger historique alertes
-async function loadAlertHistory() {
+async function loadWalletEvents(walletId) {
   try {
-    const res = await fetch(`${SERVER_URL}/api/alerts/history?userId=${userId}`);
-    const data = await res.json();
+    const res = await fetch(`${SERVER_URL}/api/wallets/${walletId}/events`);
+    const events = await res.json();
+
+    const eventsList = document.getElementById("walletEvents");
+    eventsList.innerHTML = '';
+
+    if (!events.length) {
+      eventsList.innerHTML = `<li class="list-group-item bg-transparent text-white">Aucune transaction</li>`;
+      return;
+    }
+
+    events.forEach(ev => {
+      const li = document.createElement('li');
+      li.className = 'list-group-item bg-transparent text-white';
 
-    data.forEach(alert => {
-      // on réutilise EXACTEMENT la même logique que socket
-      handleAlert(alert);
+      const date = new Date(ev.timestamp).toLocaleString('fr-FR');
+
+      li.textContent = `${date} — ${ev.type} — ${ev.amount} BTC`;
+      eventsList.appendChild(li);
     });
 
   } catch (err) {
@@ -33,19 +46,21 @@ async function loadAlertHistory() {
   }
 }
 
-// Charger prix actuel
+// Charger prix actuel ------------------------------------------------------------------------
 async function loadCurrentPrice(pair = "BTC/EUR") {
   try {
-    const res = await fetch(`${SERVER_URL}/api/prices/current?pair=${pair}`);
+    const res = await fetch(`${SERVER_URL}/api/price/current?pair=${pair}`);
     const data = await res.json();
 
     const priceEl = document.getElementById("price");
+
     if (priceEl && data.price !== undefined) {
       const currency = pair.includes("USD") ? "USD" : "EUR";
-      priceEl.textContent = Number(data.price).toLocaleString('fr-FR', {
-        style: 'currency',
-        currency: currency
-      });
+      priceEl.textContent =
+        Number(data.price).toLocaleString('fr-FR', {
+          style: 'currency',
+          currency: currency
+        });
     }
 
   } catch (err) {
@@ -53,22 +68,67 @@ async function loadCurrentPrice(pair = "BTC/EUR") {
   }
 }
 
-// Charger wallet utilisateur
-async function loadWallet() {
+// Charger wallet utilisateur -----------------------------------------------------------------------------------------
+async function loadWallets() {
   try {
-    const res = await fetch(`${SERVER_URL}/api/wallet/${userId}`);
-    const data = await res.json();
+    const res = await fetch(`${SERVER_URL}/api/wallets?userId=${userId}`);
+    const wallets = await res.json();
+
+    const walletList = document.getElementById("walletList");
+    walletList.innerHTML = '';
 
-    const balanceEl = document.getElementById("balance");
-    if (balanceEl && data.balance !== undefined) {
-      balanceEl.textContent = data.balance + " BTC";
+    if (!wallets.length) {
+      walletList.innerHTML = `<li class="list-group-item bg-transparent text-white">Aucun wallet chargé</li>`;
+      return;
     }
 
+    wallets.forEach(wallet => {
+
+      const li = document.createElement('li');
+      li.className = 'list-group-item bg-transparent text-white';
+      li.textContent = `${wallet.name} — ${wallet.balance} BTC`;
+      walletList.appendChild(li);
+
+      // Charger les transactions pour chaque wallet
+      loadWalletEvents(wallet.id);
+    });
+
   } catch (err) {
     console.error("Erreur wallet :", err);
   }
 }
 
+// Signal actuel -------------------------------------------------------------------------------------
+async function loadCurrentSignal() {
+  try {
+    const res = await fetch(`${SERVER_URL}/api/signal/current`);
+    const signal = await res.json();
+
+    const box = document.getElementById('signalBox');
+    const actionEl = document.getElementById('signalAction');
+    const critEl = document.getElementById('signalCriticality');
+    const confEl = document.getElementById('signalConfidence');
+    const reasonEl = document.getElementById('signalReason');
+
+    if (!box) return;
+
+    const action = signal.action || 'HOLD';
+
+    box.className = 'signal-box ' + action.toLowerCase();
+    actionEl.textContent = action;
+    critEl.textContent = signal.alertLevel || 'INFO';
+    confEl.textContent =
+      typeof signal.confidence === 'number'
+        ? Math.round(signal.confidence * 100) + '%'
+        : '—';
+    reasonEl.textContent = signal.reason || '';
+
+  } catch (err) {
+    console.error("Erreur API signal/current :", err);
+  }
+}
+
+// socket -------------------------------------------------------------------------------------------------------
 // Quand une alerte arrive, l'ajouter dans la liste #alertList
 function handleIncomingAlert(alert) {
 
@@ -198,6 +258,28 @@ onAlert(function(alert) {
   handleIncomingAlert(alert);
 });
 
+//button user 
+
+document.getElementById("saveUserBtn")?.addEventListener("click", () => {
+  const input = document.getElementById("userIdInput");
+  if (input.value) {
+    userId = input.value;
+    alert("User ID sauvegardé : " + userId);
+  }
+});
+
+document.getElementById("connectBtn")?.addEventListener("click", () => {
+  connectToAlerts(userId);
+  document.getElementById("connStatus").className = "badge bg-success";
+  document.getElementById("connStatus").textContent = "Statut : on";
+});
+
+document.getElementById("disconnectBtn")?.addEventListener("click", () => {
+  disconnectFromAlerts();
+  document.getElementById("connStatus").className = "badge bg-secondary";
+  document.getElementById("connStatus").textContent = "Statut : off";
+});
+
 loadAlertHistory();
 loadCurrentPrice();
 loadWallet();