]> git.digitality.be Git - pdw25-26/commitdiff
mise à jour du script
author[oceane] <[e21497@eps-marche.be]>
Sat, 28 Feb 2026 16:11:02 +0000 (17:11 +0100)
committer[oceane] <[e21497@eps-marche.be]>
Sat, 28 Feb 2026 16:11:02 +0000 (17:11 +0100)
Wallette/web/script.js

index 8e7e31e8aa6f05ab2793de10f909d5aa9dfbb773..b8202869a7960ca6dfaa935d8ca659cfe461fa0d 100644 (file)
@@ -6,14 +6,72 @@ import {
   isConnected
 } from './socketService.js';
 
+const SERVER_URL = 'http://localhost:3000';
+
 // Exemple : récupérer un userId (ici exemple statique, adapte-toi)
 const userId = 'user-123';
 
 // Connecter automatiquement à l'ouverture
 connectToAlerts(userId);
 
+
+//function api 
+
+// Charger historique alertes
+async function loadAlertHistory() {
+  try {
+    const res = await fetch(`${SERVER_URL}/api/alerts/history?userId=${userId}`);
+    const data = await res.json();
+
+    data.forEach(alert => {
+      // on réutilise EXACTEMENT la même logique que socket
+      handleAlert(alert);
+    });
+
+  } catch (err) {
+    console.error("Erreur historique alertes :", err);
+  }
+}
+
+// Charger prix actuel
+async function loadCurrentPrice(pair = "BTC/EUR") {
+  try {
+    const res = await fetch(`${SERVER_URL}/api/prices/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
+      });
+    }
+
+  } catch (err) {
+    console.error("Erreur prix actuel :", err);
+  }
+}
+
+// Charger wallet utilisateur
+async function loadWallet() {
+  try {
+    const res = await fetch(`${SERVER_URL}/api/wallet/${userId}`);
+    const data = await res.json();
+
+    const balanceEl = document.getElementById("balance");
+    if (balanceEl && data.balance !== undefined) {
+      balanceEl.textContent = data.balance + " BTC";
+    }
+
+  } catch (err) {
+    console.error("Erreur wallet :", err);
+  }
+}
+
 // Quand une alerte arrive, l'ajouter dans la liste #alertList
-onAlert(function(alert) {
+function handleIncomingAlert(alert) {
+
   console.log('Nouvelle alerte reçue dans main.js :', alert);
 
   const list = document.getElementById('alertList');
@@ -131,10 +189,19 @@ onAlert(function(alert) {
   }
 
 
-  // Préfixer pour voir les nouvelles alertes en haut
+   // Préfixer pour voir les nouvelles alertes en haut
   list.prepend(li);
+}
+
+// Socket temps réel
+onAlert(function(alert) {
+  handleIncomingAlert(alert);
 });
 
+loadAlertHistory();
+loadCurrentPrice();
+loadWallet();
+
 // Déconnexion propre à la fermeture de la page
 window.addEventListener('beforeunload', () => {
   if (isConnected()) {