Skip to content

Commit

Permalink
Add fantasy team name to chat
Browse files Browse the repository at this point in the history
* Add the team only if in the current league
* Don't add if a duplicate of the user name
* See #1309
  • Loading branch information
axelclark committed Apr 14, 2024
1 parent ff65dbd commit b48a986
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/ex338/chats.ex
Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion lib/ex338/fantasy_leagues.ex
Expand Up @@ -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)
Expand Down
50 changes: 36 additions & 14 deletions lib/ex338_web/live/championship_live/show.ex
Expand Up @@ -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}
/>
</ul>
<.live_component
Expand Down Expand Up @@ -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"""
<li id={@id} class="flex gap-x-4 hover:bg-gray-50 px-4 sm:px-6 py-2">
Expand All @@ -799,21 +808,19 @@ defmodule Ex338Web.ChampionshipLive.Show do

defp comment(assigns) do
~H"""
<li id={@id} class="flex gap-x-4 hover:bg-gray-50 px-4 sm:px-6 py-2">
<.user_icon name={@message.user.name} />
<div class="flex-auto">
<div class="flex justify-between items-start gap-x-4">
<div class="text-xs leading-5 font-medium text-gray-900">
<%= @message.user.name %>
</div>
<div class="flex-none py-0.5 text-xs leading-5 text-gray-500">
<.local_time for={@message.inserted_at} preset="DATETIME_SHORT" />
</div>
</div>
<p class="text-sm leading-6 text-gray-500">
<%= @message.content %>
<li id={@id} class="hover:bg-gray-50 px-4 sm:px-6 py-2">
<div class="flex gap-x-4">
<.user_icon name={@message.user.name} />
<p class="flex-auto text-xs leading-5 font-medium text-gray-900 truncate">
<%= user_name(@message.user, @fantasy_league) %>
</p>
<div class="flex-none py-0.5 whitespace-nowrap text-xs leading-5 text-gray-500">
<.local_time for={@message.inserted_at} preset="DATETIME_SHORT" />
</div>
</div>
<p class="pl-10 text-xs leading-6 text-gray-500">
<%= @message.content %>
</p>
</li>
"""
end
Expand All @@ -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(" ")
Expand Down
5 changes: 4 additions & 1 deletion test/ex338_web/live/championship_live/show_test.exs
Expand Up @@ -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 =
Expand Down Expand Up @@ -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} =
Expand All @@ -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, %{
Expand Down

0 comments on commit b48a986

Please sign in to comment.