From 58576a86fc4fe87bce996f9967e49d0948cf202c Mon Sep 17 00:00:00 2001 From: Giorgio Gilestro Date: Wed, 27 May 2026 14:51:45 +0200 Subject: [PATCH] portfolio-edit: add button writes position to localStorage Co-Authored-By: Claude Opus 4.7 --- app/static/js/portfolio_edit.js | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/app/static/js/portfolio_edit.js b/app/static/js/portfolio_edit.js index 627f9d2..b96bd34 100644 --- a/app/static/js/portfolio_edit.js +++ b/app/static/js/portfolio_edit.js @@ -124,4 +124,56 @@ tickerInput.addEventListener('blur', validateTicker); document.getElementById('pf-add-qty').addEventListener('input', updateSubmitState); document.getElementById('pf-add-cost').addEventListener('input', updateSubmitState); + + // ---- Add button → localStorage merge ------------------------------- + + function resetForm() { + tickerInput.value = ''; + document.getElementById('pf-add-qty').value = ''; + document.getElementById('pf-add-cost').value = ''; + document.getElementById('pf-add-date').value = ''; + validated = null; + setStatus(tickerStatus, '', ''); + costCurrencyEl.textContent = ''; + clearDuplicateWarning(); + updateSubmitState(); + tickerInput.focus(); + } + + function addPosition() { + if (submitBtn.disabled) return; + const qty = parseFloat(document.getElementById('pf-add-qty').value); + const cost = parseFloat(document.getElementById('pf-add-cost').value); + const sym = validated.symbol; + + const pie = window.CassandraPortfolio.loadPie() || { + pie_name: null, + base_currency: 'GBP', + positions: [], + totals: {invested: 0, value: 0, result: 0}, + warnings: [], + }; + pie.positions = pie.positions || []; + pie.positions.push({ + yahoo_ticker: sym, + t212_slice: sym, // shared shape with CSV path + name: validated.name || sym, + qty: qty, + avg_cost: cost, + currency: validated.currency || 'USD', + }); + window.CassandraPortfolio.savePie(pie); + window.CassandraPortfolio.mountAndRender(); + resetForm(); + } + + submitBtn.addEventListener('click', addPosition); + + // Submit on Enter from any input within the form. + form.addEventListener('keydown', function (e) { + if (e.key === 'Enter' && !submitBtn.disabled) { + e.preventDefault(); + addPosition(); + } + }); })();