Skip to content

Commit

Permalink
Covert in season form to live component
Browse files Browse the repository at this point in the history
* Add test for in season draft pick form component
* Reorder queues and send league email
* See #1299
* See #1229
  • Loading branch information
axelclark committed Apr 10, 2024
1 parent 80a3454 commit 7527087
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 11 deletions.
12 changes: 12 additions & 0 deletions lib/ex338/fantasy_team_authorizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ defmodule Ex338.FantasyTeamAuthorizer do
end
end

def authorize(
:edit_in_season_draft_pick,
%User{} = user,
%InSeasonDraftPick{} = in_season_draft_pick
) do
if owner?(user.id, in_season_draft_pick) do
:ok
else
{:error, :not_authorized}
end
end

defp owner?(user_id, %DraftPick{} = draft_pick) do
draft_pick = Repo.preload(draft_pick, fantasy_team: :owners)
owners = draft_pick.fantasy_team.owners
Expand Down
7 changes: 7 additions & 0 deletions lib/ex338/in_season_draft_picks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ defmodule Ex338.InSeasonDraftPicks do
Phoenix.PubSub.subscribe(Ex338.PubSub, @topic)
end

def change_in_season_draft_pick_as_owner(
%InSeasonDraftPick{} = in_season_draft_pick,
attrs \\ %{}
) do
InSeasonDraftPick.owner_changeset(in_season_draft_pick, attrs)
end

## Helpers

## draft_player
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
defmodule Ex338Web.ChampionshipLive.InSeasonDraftPickFormComponent do
@moduledoc false
use Ex338Web, :live_component

alias Ex338.DraftQueues
alias Ex338.InSeasonDraftPicks
alias Ex338Web.InSeasonDraftPickNotifier

@impl true
def render(assigns) do
~H"""
<div>
<.header>
Submit <%= @in_season_draft_pick.championship.title %> Draft Pick
<:subtitle>
Please make a selection for <%= @in_season_draft_pick.draft_pick_asset.fantasy_team.team_name %>'s
round <%= @in_season_draft_pick.position %> pick.
</:subtitle>
</.header>
<.simple_form
for={@form}
id="in-season-draft-pick-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
class="max-w-lg"
>
<.input
field={@form[:drafted_player_id]}
label="Player to Draft"
type="select"
options={format_players_for_select(@available_fantasy_players)}
prompt="Select a fantasy player"
/>
<:actions>
<.button phx-disable-with="Submitting...">Submit Draft Pick</.button>
</:actions>
</.simple_form>
</div>
"""
end

@impl true
def update(%{in_season_draft_pick: in_season_draft_pick} = assigns, socket) do
changeset = InSeasonDraftPicks.change_in_season_draft_pick_as_owner(in_season_draft_pick)

{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
end

@impl true
def handle_event("validate", %{"in_season_draft_pick" => in_season_draft_pick_params}, socket) do
changeset =
socket.assigns.in_season_draft_pick
|> InSeasonDraftPicks.change_in_season_draft_pick_as_owner(in_season_draft_pick_params)
|> Map.put(:action, :validate)

{:noreply, assign_form(socket, changeset)}
end

def handle_event("save", %{"in_season_draft_pick" => in_season_draft_pick_params}, socket) do
case InSeasonDraftPicks.draft_player(
socket.assigns.in_season_draft_pick,
in_season_draft_pick_params
) do
{:ok, %{update_pick: in_season_draft_pick}} ->
DraftQueues.reorder_for_league(socket.assigns.fantasy_league.id)
InSeasonDraftPickNotifier.send_update(in_season_draft_pick)

{:noreply, push_patch(socket, to: socket.assigns.patch)}

{:error, _multi_action, %Ecto.Changeset{} = changeset, _} ->
{:noreply, assign_form(socket, changeset)}
end
end

defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
end
82 changes: 72 additions & 10 deletions lib/ex338_web/live/championship_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ defmodule Ex338Web.ChampionshipLive.Show do
alias Ex338.Chats.Message
alias Ex338.Events
alias Ex338.FantasyLeagues
alias Ex338.FantasyTeamAuthorizer
alias Ex338.InSeasonDraftPicks
alias Ex338.InSeasonDraftPicks.InSeasonDraftPick
alias Ex338Web.ChampionshipLive.ChatComponent
alias Ex338Web.Presence

require Logger

@impl true
def mount(_params, _session, socket) do
if connected?(socket) do
Expand All @@ -26,19 +30,18 @@ defmodule Ex338Web.ChampionshipLive.Show do
def handle_params(params, _session, socket) do
%{"fantasy_league_id" => fantasy_league_id, "championship_id" => championship_id} = params

fantasy_league = FantasyLeagues.get(fantasy_league_id)

championship =
Championships.get_championship_by_league(championship_id, fantasy_league_id)

socket =
socket
|> assign(
:championship,
Championships.get_championship_by_league(
championship_id,
fantasy_league_id
)
)
|> assign(:fantasy_league, FantasyLeagues.get(fantasy_league_id))
|> assign(:fantasy_league, fantasy_league)
|> assign(:championship, championship)
|> maybe_assign_chat()

{:noreply, socket}
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

defp maybe_assign_chat(socket) do
Expand Down Expand Up @@ -83,6 +86,39 @@ defmodule Ex338Web.ChampionshipLive.Show do
}
end

