From ad5c7236ee3b1fa271ada9ae57ef88aec5ca17ee Mon Sep 17 00:00:00 2001 From: daveminer Date: Mon, 20 Nov 2023 16:02:14 -0500 Subject: [PATCH] presentation cleanup and clear search input work --- lib/basket/websocket/alpaca.ex | 4 ++ lib/basket_web/components/core_components.ex | 22 +++++---- lib/basket_web/components/dark_mode_toggle.ex | 19 ++++---- lib/basket_web/components/search_input.ex | 2 +- lib/basket_web/components/ticker_bar_table.ex | 5 ++- lib/basket_web/live/overview.ex | 34 +++++++++----- test/basket_web/live/overview_test.exs | 45 ++++++++++++------- 7 files changed, 85 insertions(+), 46 deletions(-) diff --git a/lib/basket/websocket/alpaca.ex b/lib/basket/websocket/alpaca.ex index b1bb8de..04323de 100644 --- a/lib/basket/websocket/alpaca.ex +++ b/lib/basket/websocket/alpaca.ex @@ -36,6 +36,8 @@ defmodule Basket.Websocket.Alpaca do @impl true def handle_disconnect(disconnect_map, state) do + IO.inspect("DISC: #{inspect(disconnect_map)}") + IO.inspect("STATE: #{inspect(state)}") Logger.info("Alpaca websocket disconnected.") super(disconnect_map, state) end @@ -61,6 +63,8 @@ defmodule Basket.Websocket.Alpaca do @impl true def handle_frame({_tpe, msg}, state) do + IO.inspect("FRAME: #{inspect(msg)}") + case Jason.decode(msg) do {:ok, decoded_message} -> Enum.each(decoded_message, &process_message/1) diff --git a/lib/basket_web/components/core_components.ex b/lib/basket_web/components/core_components.ex index e8190aa..a912150 100644 --- a/lib/basket_web/components/core_components.ex +++ b/lib/basket_web/components/core_components.ex @@ -721,16 +721,20 @@ defmodule BasketWeb.CoreComponents do defp diff_color(col, row) do key = Map.get(col, :key) - field = row[key] - - if is_number(field.value) do - case field.value - field.prev_value do - x when x > 0 -> "bg-emerald-300 text-emerald-900" - x when x < 0 -> "bg-rose-300 text-rose-900" - _ -> "" - end - else + if is_nil(key) do "" + else + field = row[key] + + if field != nil && is_number(field.value) && is_number(field.prev_value) do + case field.value - field.prev_value do + x when x > 0 -> "bg-emerald-300 text-emerald-900" + x when x < 0 -> "bg-rose-300 text-rose-900" + _ -> "" + end + else + "" + end end end end diff --git a/lib/basket_web/components/dark_mode_toggle.ex b/lib/basket_web/components/dark_mode_toggle.ex index 17c6c71..c772b7c 100644 --- a/lib/basket_web/components/dark_mode_toggle.ex +++ b/lib/basket_web/components/dark_mode_toggle.ex @@ -10,14 +10,17 @@ defmodule BasketWeb.Components.DarkModeToggle do def render(assigns) do ~F""" -
- - -
""" + + # ~F""" + #
+ # + # + #
+ # """ end end diff --git a/lib/basket_web/components/search_input.ex b/lib/basket_web/components/search_input.ex index bb38dd2..b56cadd 100644 --- a/lib/basket_web/components/search_input.ex +++ b/lib/basket_web/components/search_input.ex @@ -30,7 +30,7 @@ defmodule BasketWeb.Components.SearchInput do <.input name="selected-ticker" value="" - field={@ticker_search_form["ticker_search_field"]} + field={assigns.ticker_search_form["ticker_search_field"]} list="tickers" phx-debounce="500" errors={["TODO"]} diff --git a/lib/basket_web/components/ticker_bar_table.ex b/lib/basket_web/components/ticker_bar_table.ex index 6607180..cf346b8 100644 --- a/lib/basket_web/components/ticker_bar_table.ex +++ b/lib/basket_web/components/ticker_bar_table.ex @@ -20,6 +20,8 @@ defmodule BasketWeb.Components.TickerBarTable do end def render(assigns) do + IO.inspect("ROWS: #{@rows}") + ~F"""
<.table id="ticker-list" rows={@rows}> @@ -44,5 +46,6 @@ defmodule BasketWeb.Components.TickerBarTable do """ end - defp value_from_ticker_bar(ticker_bar), do: elem(ticker_bar, 0) + defp value_from_ticker_bar(nil), do: nil + defp value_from_ticker_bar(ticker_bar), do: ticker_bar.value end diff --git a/lib/basket_web/live/overview.ex b/lib/basket_web/live/overview.ex index 1e46969..f1b368a 100644 --- a/lib/basket_web/live/overview.ex +++ b/lib/basket_web/live/overview.ex @@ -32,6 +32,7 @@ defmodule BasketWeb.Overview do end def handle_event("ticker-add", %{"selected-ticker" => ticker}, socket) do + IO.inspect("SOCKET: #{inspect(socket.assigns)}") basket_tickers = tickers(socket) if ticker in basket_tickers or String.trim(ticker) == "" do @@ -43,13 +44,14 @@ defmodule BasketWeb.Overview do case Http.Alpaca.latest_quote(ticker) do {:ok, response} -> %{"bars" => ticker_bars} = response - - initial_bars = build_ticker_bars(ticker_bars) + new_ticker_bars = Map.to_list(ticker_bars) |> List.first() + initial_bars = build_ticker_bars(elem(new_ticker_bars, 1)) assign( socket, :basket, - socket.assigns.basket ++ [initial_bars] + socket.assigns.basket ++ + [Map.merge(initial_bars, %{"S" => %TickerBar{value: ticker}})] ) {:error, error} -> @@ -57,6 +59,9 @@ defmodule BasketWeb.Overview do socket end + form = to_form(%{"selected-ticker" => ""}) + socket = assign(socket, :ticker_search_form, form) + {:reply, %{}, socket} end end @@ -82,21 +87,19 @@ defmodule BasketWeb.Overview do %Phoenix.Socket.Broadcast{topic: "bars", event: "ticker-update", payload: bars}, socket ) do - # Get the old ticker data - old_ticker = Enum.find(socket.assigns.basket, fn t -> t["S"].value == bars["S"].value end) - - bars_with_changes = - Enum.reduce(bars, %{}, fn {k, v}, acc -> - Map.put(acc, k, %TickerBar{value: v, prev_value: old_ticker[k].value}) - end) + IO.inspect("BASKET: #{inspect(socket.assigns.basket)}") + IO.inspect("BARS: #{inspect(bars)}") + ticker = bars["S"] new_basket = Enum.map(socket.assigns.basket, fn row -> - if row["S"].value == bars["S"], - do: bars_with_changes, + if row["S"].value == ticker, + do: new_ticker_row(row, bars), else: row end) + IO.inspect("NB: #{inspect(new_basket)}") + {:noreply, assign( socket, @@ -144,5 +147,12 @@ defmodule BasketWeb.Overview do end end + defp new_ticker_row(row, bars) do + Enum.reduce(row, %{}, fn {k, v}, acc -> + new_value = Map.get(bars, k) + Map.put(acc, k, %TickerBar{value: new_value, prev_value: v.value}) + end) + end + defp tickers(socket), do: Enum.map(socket.assigns.basket, &Map.get(&1, "S").value) end diff --git a/test/basket_web/live/overview_test.exs b/test/basket_web/live/overview_test.exs index 90fd7cb..89f7fef 100644 --- a/test/basket_web/live/overview_test.exs +++ b/test/basket_web/live/overview_test.exs @@ -273,23 +273,38 @@ defmodule BasketWeb.OverviewTest do %Phoenix.Socket.Broadcast{ topic: "bars", event: "ticker-update", - payload: %{ - "S" => %TickerBar{value: "XYZ", prev_value: "XYZ"}, - "c" => %TickerBar{value: 188.15, prev_value: 187.15}, - "h" => %TickerBar{value: 188.15, prev_value: 187.15}, - "l" => %TickerBar{value: 188.15, prev_value: 187.15}, - "n" => %TickerBar{value: 358, prev_value: 358}, - "o" => %TickerBar{value: 188.11, prev_value: 187.15}, - "t" => %TickerBar{ - value: "2023-11-15T20:59:00Z", - prev_value: "2023-11-15T20:59:00Z" + payload: [ + %{ + "S" => %BasketWeb.Overview.TickerBar{value: "TSLA", prev_value: nil}, + "T" => %BasketWeb.Overview.TickerBar{value: "b", prev_value: nil}, + "c" => %BasketWeb.Overview.TickerBar{value: 234.32, prev_value: nil}, + "h" => %BasketWeb.Overview.TickerBar{value: 234.52, prev_value: nil}, + "l" => %BasketWeb.Overview.TickerBar{value: 234.3, prev_value: nil}, + "n" => %BasketWeb.Overview.TickerBar{value: 12, prev_value: nil}, + "o" => %BasketWeb.Overview.TickerBar{value: 234.52, prev_value: nil}, + "t" => %BasketWeb.Overview.TickerBar{ + value: "2023-11-20T16:24:00Z", + prev_value: nil + }, + "v" => %BasketWeb.Overview.TickerBar{value: 856, prev_value: nil}, + "vw" => %BasketWeb.Overview.TickerBar{value: 234.43257, prev_value: nil} }, - "v" => %TickerBar{value: 43_031, prev_value: 43_031}, - "vw" => %TickerBar{ - value: 188.117416, - prev_value: 187.117416 + %{ + "S" => %BasketWeb.Overview.TickerBar{value: "AAPL", prev_value: nil}, + "T" => %BasketWeb.Overview.TickerBar{value: "b", prev_value: nil}, + "c" => %BasketWeb.Overview.TickerBar{value: 191.285, prev_value: nil}, + "h" => %BasketWeb.Overview.TickerBar{value: 191.37, prev_value: nil}, + "l" => %BasketWeb.Overview.TickerBar{value: 191.23, prev_value: nil}, + "n" => %BasketWeb.Overview.TickerBar{value: 50, prev_value: nil}, + "o" => %BasketWeb.Overview.TickerBar{value: 191.23, prev_value: nil}, + "t" => %BasketWeb.Overview.TickerBar{ + value: "2023-11-20T16:24:00Z", + prev_value: nil + }, + "v" => %BasketWeb.Overview.TickerBar{value: 5433, prev_value: nil}, + "vw" => %BasketWeb.Overview.TickerBar{value: 191.328043, prev_value: nil} } - } + ] }, Map.merge(@assigns_map, %{ assigns: %{tickers: [], basket: basket_with_row},