/******************************************* * watchlist.js *******************************************/ // DOM references for Watchlist const watchlistSymbolInput = document.getElementById("watchlistSymbolInput"); const addWatchlistBtn = document.getElementById("addWatchlistBtn"); const watchlistTableBody = document.getElementById("watchlistTableBody"); // Add to watchlist addWatchlistBtn.addEventListener("click", async () => { if (!isLoggedIn()) { alert("Please log in first."); return; } const user = auth.currentUser; if (!user) return; const username = user.displayName || user.email; const symbol = watchlistSymbolInput.value.trim().toUpperCase(); if (!symbol) { alert("Please enter a symbol."); return; } // Validate with an overview fetch const overviewData = await fetchOverview(symbol); if (!overviewData) { alert("Invalid ticker or data not found. Symbol not added."); return; } const watchKey = `pt_watchlist_${username}`; let watchlist = JSON.parse(localStorage.getItem(watchKey) || "[]"); if (watchlist.includes(symbol)) { alert("Symbol already in watchlist."); return; } watchlist.push(symbol); localStorage.setItem(watchKey, JSON.stringify(watchlist)); alert(`${symbol} added to watchlist!`); watchlistSymbolInput.value = ""; loadWatchlist(); }); // Remove from watchlist (listen to the table’s “delete” button) watchlistTableBody.addEventListener("click", (e) => { if (e.target.classList.contains("delete-watchlist-btn")) { const symbol = e.target.getAttribute("data-symbol"); removeFromWatchlist(symbol); } }); // Actually remove the symbol window.removeFromWatchlist = function(symbol) { const user = auth.currentUser; if (!user) return; const username = user.displayName || user.email; const watchKey = `pt_watchlist_${username}`; let watchlist = JSON.parse(localStorage.getItem(watchKey) || "[]"); watchlist = watchlist.filter(s => s !== symbol); localStorage.setItem(watchKey, JSON.stringify(watchlist)); alert(`${symbol} removed from watchlist.`); loadWatchlist(); }; // Load the watchlist table window.loadWatchlist = async function() { const user = auth.currentUser; if (!user) return; const username = user.displayName || user.email; watchlistTableBody.innerHTML = ""; const watchKey = `pt_watchlist_${username}`; let watchlist = JSON.parse(localStorage.getItem(watchKey) || "[]"); for (let sym of watchlist) { // Parallel fetch overview + price const [overviewData, price] = await Promise.all([ fetchOverview(sym), fetchStockPrice(sym) ]); let name = "N/A"; let marketCap = "$0"; let peRatio = "N/A"; if (overviewData && overviewData.Name) { name = overviewData.Name; } if (overviewData && overviewData.MarketCapitalization) { marketCap = formatMarketCap(overviewData.MarketCapitalization); } if (overviewData && overviewData.PERatio) { peRatio = overviewData.PERatio; } let displayPrice = "N/A"; let displaySMA = "N/A"; // In your original code, SMA is just displayed as the same price if (price && !isNaN(price)) { displayPrice = `$${price.toFixed(2)}`; displaySMA = `$${price.toFixed(2)}`; } const row = document.createElement("tr"); row.innerHTML = `