Skip to content

Commit

Permalink
refactor ticker add
Browse files Browse the repository at this point in the history
  • Loading branch information
daveminer committed Nov 24, 2023
1 parent 5775310 commit 6792883
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 79 deletions.
32 changes: 12 additions & 20 deletions lib/basket_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,6 @@ defmodule BasketWeb.CoreComponents do
slot :inner_block

def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
IO.inspect("ASS: #{assigns}")

assigns
|> assign(field: nil, id: assigns.id || field.id)
|> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
Expand Down Expand Up @@ -407,8 +405,6 @@ defmodule BasketWeb.CoreComponents do

# All other inputs text, datetime-local, url, password, etc. are handled here...
def input(assigns) do
IO.inspect("DEF: #{inspect(assigns)}")

~H"""
<div phx-feedback-for={@name}>
<.label for={@id}><%= @label %></.label>
Expand Down Expand Up @@ -541,7 +537,7 @@ defmodule BasketWeb.CoreComponents do
"relative p-0",
"text-center",
@row_click && "hover:cursor-pointer",
diff_color(col, row)
diff_cell_color(col, row)
]}
>
<div class="block py-4 pr-6">
Expand Down Expand Up @@ -722,23 +718,19 @@ defmodule BasketWeb.CoreComponents do
for {^field, {msg, opts}} <- errors, do: translate_error({msg, opts})
end

defp diff_color(col, row) do
key = Map.get(col, :key)
defp diff_cell_color(%{key: nil}, _row), do: ""

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
""
defp diff_cell_color(%{key: key} = _col, row) do
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
26 changes: 0 additions & 26 deletions lib/basket_web/components/dark_mode_toggle.ex

This file was deleted.

2 changes: 0 additions & 2 deletions lib/basket_web/components/nav_row.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ defmodule BasketWeb.Components.NavRow do
@moduledoc """
The header for the home page.
"""
alias BasketWeb.Components.DarkModeToggle

use Surface.Component

def render(assigns) do
~F"""
<div class="flex flex-row gap-4 items-center justify-end">
<DarkModeToggle.render />
<.link href="/session" method="delete">Sign out</.link>
</div>
"""
Expand Down
50 changes: 19 additions & 31 deletions lib/basket_web/live/overview.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ defmodule BasketWeb.OverviewLive do

require Logger

alias Basket.{Http, Websocket}
alias Basket.Websocket
alias BasketWeb.Components.{NavRow, TickerBarTable}
alias BasketWeb.Live.Overview.Search
alias BasketWeb.Live.Overview.TickerBar
alias BasketWeb.Live.Overview.{Search, TickerAdd, TickerBar}

def mount(_, _, socket) do
BasketWeb.Endpoint.subscribe(Websocket.Alpaca.bars_topic())
Expand All @@ -24,25 +23,18 @@ defmodule BasketWeb.OverviewLive do
{:noreply, socket}
else
socket =
case Http.Alpaca.latest_quote(ticker) do
{:ok, response} ->
%{"bars" => ticker_bars} = response
new_ticker_bars = Map.to_list(ticker_bars) |> List.first()
# TODO: nil not a tuple - AKUMQ
initial_bars = build_ticker_bars(elem(new_ticker_bars, 1))

assign(
socket,
:basket,
socket.assigns.basket ++
[Map.merge(initial_bars, %{"S" => %TickerBar{value: ticker}})]
)

{:error, error} ->
Logger.error("Could not subscribe to ticker: #{error}")
end
case TickerAdd.call(ticker) do
row when is_map(row) ->
:ok = Websocket.Alpaca.subscribe(%{bars: [ticker], quotes: [], trades: []})
assign(socket, :basket, (socket.assigns.basket ++ [row]) |> sort_by_ticker())

:market_closed ->
# TODO: add market closed row
socket

:ok = Websocket.Alpaca.subscribe(%{bars: [ticker], quotes: [], trades: []})
_ ->
socket
end

{:noreply, socket}
end
Expand Down Expand Up @@ -98,22 +90,18 @@ defmodule BasketWeb.OverviewLive do
"""
end

defp build_ticker_bars(ticker_bars) do
if ticker_bars == %{} do
%{"t" => "Market Closed"}
else
Enum.reduce(ticker_bars, %{}, fn {k, v}, acc ->
Map.put(acc, k, %TickerBar{value: v})
end)
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 sort_by_ticker(bars),
do:
Enum.sort(bars, fn a, b ->
a["S"].value < b["S"].value
end)

defp tickers(socket), do: Enum.map(socket.assigns.basket, &Map.get(&1, "S").value)
end
58 changes: 58 additions & 0 deletions lib/basket_web/live/overview/ticker_add.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule BasketWeb.Live.Overview.TickerAdd do
@moduledoc """
Creates a new ticker in the table.
"""

alias Basket.Http
alias BasketWeb.Live.Overview.TickerBar

require Logger

@doc """
Creates a row to be added to the ticker bar table.
"""
def call(ticker) do
case Http.Alpaca.latest_quote(ticker) do
{:ok, response} ->
case build_ticker_bars(response) do
:no_data ->
# TODO: info flash
:no_data

:market_closed ->
:market_closed

bars ->
bars
end

{:error, error} ->
# TODO: error flash
Logger.error("Could not subscribe to ticker: #{error}")
end
end

defp build_ticker_bars(%{"bars" => nil}) do
end

defp build_ticker_bars(%{"bars" => ticker_bars}) do
# TODO: check first
new_ticker_bars = Map.to_list(ticker_bars) |> List.first()

case new_ticker_bars do
nil ->
:no_data

%{} ->
:market_closed

bars ->
Enum.reduce(bars, %{}, fn {k, v}, acc ->
Map.put(acc, k, %TickerBar{value: v})
end)

# TODO: check
# Map.merge(new_bars, %{"S" => %TickerBar{value: ticker}})
end
end
end

0 comments on commit 6792883

Please sign in to comment.