Skip to content

Commit

Permalink
presentation cleanup and clear search input work
Browse files Browse the repository at this point in the history
  • Loading branch information
daveminer committed Nov 20, 2023
1 parent 5443bfb commit ad5c723
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 46 deletions.
4 changes: 4 additions & 0 deletions lib/basket/websocket/alpaca.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
22 changes: 13 additions & 9 deletions lib/basket_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 11 additions & 8 deletions lib/basket_web/components/dark_mode_toggle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ defmodule BasketWeb.Components.DarkModeToggle do

def render(assigns) do
~F"""
<div class="justify-center" phx-click={JS.dispatch("toggle-darkmode")}>
<input type="checkbox" name="light-switch" class="light-switch sr-only">
<label class="relative cursor-pointer p-2" for="light-switch">
<.icon name="hero-sun-solid" class="hidden dark:inline-block w-8 h-8 text-yellow-500" />
<.icon name="hero-moon-solid" class="dark:hidden w-8 h-8 text-blue-200" />
<span class="sr-only">Switch between light and dark mode</span>
</label>
</div>
"""

# ~F"""
# <div class="justify-center" phx-click={JS.dispatch("toggle-darkmode")}>
# <input type="checkbox" name="light-switch" class="light-switch sr-only">
# <label class="relative cursor-pointer p-2" for="light-switch">
# <.icon name="hero-sun-solid" class="hidden dark:inline-block w-8 h-8 text-yellow-500" />
# <.icon name="hero-moon-solid" class="dark:hidden w-8 h-8 text-blue-200" />
# <span class="sr-only">Switch between light and dark mode</span>
# </label>
# </div>
# """
end
end
2 changes: 1 addition & 1 deletion lib/basket_web/components/search_input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand Down
5 changes: 4 additions & 1 deletion lib/basket_web/components/ticker_bar_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule BasketWeb.Components.TickerBarTable do
end

def render(assigns) do
IO.inspect("ROWS: #{@rows}")

Check warning on line 23 in lib/basket_web/components/ticker_bar_table.ex

View workflow job for this annotation

GitHub Actions / test

undefined module attribute @rows, please remove access to @rows or explicitly set it before access

~F"""
<div>
<.table id="ticker-list" rows={@rows}>
Expand All @@ -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
34 changes: 22 additions & 12 deletions lib/basket_web/live/overview.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,20 +44,24 @@ 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} ->
Logger.error("Could not subscribe to ticker: #{error}")
socket
end

form = to_form(%{"selected-ticker" => ""})
socket = assign(socket, :ticker_search_form, form)

{:reply, %{}, socket}
end
end
Expand All @@ -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,
Expand Down Expand Up @@ -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
45 changes: 30 additions & 15 deletions test/basket_web/live/overview_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit ad5c723

Please sign in to comment.