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');
}
- // 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()) {