-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
152 lines (138 loc) · 6.69 KB
/
Copy pathscript.js
File metadata and controls
152 lines (138 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
document.addEventListener('DOMContentLoaded', () => {
// --- ELEMENT SELECTORS ---
const recommendationsGrid = document.querySelector('.recommendations-grid');
const searchBtn = document.getElementById('search-btn');
const tickerInput = document.getElementById('ticker-input');
const resultsContainer = document.getElementById('results-container');
const loader = document.getElementById('loader');
const errorContainer = document.getElementById('error-container');
const watchlistList = document.getElementById('watchlist-list');
const addToWatchlistBtn = document.getElementById('add-to-watchlist-btn');
const timeframeControls = document.getElementById('timeframe-controls');
let historicalChart = null;
let currentTicker = null;
let watchlist = JSON.parse(localStorage.getItem('credtech_watchlist')) || [];
let autoRefreshInterval = null;
const renderWatchlist = () => {
watchlistList.innerHTML = '';
if (watchlist.length === 0) {
watchlistList.innerHTML = '<p style="color: #888;">Your watchlist is empty.</p>';
return;
}
watchlist.forEach(ticker => {
const li = document.createElement('li');
li.textContent = ticker;
li.addEventListener('click', () => { tickerInput.value = ticker; fetchScore(); });
const removeBtn = document.createElement('button');
removeBtn.textContent = '✖';
removeBtn.className = 'remove-watchlist';
removeBtn.addEventListener('click', (e) => {
e.stopPropagation();
watchlist = watchlist.filter(t => t !== ticker);
localStorage.setItem('credtech_watchlist', JSON.stringify(watchlist));
renderWatchlist();
});
li.appendChild(removeBtn);
watchlistList.appendChild(li);
});
};
addToWatchlistBtn.addEventListener('click', () => {
if (currentTicker && !watchlist.includes(currentTicker)) {
watchlist.push(currentTicker);
localStorage.setItem('credtech_watchlist', JSON.stringify(watchlist));
renderWatchlist();
}
});
const renderChart = async (ticker, period = '1m') => {
try {
const response = await fetch(`/history/${ticker}/${period}`);
const data = await response.json();
const ctx = document.getElementById('historical-chart').getContext('2d');
if (historicalChart) { historicalChart.destroy(); }
historicalChart = new Chart(ctx, {
type: 'line',
data: {
labels: Object.keys(data),
datasets: [{
label: `${ticker} ${period.toUpperCase()} Price`,
data: Object.values(data),
borderColor: '#007bff', tension: 0.1, fill: false
}]
},
options: { scales: { x: { ticks: { color: '#888' } }, y: { ticks: { color: '#888' } } }, plugins: { legend: { labels: { color: '#e0e0e0' } } } }
});
} catch (error) { console.error('Error fetching historical data:', error); }
};
const renderPeerScores = (peers) => { /* ... unchanged ... */ };
const renderExplanation = (explanation) => { /* ... unchanged ... */ };
const setupAutoRefresh = (ticker) => {
if (autoRefreshInterval) clearInterval(autoRefreshInterval);
autoRefreshInterval = setInterval(() => {
console.log(`Auto-refreshing score for ${ticker}...`);
fetchScore(true); // Pass flag to indicate auto-refresh
}, 60000);
};
const fetchScore = async (isAutoRefresh = false) => {
const ticker = isAutoRefresh ? currentTicker : tickerInput.value.toUpperCase().trim();
if (!ticker) {
errorContainer.innerText = 'Please enter a ticker symbol.';
errorContainer.classList.remove('hidden'); return;
}
currentTicker = ticker;
if (!isAutoRefresh) {
loader.classList.remove('hidden');
resultsContainer.classList.add('hidden');
}
errorContainer.classList.add('hidden');
try {
const response = await fetch(`/score/${ticker}`);
const data = await response.json();
if (!response.ok) { throw new Error(data.error || 'An unknown error occurred.'); }
document.getElementById('company-name').innerText = data.company_name;
const scoreElement = document.getElementById('credit-score');
scoreElement.innerText = data.credit_score;
scoreElement.className = '';
if (data.credit_score > 65) scoreElement.classList.add('score-good');
else if (data.credit_score < 40) scoreElement.classList.add('score-bad');
else scoreElement.classList.add('score-neutral');
// --- UPDATE: MAKE HEADLINES CLICKABLE ---
const headlinesList = document.getElementById('headlines-list');
headlinesList.innerHTML = '';
data.recent_headlines.forEach(article => {
const li = document.createElement('li');
const a = document.createElement('a');
a.href = article.url;
a.textContent = article.title;
a.target = '_blank';
li.appendChild(a);
headlinesList.appendChild(li);
});
renderExplanation(data.explanation);
renderPeerScores(data.peer_scores);
if (!isAutoRefresh) { await renderChart(ticker, '1m'); }
resultsContainer.classList.remove('hidden');
setupAutoRefresh(ticker);
} catch (error) {
errorContainer.innerText = `Error: ${error.message}`;
errorContainer.classList.remove('hidden');
} finally {
if (!isAutoRefresh) loader.classList.add('hidden');
}
};
searchBtn.addEventListener('click', () => fetchScore());
tickerInput.addEventListener('keyup', (event) => { if (event.key === 'Enter') fetchScore(); });
recommendationsGrid.addEventListener('click', (event) => {
if (event.target.classList.contains('recommendation-btn')) {
tickerInput.value = event.target.dataset.ticker;
fetchScore();
}
});
timeframeControls.addEventListener('click', (e) => {
if (e.target.classList.contains('timeframe-btn')) {
document.querySelectorAll('.timeframe-btn').forEach(btn => btn.classList.remove('active'));
e.target.classList.add('active');
if (currentTicker) renderChart(currentTicker, e.target.dataset.period);
}
});
renderWatchlist();
});