remove all n+1 queries for real this time
This commit is contained in:
@ -108,14 +108,21 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do
|
||||
[%{label: gettext("Ammo type"), key: :ammo_type} | columns]
|
||||
end
|
||||
|
||||
containers =
|
||||
ammo_groups
|
||||
|> Enum.map(fn %{container_id: container_id} -> container_id end)
|
||||
|> Containers.get_containers(current_user)
|
||||
|
||||
extra_data = %{
|
||||
current_user: current_user,
|
||||
ammo_type: ammo_type,
|
||||
columns: columns,
|
||||
container: container,
|
||||
containers: containers,
|
||||
original_counts: Ammo.get_original_counts(ammo_groups, current_user),
|
||||
cprs: Ammo.get_cprs(ammo_groups, current_user),
|
||||
last_used_dates: ActivityLog.get_last_used_dates(ammo_groups, current_user),
|
||||
percentages_remaining: Ammo.get_percentages_remaining(ammo_groups, current_user),
|
||||
actions: actions,
|
||||
range: range
|
||||
}
|
||||
@ -202,8 +209,12 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do
|
||||
"""}
|
||||
end
|
||||
|
||||
defp get_value_for_key(:remaining, ammo_group, %{current_user: current_user}) do
|
||||
percentage = ammo_group |> Ammo.get_percentage_remaining(current_user)
|
||||
defp get_value_for_key(
|
||||
:remaining,
|
||||
%{id: ammo_group_id},
|
||||
%{percentages_remaining: percentages_remaining}
|
||||
) do
|
||||
percentage = Map.fetch!(percentages_remaining, ammo_group_id)
|
||||
{percentage, gettext("%{percentage}%", percentage: percentage)}
|
||||
end
|
||||
|
||||
@ -220,12 +231,13 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do
|
||||
defp get_value_for_key(
|
||||
:container,
|
||||
%{container_id: container_id} = ammo_group,
|
||||
%{container: container, current_user: current_user}
|
||||
%{container: container_block, containers: containers}
|
||||
) do
|
||||
container = %{name: container_name} = Map.fetch!(containers, container_id)
|
||||
|
||||
assigns = %{
|
||||
container:
|
||||
%{name: container_name} = container_id |> Containers.get_container!(current_user),
|
||||
container_block: container,
|
||||
container: container,
|
||||
container_block: container_block,
|
||||
ammo_group: ammo_group
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ defmodule CanneryWeb.CoreComponents do
|
||||
import CanneryWeb.{Gettext, ViewHelpers}
|
||||
alias Cannery.{Accounts, Accounts.Invite, Accounts.User}
|
||||
alias Cannery.{Ammo, Ammo.AmmoGroup}
|
||||
alias Cannery.{Containers, Containers.Container, Containers.Tag}
|
||||
alias Cannery.{Containers.Container, Containers.Tag}
|
||||
alias CanneryWeb.{Endpoint, HomeLive}
|
||||
alias CanneryWeb.Router.Helpers, as: Routes
|
||||
alias Phoenix.LiveView.{JS, Rendered}
|
||||
@ -91,7 +91,7 @@ defmodule CanneryWeb.CoreComponents do
|
||||
attr :original_count, :integer, default: nil
|
||||
attr :cpr, :integer, default: nil
|
||||
attr :last_used_date, Date, default: nil
|
||||
attr :show_container, :boolean, default: false
|
||||
attr :container, Container, default: nil
|
||||
slot(:inner_block)
|
||||
|
||||
def ammo_group_card(assigns)
|
||||
|
@ -50,17 +50,11 @@
|
||||
<%= gettext("$%{amount}", amount: display_currency(@cpr)) %>
|
||||
</span>
|
||||
|
||||
<span
|
||||
:if={@show_container && Containers.get_container!(@ammo_group.container_id, @current_user)}
|
||||
class="rounded-lg title text-lg"
|
||||
>
|
||||
<span :if={@container} class="rounded-lg title text-lg">
|
||||
<%= gettext("Container:") %>
|
||||
|
||||
<.link
|
||||
navigate={Routes.container_show_path(Endpoint, :show, @ammo_group.container_id)}
|
||||
class="link"
|
||||
>
|
||||
<%= Containers.get_container!(@ammo_group.container_id, @current_user).name %>
|
||||
<.link navigate={Routes.container_show_path(Endpoint, :show, @container)} class="link">
|
||||
<%= @container.name %>
|
||||
</.link>
|
||||
</span>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user