From 9a1a104c6d0b3daccf77665c6f35c406f5d7cb69 Mon Sep 17 00:00:00 2001 From: shibao Date: Fri, 1 Jul 2022 19:13:54 -0400 Subject: [PATCH] add pack and round count to containers --- CHANGELOG.md | 3 ++- lib/cannery/containers.ex | 12 ++++++++++++ lib/cannery_web/components/container_card.ex | 7 ++++++- lib/cannery_web/live/container_live/show.html.heex | 12 ++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee5b619..d4b5844 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # v0.5.4 - Rename "Ammo" tab to "Catalog", and "Manage" tab is now "Ammo" -- Ammo groups are now just referred to as Ammo +- Ammo groups are now just referred to as Ammo or "Packs" - URL paths now reflect new names +- Add pack and round count to container information # v0.5.3 - Update French translation: Thank you [duponin](https://udongein.xyz/users/duponin)! diff --git a/lib/cannery/containers.ex b/lib/cannery/containers.ex index 55e0b90..58b139e 100644 --- a/lib/cannery/containers.ex +++ b/lib/cannery/containers.ex @@ -207,6 +207,18 @@ defmodule Cannery.Containers do if count == 0, do: raise("could not delete container tag"), else: count end + @doc """ + Returns number of rounds in container. If data is already preloaded, then + there will be no db hit. + """ + @spec get_container_ammo_group_count!(Container.t()) :: non_neg_integer() + def get_container_ammo_group_count!(%Container{} = container) do + container + |> Repo.preload(:ammo_groups) + |> Map.fetch!(:ammo_groups) + |> Enum.count() + end + @doc """ Returns number of rounds in container. If data is already preloaded, then there will be no db hit. diff --git a/lib/cannery_web/components/container_card.ex b/lib/cannery_web/components/container_card.ex index fc298e9..db3af29 100644 --- a/lib/cannery_web/components/container_card.ex +++ b/lib/cannery_web/components/container_card.ex @@ -45,7 +45,12 @@ defmodule CanneryWeb.Components.ContainerCard do <% end %> - <%= if @container.ammo_groups do %> + <%= unless @container.ammo_groups |> Enum.empty?() do %> + + <%= gettext("Packs:") %> + <%= @container |> Containers.get_container_ammo_group_count!() %> + + <%= gettext("Rounds:") %> <%= @container |> Containers.get_container_rounds!() %> diff --git a/lib/cannery_web/live/container_live/show.html.heex b/lib/cannery_web/live/container_live/show.html.heex index 4393579..2fc68c6 100644 --- a/lib/cannery_web/live/container_live/show.html.heex +++ b/lib/cannery_web/live/container_live/show.html.heex @@ -22,6 +22,18 @@ <% end %> + <%= unless @container.ammo_groups |> Enum.empty?() do %> + + <%= gettext("Packs:") %> + <%= @container |> Containers.get_container_ammo_group_count!() %> + + + + <%= gettext("Rounds:") %> + <%= @container |> Containers.get_container_rounds!() %> + + <% end %> +
<%= live_patch to: Routes.container_show_path(Endpoint, :edit, @container), class: "text-primary-600 link",