diff --git a/lib/ex338/chats.ex b/lib/ex338/chats.ex index 22ea39be..fd61d464 100644 --- a/lib/ex338/chats.ex +++ b/lib/ex338/chats.ex @@ -28,7 +28,7 @@ defmodule Ex338.Chats do |> Repo.insert() |> case do {:ok, message} -> - message = Repo.preload(message, :user) + message = Repo.preload(message, user: [owners: :fantasy_team]) broadcast(message, %Ex338.Events.MessageCreated{message: message}) {:ok, message} diff --git a/lib/ex338/fantasy_leagues.ex b/lib/ex338/fantasy_leagues.ex index b3b3079e..5fa20999 100644 --- a/lib/ex338/fantasy_leagues.ex +++ b/lib/ex338/fantasy_leagues.ex @@ -136,7 +136,7 @@ defmodule Ex338.FantasyLeagues do from(d in FantasyLeagueDraft, where: d.fantasy_league_id == ^fantasy_league.id and d.championship_id == ^championship.id, - preload: [chat: [messages: :user]] + preload: [chat: [messages: [user: [owners: :fantasy_team]]]] ) Repo.one(query) diff --git a/lib/ex338_web/live/championship_live/show.ex b/lib/ex338_web/live/championship_live/show.ex index 49328f0a..c6bc7be7 100644 --- a/lib/ex338_web/live/championship_live/show.ex +++ b/lib/ex338_web/live/championship_live/show.ex @@ -737,7 +737,12 @@ defmodule Ex338Web.ChampionshipLive.Show do phx-hook="ChatScrollToBottom" class="flex flex-col max-h-[400px] overflow-y-auto overflow-x-hidden" > - <.comment :for={{id, message} <- @messages} id={id} message={message} /> + <.comment + :for={{id, message} <- @messages} + id={id} + message={message} + fantasy_league={@fantasy_league} + /> <.live_component @@ -781,6 +786,10 @@ defmodule Ex338Web.ChampionshipLive.Show do """ end + attr :id, :string, required: true + attr :message, :map, required: true + attr :fantasy_league, :map, required: true + defp comment(%{message: %{user: nil}} = assigns) do ~H"""
  • @@ -799,21 +808,19 @@ defmodule Ex338Web.ChampionshipLive.Show do defp comment(assigns) do ~H""" -
  • - <.user_icon name={@message.user.name} /> -
    -
    -
    - <%= @message.user.name %> -
    -
    - <.local_time for={@message.inserted_at} preset="DATETIME_SHORT" /> -
    -
    -

    - <%= @message.content %> +

  • +
    + <.user_icon name={@message.user.name} /> +

    + <%= user_name(@message.user, @fantasy_league) %>

    +
    + <.local_time for={@message.inserted_at} preset="DATETIME_SHORT" /> +
    +

    + <%= @message.content %> +

  • """ end @@ -832,6 +839,21 @@ defmodule Ex338Web.ChampionshipLive.Show do """ end + defp user_name(user, fantasy_league) do + with owner when not is_nil(owner) <- get_owner_in_league(user, fantasy_league), + false <- owner.fantasy_team.team_name == user.name do + "#{user.name} - #{owner.fantasy_team.team_name}" + else + _ -> user.name + end + end + + defp get_owner_in_league(user, fantasy_league) do + Enum.find(user.owners, fn owner -> + owner.fantasy_team.fantasy_league_id == fantasy_league.id + end) + end + defp get_initials(name) do name |> String.split(" ") diff --git a/test/ex338_web/live/championship_live/show_test.exs b/test/ex338_web/live/championship_live/show_test.exs index fede1649..20e56c93 100644 --- a/test/ex338_web/live/championship_live/show_test.exs +++ b/test/ex338_web/live/championship_live/show_test.exs @@ -297,7 +297,7 @@ defmodule Ex338Web.ChampionshipLive.ShowTest do ) another_user = insert(:user) - team_b = insert(:fantasy_team, fantasy_league: league) + team_b = insert(:fantasy_team, fantasy_league: league, team_name: another_user.name) insert(:owner, user: another_user, fantasy_team: team_b) pick2 = @@ -343,6 +343,7 @@ defmodule Ex338Web.ChampionshipLive.ShowTest do |> form("#create-message-form", %{message: %{content: "My team is awesome!"}}) |> render_submit() + assert has_element?(view, "div", "#{user.name} - #{team_a.team_name}") assert has_element?(view, "p", "My team is awesome!") {:ok, _chat} = @@ -354,6 +355,8 @@ defmodule Ex338Web.ChampionshipLive.ShowTest do render(view) + refute has_element?(view, "div", "#{another_user.name} - #{team_b.team_name}") + assert has_element?(view, "div", "#{another_user.name}") assert has_element?(view, "p", "Wow") InSeasonDraftPicks.draft_player(in_season_draft_pick, %{