Skip to content

Commit

Permalink
add connected check to overview liveview
Browse files Browse the repository at this point in the history
  • Loading branch information
daveminer committed Jul 8, 2024
1 parent afdb1e4 commit 3e840a8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
17 changes: 14 additions & 3 deletions lib/basket_web/live/overview.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ defmodule BasketWeb.Live.Overview do
on_mount {BasketWeb.Live.UserLiveAuth, :user}

def mount(_, _, socket) do
{:ok, initialize(socket)}
socket = assign(socket, :basket, [])

if connected?(socket) do
{:ok, initialize(socket)}
else
{:ok, socket}
end
end

def handle_info({"ticker-add", %{"ticker" => ticker}}, socket) do
Expand All @@ -31,7 +37,7 @@ defmodule BasketWeb.Live.Overview do
Ticker.add!(user, ticker)
end

{:noreply, add_tickers_to_view(socket, ["#{ticker}"])}
{:noreply, add_tickers_to_view(socket, ticker)}
end
end

Expand Down Expand Up @@ -135,7 +141,12 @@ defmodule BasketWeb.Live.Overview do
user.settings["ticker_view_toggle"] == "club"
end

defp in_club?(user), do: Enum.any?(user.clubs)
defp in_club?(user) do
case user.clubs do
%Ecto.Association.NotLoaded{} -> false
clubs -> Enum.any?(clubs)
end
end

defp initialize(socket) do
user = Repo.get(User, socket.assigns.user.id) |> Repo.preload([:clubs, :offices])
Expand Down
2 changes: 1 addition & 1 deletion lib/basket_web/live/overview/ticker_bar_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule BasketWeb.Live.Overview.TickerBarTable do
end
end

def call(ticker, user_id), do: call([ticker], user_id)
def add_ticker(ticker, user_id), do: add_ticker([ticker], user_id)

defp subscribe_to_tickers(tickers, user_id),
do:
Expand Down
8 changes: 4 additions & 4 deletions test/basket/websocket/alpaca_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ defmodule Basket.Websocket.AlpacaTest do

expect(Basket.Websocket.MockClient, :send_frame, fn _,
{:text,
"{\"action\":\"subscribe\",\"bars\":[\"XYZ\"],\"quotes\":[],\"trades\":[]}"} ->
"{\"action\":\"subscribe\",\"trades\":[],\"quotes\":[],\"bars\":[\"ALPHA\"]}"} ->
:ok
end)

{:ok, _pid} = Alpaca.start_link(%{})

assert :ok == Alpaca.subscribe(%{bars: ["XYZ"], quotes: [], trades: []})
assert :ok == Alpaca.subscribe(%{bars: ["ALPHA"], quotes: [], trades: []})
end

test "unsubscribe/1" do

Check failure on line 31 in test/basket/websocket/alpaca_test.exs

View workflow job for this annotation

GitHub Actions / test

test Alpaca websocket client lifecycle unsubscribe/1 (Basket.Websocket.AlpacaTest)
expect(Basket.Websocket.MockClient, :start_link, fn _, _, _, _ -> {:ok, self()} end)

expect(Basket.Websocket.MockClient, :send_frame, fn _,
{:text,
"{\"action\":\"unsubscribe\",\"bars\":[\"XYZ\"],\"quotes\":[],\"trades\":[]}"} ->
"{\"action\":\"unsubscribe\",\"trades\":[],\"quotes\":[],\"bars\":[\"ALPHA\"]}"} ->
:ok
end)

{:ok, _pid} = Alpaca.start_link(%{})

assert :ok == Alpaca.unsubscribe(%{bars: ["XYZ"], quotes: [], trades: []})
assert :ok == Alpaca.unsubscribe(%{bars: ["ALPHA"], quotes: [], trades: []})
end
end

Expand Down
7 changes: 5 additions & 2 deletions test/basket_web/live/overview_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule BasketWeb.Live.OverviewTest do
import Phoenix.LiveViewTest

alias BasketWeb.Live.Overview.TickerRow
alias Basket.{Ticker, Users.User}
alias Basket.{Ticker, User}
alias Pow.Ecto.Schema.Password

setup :set_mox_global
Expand All @@ -21,7 +21,10 @@ defmodule BasketWeb.Live.OverviewTest do

# create a user
user =
%User{email: "test@example.com", password_hash: password_hash}
%User{
email: "test@example.com",
password_hash: password_hash
}
|> Basket.Repo.insert!()

# give the user a ticker
Expand Down
18 changes: 0 additions & 18 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,6 @@ defmodule Basket.Factory do
end

def socket_factory do
# %Phoenix.Socket{
# assigns: %{},
# channel: BasketWeb.TickerChannel,
# channel_pid: self(),
# endpoint: BasketWeb.Endpoint,
# handler: BasketWeb.UserSocket,
# id: nil,
# join_ref: "1",
# joined: true,
# private: %{},
# pubsub_server: Basket.PubSub,
# ref: nil,
# serializer: Jason,
# topic: "topic",
# transport: :websocket,
# transport_pid: self()
# }

%Phoenix.LiveView.Socket{
assigns: %{__changed__: %{}},
endpoint: BasketWeb.Endpoint,
Expand Down

0 comments on commit 3e840a8

Please sign in to comment.