defp apply_action(socket, :in_season_draft_pick_edit, params) do
with pick_id when not is_nil(pick_id) <- params["in_season_draft_pick_id"],
%InSeasonDraftPick{} = in_season_draft_pick <-
InSeasonDraftPicks.pick_with_assocs(pick_id),
:ok <- authorize_pick(socket, in_season_draft_pick) do
available_fantasy_players = InSeasonDraftPicks.available_players(in_season_draft_pick)

socket
|> assign(:in_season_draft_pick, in_season_draft_pick)
|> assign(:available_fantasy_players, available_fantasy_players)
else
_nil_or_error ->
%{fantasy_league: fantasy_league, championship: championship} = socket.assigns
push_navigate(socket, to: show_path(fantasy_league, championship))
end
end

defp apply_action(socket, :show, _params) do
socket
end

defp show_path(fantasy_league, championship) do
~p"/fantasy_leagues/#{fantasy_league}/championships/#{championship}"
end

defp authorize_pick(socket, in_season_draft_pick) do
FantasyTeamAuthorizer.authorize(
:edit_in_season_draft_pick,
socket.assigns.current_user,
in_season_draft_pick
)
end

@impl true
def handle_info(:refresh, socket) do
championship = Championships.update_next_in_season_pick(socket.assigns.championship)
Expand Down Expand Up @@ -143,6 +179,11 @@ defmodule Ex338Web.ChampionshipLive.Show do
{:noreply, assign(socket, users: users)}
end

def handle_info(message, socket) do
Logger.info("Unhandled message: #{inspect(message)}")
{:noreply, socket}
end

# Implementations

defp schedule_refresh do
Expand Down Expand Up @@ -335,6 +376,7 @@ defmodule Ex338Web.ChampionshipLive.Show do
championship={@championship}
socket={@socket}
current_user={@current_user}
fantasy_league={@fantasy_league}
/>
</div>
<% end %>
Expand All @@ -355,6 +397,21 @@ defmodule Ex338Web.ChampionshipLive.Show do
</div>
<% end %>
</div>
<.modal
:if={@live_action == :in_season_draft_pick_edit}
id="in-season-draft-pick-modal"
show
on_cancel={JS.patch(show_path(@fantasy_league, @championship))}
>
<.live_component
module={Ex338Web.ChampionshipLive.InSeasonDraftPickFormComponent}
id={@in_season_draft_pick.id}
fantasy_league={@fantasy_league}
in_season_draft_pick={@in_season_draft_pick}
available_fantasy_players={@available_fantasy_players}
patch={show_path(@fantasy_league, @championship)}
/>
</.modal>
"""
end

Expand Down Expand Up @@ -570,7 +627,12 @@ defmodule Ex338Web.ChampionshipLive.Show do
<%= pick.drafted_player.player_name %>
<% else %>
<%= if pick.available_to_pick? && (owner?(@current_user, pick) || admin?(@current_user)) do %>
<.link href={~p"/in_season_draft_picks/#{pick}/edit"} class="text-indigo-700">
<.link
patch={
~p"/fantasy_leagues/#{@fantasy_league}/championships/#{@championship}/in_season_draft_picks/#{pick}/edit"
}
class="text-indigo-700"
>
Submit Pick
</.link>
<% end %>
Expand Down
5 changes: 5 additions & 0 deletions lib/ex338_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ defmodule Ex338Web.Router do
scope "/fantasy_leagues/:fantasy_league_id" do
live "/draft_picks", DraftPickLive.Index, :index
live "/championships", ChampionshipLive.Index, :index

live "/championships/:championship_id/in_season_draft_picks/:in_season_draft_pick_id/edit",
ChampionshipLive.Show,
:in_season_draft_pick_edit

live "/championships/:championship_id", ChampionshipLive.Show, :show
end
end
Expand Down
Loading

0 comments on commit 7527087

Please sign in to comment.