33 lines
971 B
JavaScript
33 lines
971 B
JavaScript
// Minimal vanilla-JS frontend to poll only positions and render the positions table
|
|
const qs = (s) => document.querySelector(s);
|
|
const ptbody = qs('#positions-table tbody');
|
|
|
|
async function fetchJson(path) {
|
|
const res = await fetch(path);
|
|
if (!res.ok) throw new Error('Network response not ok');
|
|
return res.json();
|
|
}
|
|
|
|
function renderPositions(list) {
|
|
ptbody.innerHTML = '';
|
|
for (const p of list) {
|
|
const row = document.createElement('tr');
|
|
const avg = p.averageCost ?? p.average_cost ?? '';
|
|
row.innerHTML = `<td>${p.symbol}</td><td>${p.position}</td><td>${avg}</td><td>${p.account ?? ''}</td>`;
|
|
ptbody.appendChild(row);
|
|
}
|
|
}
|
|
|
|
async function pollPositions() {
|
|
try {
|
|
const positions = await fetchJson('/api/positions');
|
|
renderPositions(positions);
|
|
} catch (e) {
|
|
console.warn('Positions polling error', e);
|
|
}
|
|
}
|
|
|
|
// start polling every 2 seconds
|
|
pollPositions();
|
|
setInterval(pollPositions, 2000);
|