use strict context boundaries and remove all n+1 queries
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -5,8 +5,12 @@ defmodule CanneryWeb.RangeLive.FormComponent do
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup}
|
||||
alias Ecto.Changeset
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
def mount(socket), do: {:ok, socket |> assign(:ammo_group, nil)}
|
||||
|
||||
@impl true
|
||||
@spec update(
|
||||
%{
|
||||
@ -19,28 +23,23 @@ defmodule CanneryWeb.RangeLive.FormComponent do
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(
|
||||
%{
|
||||
shot_group: %ShotGroup{ammo_group_id: ammo_group_id} = shot_group,
|
||||
shot_group: %ShotGroup{ammo_group_id: ammo_group_id},
|
||||
current_user: current_user
|
||||
} = assigns,
|
||||
socket
|
||||
) do
|
||||
changeset = shot_group |> ShotGroup.update_changeset(current_user, %{})
|
||||
)
|
||||
when is_binary(ammo_group_id) do
|
||||
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
|
||||
{:ok, socket |> assign(assigns) |> assign(ammo_group: ammo_group, changeset: changeset)}
|
||||
{:ok, socket |> assign(assigns) |> assign(:ammo_group, ammo_group) |> assign_changeset(%{})}
|
||||
end
|
||||
|
||||
def update(%{shot_group: %ShotGroup{}} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"shot_group" => shot_group_params},
|
||||
%{assigns: %{current_user: current_user, shot_group: shot_group}} = socket
|
||||
) do
|
||||
changeset =
|
||||
shot_group
|
||||
|> ShotGroup.update_changeset(current_user, shot_group_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
def handle_event("validate", %{"shot_group" => shot_group_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(shot_group_params, :validate)}
|
||||
end
|
||||
|
||||
def handle_event(
|
||||
@ -61,4 +60,37 @@ defmodule CanneryWeb.RangeLive.FormComponent do
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp assign_changeset(
|
||||
%{
|
||||
assigns: %{
|
||||
action: live_action,
|
||||
current_user: user,
|
||||
ammo_group: ammo_group,
|
||||
shot_group: shot_group
|
||||
}
|
||||
} = socket,
|
||||
shot_group_params,
|
||||
action \\ nil
|
||||
) do
|
||||
default_action =
|
||||
case live_action do
|
||||
:add_shot_group -> :insert
|
||||
editing when editing in [:edit, :edit_shot_group] -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case default_action do
|
||||
:insert -> shot_group |> ShotGroup.create_changeset(user, ammo_group, shot_group_params)
|
||||
:update -> shot_group |> ShotGroup.update_changeset(user, shot_group_params)
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(action || default_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
end
|
||||
|
@ -4,7 +4,7 @@ defmodule CanneryWeb.RangeLive.Index do
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_view
|
||||
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo, Repo}
|
||||
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo}
|
||||
alias CanneryWeb.Endpoint
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@ -104,16 +104,19 @@ defmodule CanneryWeb.RangeLive.Index do
|
||||
|
||||
@spec display_shot_groups(Socket.t()) :: Socket.t()
|
||||
defp display_shot_groups(%{assigns: %{search: search, current_user: current_user}} = socket) do
|
||||
shot_groups =
|
||||
ActivityLog.list_shot_groups(search, current_user)
|
||||
|> Repo.preload(ammo_group: :ammo_type)
|
||||
|
||||
shot_groups = ActivityLog.list_shot_groups(search, current_user)
|
||||
ammo_groups = Ammo.list_staged_ammo_groups(current_user)
|
||||
chart_data = shot_groups |> get_chart_data_for_shot_group()
|
||||
original_counts = ammo_groups |> Ammo.get_original_counts(current_user)
|
||||
cprs = ammo_groups |> Ammo.get_cprs(current_user)
|
||||
last_used_dates = ammo_groups |> ActivityLog.get_last_used_dates(current_user)
|
||||
|
||||
socket
|
||||
|> assign(
|
||||
ammo_groups: ammo_groups,
|
||||
original_counts: original_counts,
|
||||
cprs: cprs,
|
||||
last_used_dates: last_used_dates,
|
||||
chart_data: chart_data,
|
||||
shot_groups: shot_groups
|
||||
)
|
||||
@ -122,7 +125,6 @@ defmodule CanneryWeb.RangeLive.Index do
|
||||
@spec get_chart_data_for_shot_group([ShotGroup.t()]) :: [map()]
|
||||
defp get_chart_data_for_shot_group(shot_groups) do
|
||||
shot_groups
|
||||
|> Repo.preload(ammo_group: :ammo_type)
|
||||
|> Enum.group_by(fn %{date: date} -> date end, fn %{count: count} -> count end)
|
||||
|> Enum.map(fn {date, rounds} ->
|
||||
sum = Enum.sum(rounds)
|
||||
|
@ -18,7 +18,14 @@
|
||||
</.link>
|
||||
|
||||
<div class="w-full flex flex-row flex-wrap justify-center items-stretch">
|
||||
<.ammo_group_card :for={ammo_group <- @ammo_groups} ammo_group={ammo_group}>
|
||||
<.ammo_group_card
|
||||
:for={%{id: ammo_group_id} = ammo_group <- @ammo_groups}
|
||||
ammo_group={ammo_group}
|
||||
original_count={Map.fetch!(@original_counts, ammo_group_id)}
|
||||
cpr={Map.get(@cprs, ammo_group_id)}
|
||||
last_used_date={Map.get(@last_used_dates, ammo_group_id)}
|
||||
current_user={@current_user}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
@ -134,29 +141,30 @@
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<.modal :if={@live_action == :edit} return_to={Routes.range_index_path(Endpoint, :index)}>
|
||||
<.live_component
|
||||
module={CanneryWeb.RangeLive.FormComponent}
|
||||
id={@shot_group.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
shot_group={@shot_group}
|
||||
return_to={Routes.range_index_path(Endpoint, :index)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
|
||||
<.modal
|
||||
:if={@live_action == :add_shot_group}
|
||||
return_to={Routes.range_index_path(Endpoint, :index)}
|
||||
>
|
||||
<.live_component
|
||||
module={CanneryWeb.Components.AddShotGroupComponent}
|
||||
id={:new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
ammo_group={@ammo_group}
|
||||
return_to={Routes.range_index_path(Endpoint, :index)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<%= case @live_action do %>
|
||||
<% :edit -> %>
|
||||
<.modal return_to={Routes.range_index_path(Endpoint, :index)}>
|
||||
<.live_component
|
||||
module={CanneryWeb.RangeLive.FormComponent}
|
||||
id={@shot_group.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
shot_group={@shot_group}
|
||||
return_to={Routes.range_index_path(Endpoint, :index)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<% :add_shot_group -> %>
|
||||
<.modal return_to={Routes.range_index_path(Endpoint, :index)}>
|
||||
<.live_component
|
||||
module={CanneryWeb.Components.AddShotGroupComponent}
|
||||
id={:new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
ammo_group={@ammo_group}
|
||||
return_to={Routes.range_index_path(Endpoint, :index)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<% _ -> %>
|
||||
<% end %>
|
||||
|
Reference in New Issue
Block a user