diff --git a/lib/basket_web/live/overview.ex b/lib/basket_web/live/overview.ex index 8d92a3e..60d8dfe 100644 --- a/lib/basket_web/live/overview.ex +++ b/lib/basket_web/live/overview.ex @@ -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 @@ -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 @@ -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]) diff --git a/lib/basket_web/live/overview/ticker_bar_table.ex b/lib/basket_web/live/overview/ticker_bar_table.ex index 261a9a8..3eaa415 100644 --- a/lib/basket_web/live/overview/ticker_bar_table.ex +++ b/lib/basket_web/live/overview/ticker_bar_table.ex @@ -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: diff --git a/test/basket/websocket/alpaca_test.exs b/test/basket/websocket/alpaca_test.exs index 9bca77b..cf2f09c 100644 --- a/test/basket/websocket/alpaca_test.exs +++ b/test/basket/websocket/alpaca_test.exs @@ -19,13 +19,13 @@ 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 @@ -33,13 +33,13 @@ defmodule Basket.Websocket.AlpacaTest do 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 diff --git a/test/basket_web/live/overview_test.exs b/test/basket_web/live/overview_test.exs index 6395a7a..9d6bdde 100644 --- a/test/basket_web/live/overview_test.exs +++ b/test/basket_web/live/overview_test.exs @@ -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 @@ -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 diff --git a/test/support/factory.ex b/test/support/factory.ex index 1510e89..b396a35 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -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,