repos
/ finance-rust master

finance-rust

mirror archived upstream

Single-binary self-hosted market watcher for stocks, ETFs, indexes, and futures: live charts, key stats, fundamentals, SEC filings, and SSE streaming.

axumdockerfinancerustself-hostedsqlitestocksvite

1.6 KB · 54 lines · JavaScript Raw History
 1// Search page enhancement: the "Add <TICKER>" button.
 2//
 3// The page is server-rendered; this only wires the add-symbol affordance.
 4// Clicking it POSTs the ticker to /api/symbols, which validates it against
 5// Yahoo and registers it. On success the browser lands on the new symbol's
 6// page; on failure the server's message is shown inline.
 7
 8export function initSearch() {
 9  const btn = document.querySelector("[data-add-ticker]");
10  if (!btn) return;
11
12  const errEl = document.querySelector('[data-role="add-error"]');
13  const ticker = btn.dataset.addTicker;
14  const label = btn.textContent;
15
16  function showError(msg) {
17    if (!errEl) return;
18    errEl.textContent = msg;
19    errEl.hidden = false;
20  }
21
22  btn.addEventListener("click", async () => {
23    btn.disabled = true;
24    btn.textContent = "Adding…";
25    if (errEl) errEl.hidden = true;
26
27    try {
28      const res = await fetch("/api/symbols", {
29        method: "POST",
30        headers: { "Content-Type": "application/json" },
31        body: JSON.stringify({ ticker }),
32      });
33      let data = {};
34      try {
35        data = await res.json();
36      } catch {
37        /* a non-JSON body falls through to the generic message below */
38      }
39
40      if (res.ok && data.ok && data.ticker) {
41        // Land on the freshly added symbol's page.
42        window.location.href = "/s/" + encodeURIComponent(data.ticker);
43        return;
44      }
45      showError(data.error || "Could not add that symbol. Try again shortly.");
46    } catch {
47      showError("Could not reach the server. Check your connection and try again.");
48    }
49
50    btn.disabled = false;
51    btn.textContent = label;
52  });
53}